[jboss-svn-commits] JBoss Common SVN: r2672 - common-core/trunk/src/main/java/org/jboss/util/graph.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Nov 11 18:38:41 EST 2007


Author: alesj
Date: 2007-11-11 18:38:41 -0500 (Sun, 11 Nov 2007)
New Revision: 2672

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/main/java/org/jboss/util/graph/VisitorEX.java
Log:
Visitor NPE when wrapping a null visitor.

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	2007-11-03 21:00:33 UTC (rev 2671)
+++ common-core/trunk/src/main/java/org/jboss/util/graph/Graph.java	2007-11-11 23:38:41 UTC (rev 2672)
@@ -21,10 +21,9 @@
  */
 package org.jboss.util.graph;
 
-import java.util.Collections;
+import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.LinkedList;
-import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -42,9 +41,9 @@
    /** Color used to mark nodes after descendants are completely visited */
    public static final int VISIT_COLOR_BLACK = 3;
    /** Vector<Vertex> of graph verticies */
-   private ArrayList<Vertex<T>> verticies;
+   private List<Vertex<T>> verticies;
    /** Vector<Edge> of edges in the graph */
-   private ArrayList<Edge<T>> edges;
+   private List<Edge<T>> edges;
    /** The vertex identified as the root of the graph */
    private Vertex<T> rootVertex;
 
@@ -98,6 +97,7 @@
    {
       return rootVertex;
    }
+
    /**
     * Set a root vertex. If root does no exist in the graph it is added.
     * @param root - the vertex to set as the root and optionally add if it
@@ -117,8 +117,7 @@
     */ 
    public Vertex<T> getVertex(int n)
    {
-      Vertex<T> v = verticies.get(n);
-      return v;
+      return verticies.get(n);
    }
 
    /**
@@ -148,6 +147,7 @@
          throw new IllegalArgumentException("from is not in graph");
       if( verticies.contains(to) == false )
          throw new IllegalArgumentException("to is not in graph");
+
       Edge<T> e = new Edge<T>(from, to, cost);
       if (from.findEdge(to) != null)
          return false;
@@ -245,11 +245,8 @@
     */ 
    public void clearMark()
    {
-      for (int i = 0; i < verticies.size(); i++)
-      {
-         Vertex<T> w = verticies.get(i);
+      for (Vertex<T> w : verticies)
          w.clearMark();
-      }
    }
 
    /**
@@ -259,11 +256,8 @@
     */ 
    public void clearEdges()
    {
-      for (int i = 0; i < edges.size(); i++)
-      {
-         Edge<T> e = (Edge<T>) edges.get(i);
+      for (Edge<T> e : edges)
          e.clearMark();
-      }
    }
 
    /**
@@ -278,7 +272,8 @@
       {
          public void visit(Graph<T> g, Vertex<T> v) throws RuntimeException
          {
-            visitor.visit(g, v);
+            if (visitor != null)
+               visitor.visit(g, v);
          }
       };
       this.depthFirstSearch(v, wrapper);
@@ -290,7 +285,7 @@
     * @param v - the Vertex to start the search from
     * @param visitor - the vistor to inform prior to 
     * @see Visitor#visit(Graph, Vertex)
-    * @throws Exception if visitor.visit throws an exception 
+    * @throws E if visitor.visit throws an exception
     */ 
    public <E extends Exception> void depthFirstSearch(Vertex<T> v, VisitorEX<T, E> visitor)
       throws E
@@ -321,11 +316,13 @@
       {
          public void visit(Graph<T> g, Vertex<T> v) throws RuntimeException
          {
-            visitor.visit(g, v);
+            if (visitor != null)
+               visitor.visit(g, v);
          }
       };
       this.breadthFirstSearch(v, wrapper);
    }
+
    /**
     * Perform a breadth first search of this graph, starting at v. The
     * vist may be cut short if visitor throws an exception during
@@ -334,7 +331,7 @@
     * @param v - the search starting point
     * @param visitor - the vistor whose vist method is called prior
     * to visting a vertex.
-    * @throws Exception if vistor.visit throws an exception
+    * @throws E if vistor.visit throws an exception
     */
    public <E extends Exception> void breadthFirstSearch(Vertex<T> v, VisitorEX<T, E> visitor)
       throws E
