Mercurial > hg > release > thermostat-2.0
changeset 2298:a9decacf1601
Add clear method to graph
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-May/018763.html
reviewed-by: jerboaa
author | Mario Torre <neugens.limasoftware@gmail.com> |
---|---|
date | Tue, 17 May 2016 14:45:47 +0200 |
parents | 524aeb3ae995 |
children | 08d858358908 |
files | platform/collections/src/main/java/com/redhat/thermostat/collections/graph/Graph.java platform/collections/src/main/java/com/redhat/thermostat/collections/graph/HashGraph.java platform/collections/src/test/java/com/redhat/thermostat/collections/graph/GraphTest.java |
diffstat | 3 files changed, 39 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/platform/collections/src/main/java/com/redhat/thermostat/collections/graph/Graph.java Fri May 13 12:28:07 2016 -0400 +++ b/platform/collections/src/main/java/com/redhat/thermostat/collections/graph/Graph.java Tue May 17 14:45:47 2016 +0200 @@ -83,4 +83,10 @@ */ @Override Iterator<Node> iterator(); + + /** + * Clear the graph by removing all its content. The running time of this + * method is implementation dependent. + */ + void clear(); }
--- a/platform/collections/src/main/java/com/redhat/thermostat/collections/graph/HashGraph.java Fri May 13 12:28:07 2016 -0400 +++ b/platform/collections/src/main/java/com/redhat/thermostat/collections/graph/HashGraph.java Tue May 17 14:45:47 2016 +0200 @@ -112,4 +112,10 @@ public Iterator<Node> iterator() { return relationships.keySet().iterator(); } + + @Override + public void clear() { + relationships.clear(); + size = 0; + } }
--- a/platform/collections/src/test/java/com/redhat/thermostat/collections/graph/GraphTest.java Fri May 13 12:28:07 2016 -0400 +++ b/platform/collections/src/test/java/com/redhat/thermostat/collections/graph/GraphTest.java Tue May 17 14:45:47 2016 +0200 @@ -41,6 +41,7 @@ import java.util.Set; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; /** @@ -129,4 +130,30 @@ assertEquals(3, graph.size()); assertEquals(2, graph.order()); } + + @Test + public void testClear() { + Graph graph = new HashGraph(); + assertEquals(0, graph.size()); + assertEquals(0, graph.order()); + + Node a = new Node("A"); + Node b = new Node("B"); + + graph.addRelationship(a, "knows", b); + + assertEquals(1, graph.size()); + assertEquals(2, graph.order()); + + graph.clear(); + assertEquals(0, graph.size()); + assertEquals(0, graph.order()); + + Set<Relationship> rel = graph.getRelationships(a); + assertNull(rel); + + graph.addRelationship(a, "knows", b); + assertEquals(1, graph.size()); + assertEquals(2, graph.order()); + } }