Remove access to underlying containers inside Graph and Node.

This commit is contained in:
Holt59
2018-03-22 19:47:01 +01:00
parent 797a4e1c8c
commit bdb378c079
12 changed files with 206 additions and 129 deletions

View File

@@ -1,7 +1,7 @@
package org.insa.graph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
@@ -11,7 +11,7 @@ import java.util.List;
* holds a list of nodes and each node holds a list of its successors.
*
*/
public class Graph {
public class Graph implements Iterable<Node> {
// Map identifier.
private final String mapId;
@@ -48,10 +48,28 @@ public class Graph {
}
/**
* @return Immutable view of the list of nodes of this graph.
* Fetch the node with the given ID.
*
* Complexity: O(1).
*
* @param id ID of the node to fetch.
*
* @return Node with the given ID.
*/
public List<Node> getNodes() {
return Collections.unmodifiableList(nodes);
public Node get(int id) {
return this.nodes.get(id);
}
/**
* @return Number of nodes in this graph.
*/
public int size() {
return this.nodes.size();
}
@Override
public Iterator<Node> iterator() {
return this.nodes.iterator();
}
/**
@@ -78,7 +96,7 @@ public class Graph {
}
for (Node node: nodes) {
Node orig = trNodes.get(node.getId());
for (Arc arc: node.getSuccessors()) {
for (Arc arc: node) {
if (arc.getRoadInformation().isOneWay()) {
Node dest = trNodes.get(arc.getDestination().getId());
dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),

View File

@@ -2,7 +2,7 @@ package org.insa.graph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Iterator;
/**
* Class representing a Node in a {@link Graph}.
@@ -13,7 +13,7 @@ import java.util.List;
* Nodes are comparable based on their ID.
*
*/
public class Node implements Comparable<Node> {
public class Node implements Comparable<Node>, Iterable<Arc> {
/**
* Link the two given nodes with one or two arcs (depending on roadInformation),
@@ -94,10 +94,22 @@ public class Node implements Comparable<Node> {
}
/**
* @return Immutable view of the list of successors of this node.
* @return Number of successors of this node.
*/
public List<Arc> getSuccessors() {
return Collections.unmodifiableList(successors);
public int getNumberOfSuccessors() {
return this.successors.size();
}
/**
* @return true if this node has at least one successor.
*/
public boolean hasSuccessors() {
return !this.successors.isEmpty();
}
@Override
public Iterator<Arc> iterator() {
return this.successors.iterator();
}
/**

View File

@@ -70,7 +70,7 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
* @throws IndexOutOfBoundsException if the node is not in the graph.
*/
protected Node readNode(Graph graph) throws IOException {
return graph.getNodes().get(dis.readInt());
return graph.get(dis.readInt());
}
}