diff --git a/src/berack96/sim/util/graph/Graph.java b/src/berack96/sim/util/graph/Graph.java index 6b88e23..44e2747 100644 --- a/src/berack96/sim/util/graph/Graph.java +++ b/src/berack96/sim/util/graph/Graph.java @@ -116,7 +116,7 @@ public interface Graph extends Iterable { * @throws NullPointerException if one of the param is null * @throws IllegalArgumentException if the vertex is not contained in the graph */ - void mark(V vertex, String mark) throws NullPointerException, IllegalArgumentException; + void mark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException; /** * Remove the selected mark from the vertex.
@@ -126,7 +126,7 @@ public interface Graph extends Iterable { * @throws NullPointerException if a param is null * @throws IllegalArgumentException if the vertex is not contained in the graph */ - void unMark(V vertex, String mark) throws NullPointerException, IllegalArgumentException; + void unMark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException; /** * Unmark the vertex selected.
@@ -148,7 +148,7 @@ public interface Graph extends Iterable { * @throws NullPointerException if the vertex is null * @throws IllegalArgumentException if the vertex is not contained in the graph */ - Collection getMarks(V vertex) throws NullPointerException, IllegalArgumentException; + Collection getMarks(V vertex) throws NullPointerException, IllegalArgumentException; /** * Remove the selected mark from all the vertices @@ -156,7 +156,7 @@ public interface Graph extends Iterable { * @param mark the mark to remove * @throws NullPointerException if the mark is null */ - void unMarkAll(String mark) throws NullPointerException; + void unMarkAll(Object mark) throws NullPointerException; /** * Remove all the marker to all the vertex.
@@ -488,7 +488,7 @@ public interface Graph extends Iterable { * @param marker the marker * @return a sub-graph of the current graph */ - Graph subGraph(String marker); + Graph subGraph(Object marker); /** * Get the minimum path from the source vertex to the destination vertex.
diff --git a/src/berack96/sim/util/graph/MapGraph.java b/src/berack96/sim/util/graph/MapGraph.java index 451eeb9..d13b04d 100644 --- a/src/berack96/sim/util/graph/MapGraph.java +++ b/src/berack96/sim/util/graph/MapGraph.java @@ -32,7 +32,7 @@ public class MapGraph implements Graph { /** * Map that contains the vertex as key and all the marker as the value associated with that vertex. */ - private final Map> marked = new HashMap<>(); + private final Map> marked = new HashMap<>(); /** * Need this variable for not calculating each time the SCC or the cyclic part if the graph doesn't change @@ -103,16 +103,16 @@ public class MapGraph implements Graph { } @Override - public void mark(V vertex, String mark) throws NullPointerException, IllegalArgumentException { + public void mark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException { checkNullAndExist(vertex); checkNull(mark); - Set set = marked.computeIfAbsent(vertex, (m) -> new HashSet<>()); + Set set = marked.computeIfAbsent(vertex, (m) -> new HashSet<>()); set.add(mark); } @Override - public void unMark(V vertex, String mark) throws NullPointerException, IllegalArgumentException { + public void unMark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException { checkNullAndExist(vertex); checkNull(mark); marked.get(vertex).remove(mark); @@ -125,13 +125,13 @@ public class MapGraph implements Graph { } @Override - public Set getMarks(V vertex) throws NullPointerException, IllegalArgumentException { + public Set getMarks(V vertex) throws NullPointerException, IllegalArgumentException { checkNullAndExist(vertex); return marked.computeIfAbsent(vertex, (m) -> new HashSet<>()); } @Override - public void unMarkAll(String mark) { + public void unMarkAll(Object mark) { checkNull(mark); marked.forEach((v, m) -> m.remove(mark)); } @@ -394,7 +394,7 @@ public class MapGraph implements Graph { } @Override - public Graph subGraph(final String marker) { + public Graph subGraph(final Object marker) { final Graph graph = new MapGraph<>(); final Set allVertices = new HashSet<>(); diff --git a/src/berack96/sim/util/graph/Vertex.java b/src/berack96/sim/util/graph/Vertex.java index 7c36eb6..3e29425 100644 --- a/src/berack96/sim/util/graph/Vertex.java +++ b/src/berack96/sim/util/graph/Vertex.java @@ -57,17 +57,29 @@ public class Vertex { * * @param mark the marker * @throws NullPointerException if the marker is null - * @throws UnsupportedOperationException if the vertes is not in the graph anymore + * @throws UnsupportedOperationException if the vertex is not in the graph anymore */ - public void mark(String mark) throws NullPointerException, UnsupportedOperationException { + public void mark(Object mark) throws NullPointerException, UnsupportedOperationException { throwIfNotContained(); graph.mark(vertex, mark); } + /** + * Remove the specified mark from this vertex + * + * @param mark the marker + * @throws NullPointerException if the mark is null + * @throws UnsupportedOperationException if the vertex is not in the graph anymore + */ + public void unMark(Object mark) throws UnsupportedOperationException { + throwIfNotContained(); + graph.unMark(vertex, mark); + } + /** * Remove all the marker from the vertex * - * @throws UnsupportedOperationException if the vertes is not in the graph anymore + * @throws UnsupportedOperationException if the vertex is not in the graph anymore */ public void unMark() throws UnsupportedOperationException { throwIfNotContained(); @@ -78,9 +90,9 @@ public class Vertex { * Get all the marks that are associated with this vertex * * @return a set of marks - * @throws UnsupportedOperationException if the vertes is not in the graph anymore + * @throws UnsupportedOperationException if the vertex is not in the graph anymore */ - public Collection getMarks() throws UnsupportedOperationException { + public Collection getMarks() throws UnsupportedOperationException { throwIfNotContained(); return graph.getMarks(vertex); } diff --git a/test/berack96/test/sim/TestGraph.java b/test/berack96/test/sim/TestGraph.java index d74a29a..1b53131 100644 --- a/test/berack96/test/sim/TestGraph.java +++ b/test/berack96/test/sim/TestGraph.java @@ -1140,6 +1140,7 @@ public class TestGraph { shouldThrow(nullException, () -> vertex.addChild(null, 3)); shouldThrow(nullException, () -> vertex.addChild(null, null)); shouldThrow(nullException, () -> vertex.mark(null)); + shouldThrow(nullException, () -> vertex.unMark(null)); shouldThrow(nullException, () -> vertex.removeChild(null)); shouldThrow(new NullPointerException(), () -> vertex.visit(null, null)); @@ -1185,6 +1186,13 @@ public class TestGraph { shouldContain(graph.getMarks(vertex.getValue()), "ciao", "ciao2"); vertex.unMark(); shouldContain(vertex.getMarks()); + vertex.mark("cio"); + vertex.mark(1); + shouldContain(vertex.getMarks(), "cio", 1); + vertex.unMark(1); + shouldContain(vertex.getMarks(), "cio"); + vertex.unMark("cio"); + shouldContain(vertex.getMarks()); vertex.removeChild("1"); shouldContain(vertex.getChildren(), "2");