From a6e8a2208181d17a1fde91451464043fb6d2cdbe Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Tue, 20 Feb 2018 11:26:04 +0100 Subject: [PATCH] Add origin node to arc. --- src/main/org/insa/graph/Arc.java | 23 ++++++++++++++----- src/main/org/insa/graph/Node.java | 2 +- .../org/insa/graph/io/BinaryGraphReader.java | 6 ++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/org/insa/graph/Arc.java b/src/main/org/insa/graph/Arc.java index 6405ba9..a84c824 100644 --- a/src/main/org/insa/graph/Arc.java +++ b/src/main/org/insa/graph/Arc.java @@ -5,7 +5,7 @@ import java.util.ArrayList; public class Arc { // Destination node. - private Node dest; + private Node origin, destination; // Length of the road (in meters). private int length; @@ -22,11 +22,13 @@ public class Arc { * @param roadInformation * @param points */ - public Arc(Node dest, int length, RoadInformation roadInformation) { - this.dest = dest; + public Arc(Node origin, Node dest, int length, RoadInformation roadInformation) { + this.origin = origin; + this.destination = dest; this.length = length; this.info = roadInformation; this.points = new ArrayList(); + origin.addSuccessor(this); } /** @@ -35,18 +37,27 @@ public class Arc { * @param roadInformation * @param points */ - public Arc(Node dest, int length, RoadInformation roadInformation, ArrayList points) { - this.dest = dest; + public Arc(Node origin, Node dest, int length, RoadInformation roadInformation, ArrayList points) { + this.origin = origin; + this.destination = dest; this.length = length; this.info = roadInformation; this.points = points; + origin.addSuccessor(this); + } + + /** + * @return Origin node of this arc. + */ + public Node getOrigin() { + return origin; } /** * @return Destination node of this arc. */ public Node getDestination() { - return dest; + return destination; } /** diff --git a/src/main/org/insa/graph/Node.java b/src/main/org/insa/graph/Node.java index 413da02..9c46437 100644 --- a/src/main/org/insa/graph/Node.java +++ b/src/main/org/insa/graph/Node.java @@ -30,7 +30,7 @@ public class Node implements Comparable { * * @param arc Arc to the successor. */ - public void addSuccessor(Arc arc) { + protected void addSuccessor(Arc arc) { successors.add(arc); } diff --git a/src/main/org/insa/graph/io/BinaryGraphReader.java b/src/main/org/insa/graph/io/BinaryGraphReader.java index f8d9446..8d4eecc 100644 --- a/src/main/org/insa/graph/io/BinaryGraphReader.java +++ b/src/main/org/insa/graph/io/BinaryGraphReader.java @@ -5,8 +5,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; -import javax.sound.midi.ControllerEventListener; - import org.insa.graph.Arc; import org.insa.graph.Graph; import org.insa.graph.Node; @@ -147,14 +145,14 @@ public class BinaryGraphReader extends BinaryReader implements AbstractGraphRead Node dest = nodes.get(destNode); // Add successor to initial arc. - orig.addSuccessor(new Arc(dest, length, info, points)); + new Arc(orig, dest, length, info, points); // And reverse arc if its a two-way road. if (!info.isOneWay()) { // Add without segments. ArrayList rPoints = new ArrayList(points); Collections.reverse(rPoints); - dest.addSuccessor(new Arc(orig, length, info, rPoints)); + new Arc(dest, orig, length, info, rPoints); } }