From 5b733e25c705db5b3635af65a6af333bb98af200 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Thu, 22 Mar 2018 20:05:42 +0100 Subject: [PATCH] Return unmodifiable iterators from Node and Graph. Make Node and Graph final. --- src/main/org/insa/graph/Graph.java | 5 +++-- src/main/org/insa/graph/Node.java | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/org/insa/graph/Graph.java b/src/main/org/insa/graph/Graph.java index 570dcbb..5461df8 100644 --- a/src/main/org/insa/graph/Graph.java +++ b/src/main/org/insa/graph/Graph.java @@ -1,6 +1,7 @@ package org.insa.graph; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -11,7 +12,7 @@ import java.util.List; * holds a list of nodes and each node holds a list of its successors. * */ -public class Graph implements Iterable { +public final class Graph implements Iterable { // Map identifier. private final String mapId; @@ -36,7 +37,7 @@ public class Graph implements Iterable { public Graph(String mapId, String mapName, List nodes, GraphStatistics graphStatistics) { this.mapId = mapId; this.mapName = mapName; - this.nodes = nodes; + this.nodes = Collections.unmodifiableList(nodes); this.graphStatistics = graphStatistics; } diff --git a/src/main/org/insa/graph/Node.java b/src/main/org/insa/graph/Node.java index 3722bc9..694b5a5 100644 --- a/src/main/org/insa/graph/Node.java +++ b/src/main/org/insa/graph/Node.java @@ -13,7 +13,7 @@ import java.util.Iterator; * Nodes are comparable based on their ID. * */ -public class Node implements Comparable, Iterable { +public final class Node implements Comparable, Iterable { /** * Link the two given nodes with one or two arcs (depending on roadInformation), @@ -109,7 +109,7 @@ public class Node implements Comparable, Iterable { @Override public Iterator iterator() { - return this.successors.iterator(); + return Collections.unmodifiableList(this.successors).iterator(); } /**