Author: simonetripodi
Date: Fri Jan 20 21:44:53 2012
New Revision: 1234159

URL: http://svn.apache.org/viewvc?rev=1234159&view=rev
Log:
reusing assert expressions instead of repeat the same code

Modified:
    
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/VertexPair.java
    
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java
    
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
    
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryPath.java
    
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java
    
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/VertexPair.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/VertexPair.java?rev=1234159&r1=1234158&r2=1234159&view=diff
==============================================================================
--- 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/VertexPair.java
 (original)
+++ 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/VertexPair.java
 Fri Jan 20 21:44:53 2012
@@ -19,6 +19,8 @@ package org.apache.commons.graph;
  * under the License.
  */
 
+import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+
 import static java.lang.String.format;
 
 /**
@@ -41,16 +43,8 @@ public final class VertexPair<V extends 
      */
     public VertexPair( V head, V tail )
     {
-        if ( head == null )
-        {
-            throw new IllegalArgumentException( "Impossible to construct a 
Vertex with a null head" );
-        }
-        if ( tail == null )
-        {
-            throw new IllegalArgumentException( "Impossible to construct a 
Vertex with a null tail" );
-        }
-        this.head = head;
-        this.tail = tail;
+        this.head = checkNotNull( head, "Impossible to construct a Vertex with 
a null head" );
+        this.tail = checkNotNull( tail, "Impossible to construct a Vertex with 
a null tail" );
     }
 
     /**

Modified: 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java?rev=1234159&r1=1234158&r2=1234159&view=diff
==============================================================================
--- 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java
 (original)
+++ 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/coloring/ColoredVertices.java
 Fri Jan 20 21:44:53 2012
@@ -19,6 +19,8 @@ package org.apache.commons.graph.colorin
  * under the License.
  */
 
+import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -87,10 +89,7 @@ public final class ColoredVertices<V ext
      */
     public C getColor( V v )
     {
-        if ( v == null )
-        {
-            throw new IllegalArgumentException( "Impossible to get the color 
for a null Vertex" );
-        }
+        v = checkNotNull( v, "Impossible to get the color for a null Vertex" );
 
         return coloredVertices.get( v );
     }

Modified: 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java?rev=1234159&r1=1234158&r2=1234159&view=diff
==============================================================================
--- 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
 (original)
+++ 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
 Fri Jan 20 21:44:53 2012
@@ -19,6 +19,8 @@ package org.apache.commons.graph.export;
  * under the License.
  */
 
+import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -79,10 +81,7 @@ public final class DotExporter
                                                                      File 
outputFile )
         throws IOException
     {
-        if ( outputFile == null )
-        {
-            throw new IllegalArgumentException( "DOT exporter requires a not 
null file" );
-        }
+        outputFile = checkNotNull( outputFile, "DOT exporter requires a not 
null file" );
         dotExport( graph, new FileOutputStream( outputFile ) );
     }
 
@@ -115,10 +114,7 @@ public final class DotExporter
                                                                      
OutputStream outputStream )
         throws IOException
     {
-        if ( outputStream == null )
-        {
-            throw new IllegalArgumentException( "DOT exporter requires a not 
null output stream" );
-        }
+        outputStream = checkNotNull( outputStream, "DOT exporter requires a 
not null output stream" );
         dotExport( graph, new OutputStreamWriter( outputStream ) );
     }
 
@@ -150,14 +146,8 @@ public final class DotExporter
     public static <V extends Vertex, E extends Edge> void dotExport( Graph<V, 
E> graph, String name, Writer writer )
         throws IOException
     {
-        if ( graph == null )
-        {
-            throw new IllegalArgumentException( "Impossible to export a null 
Graph to DOT notation" );
-        }
-        if ( writer == null )
-        {
-            throw new IllegalArgumentException( "DOT exporter requires a not 
null writer" );
-        }
+        graph = checkNotNull( graph, "Impossible to export a null Graph to DOT 
notation" );
+        writer = checkNotNull( writer, "DOT exporter requires a not null 
writer" );
 
         String graphDeclaration;
         String connector;

Modified: 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryPath.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryPath.java?rev=1234159&r1=1234158&r2=1234159&view=diff
==============================================================================
--- 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryPath.java
 (original)
+++ 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryPath.java
 Fri Jan 20 21:44:53 2012
@@ -19,6 +19,8 @@ package org.apache.commons.graph.model;
  * under the License.
  */
 
+import static org.apache.commons.graph.utils.Assertions.*;
+
 import static java.util.Arrays.asList;
 import static java.lang.String.format;
 import static java.util.Collections.unmodifiableList;
@@ -60,17 +62,8 @@ public class InMemoryPath<V extends Vert
 
     public InMemoryPath( V start, V target )
     {
-        if ( start == null )
-        {
-            throw new IllegalArgumentException( "Path source cannot be null" );
-        }
-        if ( target == null )
-        {
-            throw new IllegalArgumentException( "Path target cannot be null" );
-        }
-
-        this.source = start;
-        this.target = target;
+        this.source = checkNotNull( start, "Path source cannot be null" );
+        this.target = checkNotNull( target, "Path target cannot be null" );
     }
 
     /**
@@ -161,15 +154,9 @@ public class InMemoryPath<V extends Vert
      */
     public int getDegree( V v )
     {
-        if ( v == null )
-        {
-            throw new IllegalArgumentException( "Impossible to get the degree 
of a null vertex" );
-        }
-
-        if ( !successors.containsKey( v ) )
-        {
-            throw new IllegalArgumentException( "Impossible to get the degree 
of vertex not contained in this path" );
-        }
+        v = checkNotNull( v, "Impossible to get the degree of a null vertex" );
+        checkArgument( successors.containsKey( v ),
+                       "Impossible to get the degree of input vertex; %s not 
contained in this path", v );
 
         if ( source.equals( v ) || target.equals( v ) )
         {
@@ -184,20 +171,15 @@ public class InMemoryPath<V extends Vert
      */
     public Iterable<V> getConnectedVertices( V v )
     {
-        if ( v == null )
-        {
-            throw new IllegalArgumentException( "Impossible to get the 
successor of a null vertex" );
-        }
+        v = checkNotNull( v, "Impossible to get the degree of a null vertex" );
 
         if ( target.equals( v ) )
         {
             return null;
         }
 
-        if ( !successors.containsKey( v ) )
-        {
-            throw new IllegalArgumentException( "Impossible to get the 
successor of vertex not contained in this path" );
-        }
+        checkArgument( successors.containsKey( v ),
+                       "Impossible to get the degree of input vertex; %s not 
contained in this path", v );
 
         @SuppressWarnings( "unchecked" ) // type driven by input type
         List<V> connected = asList( successors.get( v ) );

Modified: 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java?rev=1234159&r1=1234158&r2=1234159&view=diff
==============================================================================
--- 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java
 (original)
+++ 
commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/shortestpath/AllVertexPairsShortestPath.java
 Fri Jan 20 21:44:53 2012
@@ -19,6 +19,8 @@ package org.apache.commons.graph.shortes
  * under the License.
  */
 
+import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+
 import java.util.HashMap;
 import java.util.Map;
 
@@ -59,18 +61,9 @@ public final class AllVertexPairsShortes
      */
     void addShortestPath( V source, V target, WeightedPath<V, WE, W> 
weightedPath )
     {
-        if ( source == null )
-        {
-            throw new IllegalArgumentException( "Impossible to add a shortest 
path from a null source" );
-        }
-        if ( target == null )
-        {
-            throw new IllegalArgumentException( "Impossible to add a shortest 
path to a null target" );
-        }
-        if ( weightedPath == null )
-        {
-            throw new IllegalArgumentException( "Impossible to add a null 
weightedPath path to a null target" );
-        }
+        source = checkNotNull( source, "Impossible to add a shortest path from 
a null source" );
+        target = checkNotNull( target, "Impossible to add a shortest path to a 
null target" );
+        weightedPath = checkNotNull( weightedPath, "Impossible to add a null 
weightedPath path to a null target" );
 
         paths.put( new VertexPair<V>( source, target ), weightedPath );
     }
@@ -84,14 +77,8 @@ public final class AllVertexPairsShortes
      */
     public WeightedPath<V, WE, W> findShortestPath( V source, V target )
     {
-        if ( source == null )
-        {
-            throw new IllegalArgumentException( "Impossible to find the 
shortest path from a null source" );
-        }
-        if ( target == null )
-        {
-            throw new IllegalArgumentException( "Impossible to find the 
shortest path to a null target" );
-        }
+        source = checkNotNull( source, "Impossible to add a shortest path from 
a null source" );
+        target = checkNotNull( target, "Impossible to add a shortest path to a 
null target" );
 
         WeightedPath<V, WE, W> path = paths.get( new VertexPair<V>( source, 
target ) );
 
@@ -110,18 +97,9 @@ public final class AllVertexPairsShortes
      */
     void addShortestDistance( V source, V target, W distance )
     {
-        if ( source == null )
-        {
-            throw new IllegalArgumentException( "Impossible to add a shortest 
distance with a null source" );
-        }
-        if ( target == null )
-        {
-            throw new IllegalArgumentException( "Impossible to add a shortest 
distance with a null target" );
-        }
-        if ( distance == null )
-        {
-            throw new IllegalArgumentException( "Impossible to add a shortest 
distance with a null distance" );
-        }
+        source = checkNotNull( source, "Impossible to add a shortest path from 
a null source" );
+        target = checkNotNull( target, "Impossible to add a shortest path to a 
null target" );
+        distance = checkNotNull( distance, "Impossible to add a shortest 
distance with a null distance" );
 
         shortestDistances.put( new VertexPair<V>( source, target ), distance );
     }
@@ -135,14 +113,8 @@ public final class AllVertexPairsShortes
      */
     W getShortestDistance( V source, V target )
     {
-        if ( source == null )
-        {
-            throw new IllegalArgumentException( "Impossible to get the 
shortest distance from a null source" );
-        }
-        if ( target == null )
-        {
-            throw new IllegalArgumentException( "Impossible to get the 
shortest distance to a null target" );
-        }
+        source = checkNotNull( source, "Impossible to add a shortest path from 
a null source" );
+        target = checkNotNull( target, "Impossible to add a shortest path to a 
null target" );
 
         if ( source.equals( target ) )
         {
@@ -161,14 +133,8 @@ public final class AllVertexPairsShortes
      */
     boolean hasShortestDistance( V source, V target )
     {
-        if ( source == null )
-        {
-            throw new IllegalArgumentException( "Impossible to get the 
shortest distance from a null source" );
-        }
-        if ( target == null )
-        {
-            throw new IllegalArgumentException( "Impossible to get the 
shortest distance to a null target" );
-        }
+        source = checkNotNull( source, "Impossible to add a shortest path from 
a null source" );
+        target = checkNotNull( target, "Impossible to add a shortest path to a 
null target" );
 
         if ( source.equals( target ) )
         {

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=1234159&r1=1234158&r2=1234159&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
 Fri Jan 20 21:44:53 2012
@@ -19,6 +19,8 @@ package org.apache.commons.graph.visit;
  * under the License.
  */
 
+import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -65,18 +67,9 @@ public final class Visit
     public static final <V extends Vertex, E extends Edge> void 
breadthFirstSearch( Graph<V, E> graph, V source,
                                                                                
        GraphVisitHandler<V, E> handler )
     {
-        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 )
-        {
-            throw new IllegalArgumentException( "Graph visitor handler can not 
be null." );
-        }
+        graph = checkNotNull( graph, "Graph has to be visited can not be 
null." );
+        source = checkNotNull( source, "Root node the search begins from can 
not be null." );
+        handler = checkNotNull( handler, "Graph visitor handler can not be 
null." );
 
         handler.discoverGraph( graph );
 
@@ -87,16 +80,16 @@ public final class Visit
         visitedVertices.add( source );
 
         boolean visitingGraph = true;
-        
+
         while ( visitingGraph && !vertexQueue.isEmpty() )
         {
             V v = vertexQueue.remove();
 
             if ( handler.discoverVertex( v ) ) {
-                
+
                 Iterator<V> connected = ( graph instanceof DirectedGraph ) ? ( 
(DirectedGraph<V, E>) graph ).getOutbound( v ).iterator()
                                 : graph.getConnectedVertices( v ).iterator();
-                
+
                 while ( connected.hasNext() )
                 {
                     V w = connected.next();
@@ -177,7 +170,7 @@ public final class Visit
         visitedVertices.add( source );
 
         boolean visitingGraph = true;
-        
+
         while ( visitingGraph && !vertexStack.isEmpty() )
         {
             V v = vertexStack.pop();
@@ -186,7 +179,7 @@ public final class Visit
             {
                 Iterator<V> connected = ( graph instanceof DirectedGraph ) ? ( 
(DirectedGraph<V, E>) graph ).getOutbound( v ).iterator()
                                 : graph.getConnectedVertices( v ).iterator();
-                
+
                 while ( connected.hasNext() )
                 {
                     V w = connected.next();


Reply via email to