From e45a2ce14883c4aeb67f4e0d2690915c36e25566 Mon Sep 17 00:00:00 2001 From: vishwesh Date: Sat, 25 Nov 2023 15:52:08 -0700 Subject: [PATCH] refactor: Encapsulate graphObject and create a getter function. This refactor commit aims to encapsulate the graphObject object by making it private to the GraphData class. A getter function called getGraph is now used to safely access the object. --- src/main/java/GraphData.java | 6 +++++- src/test/java/GraphDataTest.java | 12 ++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/GraphData.java b/src/main/java/GraphData.java index 6ad5bce..253b406 100644 --- a/src/main/java/GraphData.java +++ b/src/main/java/GraphData.java @@ -19,13 +19,17 @@ import java.util.List; public class GraphData { - Graph graphObject = new DefaultDirectedGraph<>(DefaultEdge.class); + private Graph graphObject = new DefaultDirectedGraph<>(DefaultEdge.class); enum Algorithm{ BFS, DFS } + public Graph getGraph() { + return graphObject; + } + public boolean parseGraph(String filepath) { // Import the graph from file diff --git a/src/test/java/GraphDataTest.java b/src/test/java/GraphDataTest.java index 048fa2c..28ff893 100644 --- a/src/test/java/GraphDataTest.java +++ b/src/test/java/GraphDataTest.java @@ -27,13 +27,13 @@ public void TestParseGraph() { nodes.add("B"); nodes.add("C"); nodes.add("D"); - assertEquals(nodes, graphApi.graphObject.vertexSet()); + assertEquals(nodes, graphApi.getGraph().vertexSet()); Set edges = new HashSet(); Set expected_edges = new HashSet(); edges.add("(A : B)"); edges.add("(A : C)"); edges.add("(A : D)"); - for (DefaultEdge e: graphApi.graphObject.edgeSet()) { + for (DefaultEdge e: graphApi.getGraph().edgeSet()) { expected_edges.add(e.toString()); } assertEquals(expected_edges, edges); @@ -67,7 +67,7 @@ public void TestOutputGraph() throws IOException { @DisplayName("Test outputGraph if node does not exist already") public void TestAddNode(){ assertTrue(graphApi.addNode("Z")); - assertTrue(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals("Z")))); + assertTrue(graphApi.getGraph().vertexSet().stream().anyMatch(v -> (v.equals("Z")))); } @Test @@ -83,7 +83,7 @@ public void TestAddNodes(){ String[] nodes = {"Z", "X", "Y"}; graphApi.addNodes(nodes); for(String node:nodes) { - assertTrue(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals(node)))); + assertTrue(graphApi.getGraph().vertexSet().stream().anyMatch(v -> (v.equals(node)))); } } @@ -98,7 +98,7 @@ public void TestAddNodesIfExistAlready(){ @DisplayName("Test removeNode if node exists.") public void TestRemoveNode() throws Exception { graphApi.removeNode("A"); - assertFalse(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals("A")))); + assertFalse(graphApi.getGraph().vertexSet().stream().anyMatch(v -> (v.equals("A")))); } @Test @@ -113,7 +113,7 @@ public void TestRemoveNodes() throws Exception { String[] nodes = {"A", "B", "C"}; graphApi.removeNodes(nodes); for(String node:nodes) { - assertFalse(graphApi.graphObject.vertexSet().stream().anyMatch(v -> (v.equals(node)))); + assertFalse(graphApi.getGraph().vertexSet().stream().anyMatch(v -> (v.equals(node)))); } }