[jboss-svn-commits] JBoss Common SVN: r3873 - in common-core/trunk/src: test/java/org/jboss/test/util/test/graph and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Dec 22 10:41:57 EST 2009
Author: alesj
Date: 2009-12-22 10:41:57 -0500 (Tue, 22 Dec 2009)
New Revision: 3873
Modified:
common-core/trunk/src/main/java/org/jboss/util/graph/Graph.java
common-core/trunk/src/main/java/org/jboss/util/graph/Vertex.java
common-core/trunk/src/test/java/org/jboss/test/util/test/graph/GraphTestCase.java
Log:
[JBCOMMON-101]; fix Verte::remove(Edge).
Modified: common-core/trunk/src/main/java/org/jboss/util/graph/Graph.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/graph/Graph.java 2009-12-22 15:35:34 UTC (rev 3872)
+++ common-core/trunk/src/main/java/org/jboss/util/graph/Graph.java 2009-12-22 15:41:57 UTC (rev 3873)
@@ -23,13 +23,16 @@
import java.util.ArrayList;
import java.util.Comparator;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
/**
* A directed graph data structure.
*
* @author Scott.Stark at jboss.org
+ * @author Ales.Justin at jboss.org
* @version $Revision$
* @param <T>
*/
@@ -42,8 +45,8 @@
public static final int VISIT_COLOR_GREY = 2;
/** Color used to mark nodes after descendants are completely visited */
public static final int VISIT_COLOR_BLACK = 3;
- /** Vector<Vertex> of graph verticies */
- private List<Vertex<T>> verticies;
+ /** Map<String, Vertex> of graph verticies */
+ private Map<String, Vertex<T>> verticies;
/** Vector<Edge> of edges in the graph */
private List<Edge<T>> edges;
/** The vertex identified as the root of the graph */
@@ -54,7 +57,7 @@
*/
public Graph()
{
- verticies = new ArrayList<Vertex<T>>();
+ verticies = new LinkedHashMap<String, Vertex<T>>();
edges = new ArrayList<Edge<T>>();
}
@@ -74,12 +77,12 @@
*/
public boolean addVertex(Vertex<T> v)
{
- boolean added = false;
- if( verticies.contains(v) == false )
+ if( verticies.containsValue(v) == false )
{
- added = verticies.add(v);
+ verticies.put(v.getName(), v);
+ return true;
}
- return added;
+ return false;
}
/**
@@ -108,8 +111,8 @@
public void setRootVertex(Vertex<T> root)
{
this.rootVertex = root;
- if( verticies.contains(root) == false )
- this.addVertex(root);
+ if( verticies.containsValue(root) == false )
+ addVertex(root);
}
/**
@@ -119,7 +122,7 @@
*/
public Vertex<T> getVertex(int n)
{
- return verticies.get(n);
+ return getVerticies().get(n);
}
/**
@@ -129,7 +132,7 @@
*/
public List<Vertex<T>> getVerticies()
{
- return this.verticies;
+ return new ArrayList<Vertex<T>>(verticies.values());
}
/**
@@ -145,9 +148,9 @@
public boolean addEdge(Vertex<T> from, Vertex<T> to, int cost)
throws IllegalArgumentException
{
- if( verticies.contains(from) == false )
+ if( verticies.containsValue(from) == false )
throw new IllegalArgumentException("from is not in graph");
- if( verticies.contains(to) == false )
+ if( verticies.containsValue(to) == false )
throw new IllegalArgumentException("to is not in graph");
Edge<T> e = new Edge<T>(from, to, cost);
@@ -194,10 +197,10 @@
*/
public boolean removeVertex(Vertex<T> v)
{
- if (!verticies.contains(v))
+ if (!verticies.containsValue(v))
return false;
- verticies.remove(v);
+ verticies.remove(v.getName());
if( v == rootVertex )
rootVertex = null;
@@ -247,7 +250,7 @@
*/
public void clearMark()
{
- for (Vertex<T> w : verticies)
+ for (Vertex<T> w : verticies.values())
w.clearMark();
}
@@ -282,7 +285,7 @@
/**
* Perform a depth first serach using recursion. The search may
* be cut short if the visitor throws an exception.
- * @param <E>
+ * @param <E> exception type
*
* @param v - the Vertex to start the search from
* @param visitor - the vistor to inform prior to
@@ -329,7 +332,7 @@
* Perform a breadth first search of this graph, starting at v. The
* vist may be cut short if visitor throws an exception during
* a vist callback.
- * @param <E>
+ * @param <E> exception type
*
* @param v - the search starting point
* @param visitor - the vistor whose vist method is called prior
@@ -397,16 +400,7 @@
*/
public Vertex<T> findVertexByName(String name)
{
- Vertex<T> match = null;
- for(Vertex<T> v : verticies)
- {
- if( name.equals(v.getName()) )
- {
- match = v;
- break;
- }
- }
- return match;
+ return verticies.get(name);
}
/**
@@ -420,7 +414,7 @@
public Vertex<T> findVertexByData(T data, Comparator<T> compare)
{
Vertex<T> match = null;
- for(Vertex<T> v : verticies)
+ for(Vertex<T> v : verticies.values())
{
if( compare.compare(data, v.getData()) == 0 )
{
@@ -484,7 +478,7 @@
public String toString()
{
StringBuffer tmp = new StringBuffer("Graph[");
- for (Vertex<T> v : verticies)
+ for (Vertex<T> v : verticies.values())
tmp.append(v);
tmp.append(']');
return tmp.toString();
Modified: common-core/trunk/src/main/java/org/jboss/util/graph/Vertex.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/graph/Vertex.java 2009-12-22 15:35:34 UTC (rev 3872)
+++ common-core/trunk/src/main/java/org/jboss/util/graph/Vertex.java 2009-12-22 15:41:57 UTC (rev 3873)
@@ -28,6 +28,7 @@
* A named graph vertex with optional data.
*
* @author Scott.Stark at jboss.org
+ * @author Ales.Justin at jboss.org
* @version $Revision$
* @param <T>
*/
@@ -148,9 +149,9 @@
public boolean hasEdge(Edge<T> e)
{
if (e.getFrom() == this)
+ return outgoingEdges.contains(e);
+ else if (e.getTo() == this)
return incomingEdges.contains(e);
- else if (e.getTo() == this)
- return outgoingEdges.contains(e);
else
return false;
}
@@ -165,9 +166,9 @@
public boolean remove(Edge<T> e)
{
if (e.getFrom() == this)
+ outgoingEdges.remove(e);
+ else if (e.getTo() == this)
incomingEdges.remove(e);
- else if (e.getTo() == this)
- outgoingEdges.remove(e);
else
return false;
return true;
Modified: common-core/trunk/src/test/java/org/jboss/test/util/test/graph/GraphTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/graph/GraphTestCase.java 2009-12-22 15:35:34 UTC (rev 3872)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/graph/GraphTestCase.java 2009-12-22 15:41:57 UTC (rev 3873)
@@ -23,11 +23,12 @@
import java.util.ArrayList;
import java.util.Comparator;
+import java.util.List;
import junit.framework.TestCase;
+import org.jboss.util.graph.Edge;
import org.jboss.util.graph.Graph;
-import org.jboss.util.graph.Edge;
import org.jboss.util.graph.Vertex;
import org.jboss.util.graph.Visitor;
@@ -35,6 +36,7 @@
* Tests of the graph package
*
* @author Scott.Stark at jboss.org
+ * @author Ales.Justin at jboss.org
* @version $Revision: 1.1 $
*/
@SuppressWarnings("unchecked")
@@ -59,11 +61,49 @@
super(name);
}
+ public void testBasicOps() throws Exception
+ {
+ Graph graph = new Graph();
+
+ Vertex v1 = new Vertex("1");
+ graph.addVertex(v1);
+ Vertex v2 = new Vertex("2");
+ graph.addVertex(v2);
+
+ graph.addEdge(v1, v2, 0);
+ List edges = graph.getEdges();
+ assertNotNull(edges);
+ assertEquals(1, edges.size());
+ Edge e = (Edge)edges.get(0);
+ List outgoing1 = v1.getOutgoingEdges();
+ assertEquals(1, outgoing1.size());
+ assertEquals(e, outgoing1.get(0));
+ List incoming1 = v1.getIncomingEdges();
+ assertTrue(incoming1 == null || incoming1.isEmpty());
+ List outgoing2 = v2.getOutgoingEdges();
+ assertTrue(outgoing2 == null || outgoing2.isEmpty());
+ List incoming2 = v2.getIncomingEdges();
+ assertEquals(1, incoming2.size());
+ assertEquals(e, incoming2.get(0));
+
+ graph.removeEdge(v1, v2);
+ edges = graph.getEdges();
+ assertNotNull(edges);
+ assertEquals(0, edges.size());
+ outgoing1 = v1.getOutgoingEdges();
+ assertTrue(outgoing1 == null || outgoing1.isEmpty());
+ incoming1 = v1.getIncomingEdges();
+ assertTrue(incoming1 == null || incoming1.isEmpty());
+ outgoing2 = v2.getOutgoingEdges();
+ assertTrue(outgoing2 == null || outgoing2.isEmpty());
+ incoming2 = v2.getIncomingEdges();
+ assertTrue(incoming2 == null || incoming2.isEmpty());
+ }
+
/** Depth first search of digraph1
* @throws Exception
*/
- public void testDFS()
- throws Exception
+ public void testDFS() throws Exception
{
Graph graph = buildGraph1();
More information about the jboss-svn-commits
mailing list