@@ -351,12 +348,13 @@
          for (int i = 0; i < v.getOutgoingEdgeCount(); i++)
          {
             Edge<T> e = v.getOutgoingEdge(i);
-            if (!e.getTo().visited())
+            Vertex<T> to = e.getTo();
+            if (!to.visited())
             {
-               q.add(e.getTo());
+               q.add(to);
                if( visitor != null )
-                  visitor.visit(this, e.getTo());
-               e.getTo().visit();
+                  visitor.visit(this, to);
+               to.visit();
             }
          }
       }
@@ -483,11 +481,8 @@
    public String toString()
    {
       StringBuffer tmp = new StringBuffer("Graph[");
-      for (int i = 0; i < verticies.size(); i++)
-      {
-         Vertex<T> v = verticies.get(i);
+      for (Vertex<T> v : verticies)
          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	2007-11-03 21:00:33 UTC (rev 2671)
+++ common-core/trunk/src/main/java/org/jboss/util/graph/Vertex.java	2007-11-11 23:38:41 UTC (rev 2672)
@@ -32,8 +32,8 @@
  */
 public class Vertex<T>
 {
-   private ArrayList<Edge<T>> incomingEdges;
-   private ArrayList<Edge<T>> outgoingEdges;
+   private List<Edge<T>> incomingEdges;
+   private List<Edge<T>> outgoingEdges;
    private String name;
    private boolean mark;
    private int markState;
@@ -63,7 +63,7 @@
    {
       incomingEdges = new ArrayList<Edge<T>>();
       outgoingEdges = new ArrayList<Edge<T>>();
-      name = new String(n);
+      name = n;
       mark = false;
       this.data = data;
    }
@@ -83,6 +83,7 @@
    {
       return this.data;
    }
+
    /**
     * @param data The data to set.
     */
@@ -122,6 +123,7 @@
       Edge<T> out = new Edge<T>(this, to, cost);
       outgoingEdges.add(out);
    }
+
    /**
     * Add an incoming edge starting at from
     * 
@@ -185,8 +187,7 @@
     */
    public Edge<T> getIncomingEdge(int i)
    {
-      Edge<T> e = incomingEdges.get(i);
-      return e;
+      return incomingEdges.get(i);
    }
 
    /**
@@ -206,6 +207,7 @@
    {
       return outgoingEdges.size();
    }
+
    /**
     * Get the ith outgoing edge
     * @param i the index into outgoing edges
@@ -213,8 +215,7 @@
     */
    public Edge<T> getOutgoingEdge(int i)
    {
-      Edge<T> e = outgoingEdges.get(i);
-      return e;
+      return outgoingEdges.get(i);
    }
 
    /**
@@ -229,14 +230,14 @@
    /**
     * Search the outgoing edges looking for an edge whose's
     * edge.to == dest.
+    * @param dest the destination
     * @return the outgoing edge going to dest if one exists,
     *    null otherwise.
     */
    public Edge<T> findEdge(Vertex<T> dest)
    {
-      for (int i = 0; i < outgoingEdges.size(); i++)
+      for (Edge<T> e : outgoingEdges)
       {
-         Edge<T> e = outgoingEdges.get(i);
          if (e.getTo() == dest)
             return e;
       }
@@ -307,15 +308,17 @@
    {
       mark = true;
    }
+
    /**
     * Set the mark state to state.
     * 
-    * @param state 
+    * @param state the state
     */
    public void setMarkState(int state)
    {
       markState = state;
    }
+
    /**
     * Get the mark state value.
     * @return

Modified: common-core/trunk/src/main/java/org/jboss/util/graph/VisitorEX.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/graph/VisitorEX.java	2007-11-03 21:00:33 UTC (rev 2671)
+++ common-core/trunk/src/main/java/org/jboss/util/graph/VisitorEX.java	2007-11-11 23:38:41 UTC (rev 2672)
@@ -29,13 +29,13 @@
  * @version $Revision$
  */
 public interface VisitorEX<T, E extends Exception>
-{
-   
+{   
    /**
     * Called by the graph traversal methods when a vertex is first visited.
     * 
     * @param g - the graph
     * @param v - the vertex being visited.
+    * @throws E exception for any error
     */
    public void visit(Graph<T> g, Vertex<T> v) throws E;
 }




More information about the jboss-svn-commits mailing list