You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/06/13 21:43:23 UTC

svn commit: r1135247 - /commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java

Author: simonetripodi
Date: Mon Jun 13 19:43:22 2011
New Revision: 1135247

URL: http://svn.apache.org/viewvc?rev=1135247&view=rev
Log:
handled potential NPEs

Modified:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java?rev=1135247&r1=1135246&r2=1135247&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/visit/Visit.java Mon Jun 13 19:43:22 2011
@@ -46,7 +46,19 @@ public final class Visit
     public final <V extends Vertex, E extends Edge<V>> void breadthFirstSearch( Graph<V, E> graph, V source,
                                                                                 GraphVisitHandler<V, E> handler )
     {
-        handler.discoverGraph( graph );
+        if ( graph == null )
+        {
+            throw new IllegalArgumentException( "Graph has to be visited can not be null." );
+        }
+        if ( source == null )
+        {
+            throw new IllegalArgumentException( "Root node the search begins from can not be null." );
+        }
+
+        if ( handler != null )
+        {
+            handler.discoverGraph( graph );
+        }
 
         LinkedList<V> vertexQueue = new LinkedList<V>();
         vertexQueue.add( source );
@@ -58,7 +70,10 @@ public final class Visit
         {
             V v = vertexQueue.remove();
 
-            handler.discoverVertex( v );
+            if ( handler != null )
+            {
+                handler.discoverVertex( v );
+            }
 
             for ( E e : graph.getEdges( v ) )
             {
@@ -66,18 +81,30 @@ public final class Visit
 
                 if ( !visitedVetices.add( w ) )
                 {
-                    handler.discoverEdge( e );
+                    if ( handler != null )
+                    {
+                        handler.discoverEdge( e );
+                    }
 
                     vertexQueue.addFirst( v );
 
-                    handler.finishEdge( e );
+                    if ( handler != null )
+                    {
+                        handler.finishEdge( e );
+                    }
                 }
             }
 
-            handler.finishVertex( v );
+            if ( handler != null )
+            {
+                handler.finishVertex( v );
+            }
         }
 
-        handler.finishGraph( graph );
+        if ( handler != null )
+        {
+            handler.finishGraph( graph );
+        }
     }
 
     /**
@@ -92,7 +119,19 @@ public final class Visit
     public final <V extends Vertex, E extends Edge<V>> void depthFirstSearch( Graph<V, E> graph, V source,
                                                                               GraphVisitHandler<V, E> handler )
     {
-        handler.discoverGraph( graph );
+        if ( graph == null )
+        {
+            throw new IllegalArgumentException( "Graph has to be visited can not be null." );
+        }
+        if ( source == null )
+        {
+            throw new IllegalArgumentException( "Root node the search begins from can not be null." );
+        }
+
+        if ( handler != null )
+        {
+            handler.discoverGraph( graph );
+        }
 
         Stack<V> vertexStack = new Stack<V>();
         vertexStack.push( source );
@@ -104,7 +143,10 @@ public final class Visit
         {
             V v = vertexStack.pop();
 
-            handler.discoverVertex( v );
+            if ( handler != null )
+            {
+                handler.discoverVertex( v );
+            }
 
             for ( E e : graph.getEdges( v ) )
             {
@@ -112,18 +154,30 @@ public final class Visit
 
                 if ( !visitedVetices.add( w ) )
                 {
-                    handler.discoverEdge( e );
+                    if ( handler != null )
+                    {
+                        handler.discoverEdge( e );
+                    }
 
                     vertexStack.push( w );
 
-                    handler.finishEdge( e );
+                    if ( handler != null )
+                    {
+                        handler.finishEdge( e );
+                    }
                 }
             }
 
-            handler.finishVertex( v );
+            if ( handler != null )
+            {
+                handler.finishVertex( v );
+            }
         }
 
-        handler.finishGraph( graph );
+        if ( handler != null )
+        {
+            handler.finishGraph( graph );
+        }
     }
 
     /**