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 2012/02/16 11:08:05 UTC

svn commit: r1244913 - in /commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model: BaseGraph.java BaseMutableGraph.java

Author: simonetripodi
Date: Thu Feb 16 10:08:04 2012
New Revision: 1244913

URL: http://svn.apache.org/viewvc?rev=1244913&view=rev
Log:
added an internal utility method according to the DRY principle

Modified:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java?rev=1244913&r1=1244912&r2=1244913&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseGraph.java Thu Feb 16 10:08:04 2012
@@ -19,6 +19,7 @@ package org.apache.commons.graph.model;
  * under the License.
  */
 
+import static java.lang.String.format;
 import static java.util.Collections.unmodifiableCollection;
 import static java.util.Collections.unmodifiableSet;
 
@@ -92,10 +93,9 @@ public abstract class BaseGraph<V extend
     public final Iterable<V> getConnectedVertices( V v )
     {
         final Set<V> adj = adjacencyList.get( v );
-        if ( adj == null )
-        {
-            throw new GraphException( "Vertex %s doesn't exist in the Graph", v );
-        }
+
+        checkGraphCondition( adj != null, "Vertex %s does not exist in the Graph", v );
+
         return unmodifiableSet( adj );
     }
 
@@ -104,14 +104,9 @@ public abstract class BaseGraph<V extend
      */
     public final E getEdge( V source, V target )
     {
-        if ( !adjacencyList.containsKey( source ) )
-        {
-            throw new GraphException( "Vertex %s doesn't exist in the Graph", source );
-        }
-        if ( !adjacencyList.containsKey( target ) )
-        {
-            throw new GraphException( "Vertex %s doesn't exist in the Graph", target );
-        }
+        checkGraphCondition( adjacencyList.containsKey( source ), "Vertex %s does not exist in the Graph", source );
+        checkGraphCondition( adjacencyList.containsKey( target ), "Vertex %s does not exist in the Graph", target );
+
         return indexedEdges.get( new VertexPair<Vertex>( source, target ) );
     }
 
@@ -197,4 +192,12 @@ public abstract class BaseGraph<V extend
         return indexedVertices;
     }
 
+    protected static void checkGraphCondition( boolean expression, String errorMessageTemplate, Object...errorMessageArgs )
+    {
+        if ( !expression )
+        {
+            throw new GraphException( format( errorMessageTemplate, errorMessageArgs ) );
+        }
+    }
+
 }

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java?rev=1244913&r1=1244912&r2=1244913&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/BaseMutableGraph.java Thu Feb 16 10:08:04 2012
@@ -23,7 +23,6 @@ import java.util.LinkedHashSet;
 
 import org.apache.commons.graph.Edge;
 import org.apache.commons.graph.Graph;
-import org.apache.commons.graph.GraphException;
 import org.apache.commons.graph.MutableGraph;
 import org.apache.commons.graph.Vertex;
 import org.apache.commons.graph.VertexPair;
@@ -46,15 +45,8 @@ public abstract class BaseMutableGraph<V
      */
     public final void addVertex( V v )
     {
-        if ( v == null )
-        {
-            throw new GraphException( "Impossible to add a null Vertex to the Graph" );
-        }
-
-        if ( getAdjacencyList().containsKey( v ) )
-        {
-            throw new GraphException( "Vertex '%s' already present in the Graph", v );
-        }
+        checkGraphCondition( v != null, "Impossible to add a null Vertex to the Graph" );
+        checkGraphCondition( !getAdjacencyList().containsKey( v ), "Vertex '%s' already present in the Graph", v );
 
         getAdjacencyList().put( v, new LinkedHashSet<V>() );
 
@@ -71,14 +63,8 @@ public abstract class BaseMutableGraph<V
      */
     public final void removeVertex( V v )
     {
-        if ( v == null )
-        {
-            throw new GraphException( "Impossible to remove a null Vertex from the Graph" );
-        }
-
-        if ( !getAdjacencyList().containsKey( v ) ){
-            throw new GraphException( "Vertex '%s' not present in the Graph", v );
-        }
+        checkGraphCondition( v != null, "Impossible to remove a null Vertex from the Graph" );
+        checkGraphCondition( getAdjacencyList().containsKey( v ), "Vertex '%s' not present in the Graph", v );
 
         for ( V tail : getAdjacencyList().get( v ) )
         {
@@ -101,32 +87,12 @@ public abstract class BaseMutableGraph<V
      */
     public void addEdge( V head, E e, V tail )
     {
-        if ( head == null )
-        {
-            throw new GraphException( "Null head Vertex not admitted" );
-        }
-        if ( e == null )
-        {
-            throw new GraphException( "Impossible to add a null Edge in the Graph" );
-        }
-        if ( tail == null )
-        {
-            throw new GraphException( "Null tail Vertex not admitted" );
-        }
-
-        if ( !getAdjacencyList().containsKey( head ) )
-        {
-            throw new GraphException( "Head Vertex '%s' not present in the Graph", head );
-        }
-        if ( !getAdjacencyList().containsKey( tail ) )
-        {
-            throw new GraphException( "Tail Vertex '%s' not present in the Graph", tail );
-        }
-
-        if ( getEdge( head, tail ) != null )
-        {
-            throw new GraphException( "Edge %s is already present in the Graph", e );
-        }
+        checkGraphCondition( head != null, "Null head Vertex not admitted" );
+        checkGraphCondition( e != null, "Impossible to add a null Edge in the Graph" );
+        checkGraphCondition( tail != null, "Null tail Vertex not admitted" );
+        checkGraphCondition( getAdjacencyList().containsKey( head ), "Head Vertex '%s' not present in the Graph", head );
+        checkGraphCondition( getAdjacencyList().containsKey( tail ), "Head Vertex '%s' not present in the Graph", tail );
+        checkGraphCondition( getEdge( head, tail ) == null, "Edge %s is already present in the Graph", e );
 
         getAllEdges().add( e );
 
@@ -160,10 +126,7 @@ public abstract class BaseMutableGraph<V
      */
     public final void removeEdge( E e )
     {
-        if ( e == null )
-        {
-            throw new GraphException( "Impossible to add a null Edge in the Graph" );
-        }
+        checkGraphCondition( e != null, "Impossible to remove a null Edge from the Graph" );
 
         // TODO to be completed