Browse Source

Return unmodifiable iterators from Node and Graph. Make Node and Graph final.

Holt59 6 years ago
parent
commit
5b733e25c7
2 changed files with 5 additions and 4 deletions
  1. 3
    2
      src/main/org/insa/graph/Graph.java
  2. 2
    2
      src/main/org/insa/graph/Node.java

+ 3
- 2
src/main/org/insa/graph/Graph.java View File

@@ -1,6 +1,7 @@
1 1
 package org.insa.graph;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.Collections;
4 5
 import java.util.Iterator;
5 6
 import java.util.List;
6 7
 
@@ -11,7 +12,7 @@ import java.util.List;
11 12
  * holds a list of nodes and each node holds a list of its successors.
12 13
  *
13 14
  */
14
-public class Graph implements Iterable<Node> {
15
+public final class Graph implements Iterable<Node> {
15 16
 
16 17
     // Map identifier.
17 18
     private final String mapId;
@@ -36,7 +37,7 @@ public class Graph implements Iterable<Node> {
36 37
     public Graph(String mapId, String mapName, List<Node> nodes, GraphStatistics graphStatistics) {
37 38
         this.mapId = mapId;
38 39
         this.mapName = mapName;
39
-        this.nodes = nodes;
40
+        this.nodes = Collections.unmodifiableList(nodes);
40 41
         this.graphStatistics = graphStatistics;
41 42
     }
42 43
 

+ 2
- 2
src/main/org/insa/graph/Node.java View File

@@ -13,7 +13,7 @@ import java.util.Iterator;
13 13
  * Nodes are comparable based on their ID.
14 14
  *
15 15
  */
16
-public class Node implements Comparable<Node>, Iterable<Arc> {
16
+public final class Node implements Comparable<Node>, Iterable<Arc> {
17 17
 
18 18
     /**
19 19
      * Link the two given nodes with one or two arcs (depending on roadInformation),
@@ -109,7 +109,7 @@ public class Node implements Comparable<Node>, Iterable<Arc> {
109 109
 
110 110
     @Override
111 111
     public Iterator<Arc> iterator() {
112
-        return this.successors.iterator();
112
+        return Collections.unmodifiableList(this.successors).iterator();
113 113
     }
114 114
 
115 115
     /**

Loading…
Cancel
Save