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/04/21 21:38:40 UTC

svn commit: r1328729 - in /commons/sandbox/graph/trunk/src: main/java/org/apache/commons/graph/ main/java/org/apache/commons/graph/model/ main/java/org/apache/commons/graph/utils/ test/java/org/apache/commons/graph/model/

Author: simonetripodi
Date: Sat Apr 21 19:38:39 2012
New Revision: 1328729

URL: http://svn.apache.org/viewvc?rev=1328729&view=rev
Log:
eliminate equals/hashcode boilerplate code with simply utility methods

Added:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/utils/Objects.java   (with props)
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/model/BaseGraph.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/model/InMemoryWeightedPath.java
    commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java
    commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java
    commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.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=1328729&r1=1328728&r2=1328729&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 Sat Apr 21 19:38:39 2012
@@ -21,6 +21,8 @@ package org.apache.commons.graph;
 
 import static java.lang.String.format;
 import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+import static org.apache.commons.graph.utils.Objects.eq;
+import static org.apache.commons.graph.utils.Objects.hash;
 
 import java.io.Serializable;
 
@@ -73,11 +75,7 @@ public final class VertexPair<V> impleme
     @Override
     public int hashCode()
     {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + ( ( head == null ) ? 0 : head.hashCode() );
-        result = prime * result + ( ( tail == null ) ? 0 : tail.hashCode() );
-        return result;
+        return hash( 1, 31, head, tail );
     }
 
     /**
@@ -91,29 +89,15 @@ public final class VertexPair<V> impleme
             return true;
         }
 
-        if ( obj == null )
-        {
-            return false;
-        }
-
-        if ( getClass() != obj.getClass() )
+        if ( obj == null || getClass() != obj.getClass() )
         {
             return false;
         }
 
         @SuppressWarnings( "unchecked" ) // equals() invoked against only same VertexPair type
         VertexPair<V> other = (VertexPair<V>) obj;
-        if ( !head.equals( other.getHead() ) )
-        {
-            return false;
-        }
-
-        if ( !tail.equals( other.getTail() ) )
-        {
-            return false;
-        }
-
-        return true;
+        return eq( head, other.getHead() )
+            && eq( tail, other.getTail() );
     }
 
     @Override

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=1328729&r1=1328728&r2=1328729&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 Sat Apr 21 19:38:39 2012
@@ -19,6 +19,7 @@ package org.apache.commons.graph.model;
  * under the License.
  */
 
+import static org.apache.commons.graph.utils.Objects.*;
 import static java.lang.String.format;
 import static java.util.Collections.unmodifiableCollection;
 import static java.util.Collections.unmodifiableSet;
@@ -153,12 +154,7 @@ public abstract class BaseGraph<V, E>
             return true;
         }
 
-        if ( obj == null )
-        {
-            return false;
-        }
-
-        if ( getClass() != obj.getClass() )
+        if ( obj == null || getClass() != obj.getClass() )
         {
             return false;
         }
@@ -166,11 +162,7 @@ public abstract class BaseGraph<V, E>
         @SuppressWarnings( "unchecked" )
         // test against any Graph typed instance
         BaseGraph<Object, Object> other = (BaseGraph<Object, Object>) obj;
-        if ( !adjacencyList.equals( other.getAdjacencyList() ) )
-        {
-            return false;
-        }
-        return true;
+        return eq( adjacencyList, other.getAdjacencyList() );
     }
 
     /**

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=1328729&r1=1328728&r2=1328729&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 Sat Apr 21 19:38:39 2012
@@ -24,6 +24,8 @@ import static java.util.Arrays.asList;
 import static java.util.Collections.unmodifiableList;
 import static org.apache.commons.graph.utils.Assertions.checkArgument;
 import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+import static org.apache.commons.graph.utils.Objects.eq;
+import static org.apache.commons.graph.utils.Objects.hash;
 
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -224,13 +226,7 @@ public class InMemoryPath<V, E>
     @Override
     public int hashCode()
     {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + ( ( edges == null ) ? 0 : edges.hashCode() );
-        result = prime * result + ( ( source == null ) ? 0 : source.hashCode() );
-        result = prime * result + ( ( target == null ) ? 0 : target.hashCode() );
-        result = prime * result + ( ( vertices == null ) ? 0 : vertices.hashCode() );
-        return result;
+        return hash( 1, 31, edges, source, target, vertices );
     }
 
     /**
@@ -244,39 +240,17 @@ public class InMemoryPath<V, E>
             return true;
         }
 
-        if ( obj == null )
-        {
-            return false;
-        }
-
-        if ( getClass() != obj.getClass() )
+        if ( obj == null || getClass() != obj.getClass() )
         {
             return false;
         }
 
         @SuppressWarnings( "unchecked" ) // test against any Path typed instance
         InMemoryPath<Object, Object> other = (InMemoryPath<Object, Object>) obj;
-        if ( !source.equals( other.getSource() ) )
-        {
-            return false;
-        }
-
-        if ( !target.equals( other.getTarget() ) )
-        {
-            return false;
-        }
-
-        if ( !vertices.equals( other.getVertices() ) )
-        {
-            return false;
-        }
-
-        if ( !edges.equals( other.getEdges() ) )
-        {
-            return false;
-        }
-
-        return true;
+        return eq( source, other.getSource() )
+            && eq( target, other.getTarget() )
+            && eq( vertices, other.getVertices() )
+            && eq( edges, other.getEdges() );
     }
 
     /**

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java?rev=1328729&r1=1328728&r2=1328729&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/model/InMemoryWeightedPath.java Sat Apr 21 19:38:39 2012
@@ -20,6 +20,7 @@ package org.apache.commons.graph.model;
  */
 
 import static java.lang.String.format;
+import static org.apache.commons.graph.utils.Objects.eq;
 
 import org.apache.commons.graph.Mapper;
 import org.apache.commons.graph.WeightedPath;
@@ -131,11 +132,7 @@ public final class InMemoryWeightedPath<
 
         @SuppressWarnings( "unchecked" ) // test against any WeightedPath typed instance
         InMemoryWeightedPath<Object, Object, W> other = (InMemoryWeightedPath<Object, Object, W>) obj;
-        if ( !weight.equals( other.getWeight() ) )
-        {
-            return false;
-        }
-        return true;
+        return eq( weight, other.getWeight() );
     }
 
     /**

Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/utils/Objects.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/utils/Objects.java?rev=1328729&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/utils/Objects.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/utils/Objects.java Sat Apr 21 19:38:39 2012
@@ -0,0 +1,65 @@
+package org.apache.commons.graph.utils;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Object simple utility methods.
+ */
+public final class Objects
+{
+
+    /**
+     * Hidden constructor, this class must not be instantiated.
+     */
+    private Objects()
+    {
+        // do nothing
+    }
+
+    /**
+     * Computes a hashCode given the input objects.
+     *
+     * @param initialNonZeroOddNumber a non-zero, odd number used as the initial value.
+     * @param multiplierNonZeroOddNumber a non-zero, odd number used as the multiplier.
+     * @param objs the objects to compute hash code.
+     * @return the computed hashCode.
+     */
+    public static int hash( int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object...objs )
+    {
+        int result = initialNonZeroOddNumber;
+        for ( Object obj : objs )
+        {
+            result = multiplierNonZeroOddNumber * result + ( obj != null ? obj.hashCode() : 0 );
+        }
+        return result;
+    }
+
+    /**
+     * Verifies input objects are equal.
+     *
+     * @param o1 the first argument to compare
+     * @param o2 the second argument to compare
+     * @return true, if the input arguments are equal, false otherwise.
+     */
+    public static <O> boolean eq( O o1, O o2 )
+    {
+        return o1 != null ? o1.equals( o2 ) : o2 == null;
+    }
+
+}

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/utils/Objects.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/utils/Objects.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/utils/Objects.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java?rev=1328729&r1=1328728&r2=1328729&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java (original)
+++ commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledEdge.java Sat Apr 21 19:38:39 2012
@@ -21,6 +21,8 @@ package org.apache.commons.graph.model;
 
 import static java.lang.String.format;
 import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+import static org.apache.commons.graph.utils.Objects.eq;
+import static org.apache.commons.graph.utils.Objects.hash;
 
 import java.io.Serializable;
 
@@ -49,10 +51,7 @@ public class BaseLabeledEdge implements 
     @Override
     public int hashCode()
     {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + label.hashCode();
-        return result;
+        return hash( 1, 31, label );
     }
 
     /**
@@ -66,24 +65,14 @@ public class BaseLabeledEdge implements 
             return true;
         }
 
-        if ( obj == null )
-        {
-            return false;
-        }
-
-        if ( getClass() != obj.getClass() )
+        if ( obj == null || getClass() != obj.getClass() )
         {
             return false;
         }
 
         BaseLabeledEdge other = (BaseLabeledEdge) obj;
 
-        if ( !label.equals( other.label ) )
-        {
-            return false;
-        }
-
-        return true;
+        return eq( label, other.label );
     }
 
     /**

Modified: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java?rev=1328729&r1=1328728&r2=1328729&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java (original)
+++ commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledVertex.java Sat Apr 21 19:38:39 2012
@@ -21,6 +21,8 @@ package org.apache.commons.graph.model;
 
 import static java.lang.String.format;
 import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+import static org.apache.commons.graph.utils.Objects.eq;
+import static org.apache.commons.graph.utils.Objects.hash;
 
 import java.io.Serializable;
 
@@ -49,10 +51,7 @@ public class BaseLabeledVertex implement
     @Override
     public int hashCode()
     {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + label.hashCode();
-        return result;
+        return hash( 1, 31, label );
     }
 
     /**
@@ -66,23 +65,13 @@ public class BaseLabeledVertex implement
             return true;
         }
 
-        if ( obj == null )
-        {
-            return false;
-        }
-
-        if ( getClass() != obj.getClass() )
+        if ( obj == null || getClass() != obj.getClass() )
         {
             return false;
         }
 
         BaseLabeledVertex other = (BaseLabeledVertex) obj;
-        if ( !label.equals( other.getLabel() ) )
-        {
-            return false;
-        }
-
-        return true;
+        return eq( label, other.getLabel() );
     }
 
     /**

Modified: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.java?rev=1328729&r1=1328728&r2=1328729&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.java (original)
+++ commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/model/BaseLabeledWeightedEdge.java Sat Apr 21 19:38:39 2012
@@ -21,6 +21,8 @@ package org.apache.commons.graph.model;
 
 import static java.lang.String.format;
 import static org.apache.commons.graph.utils.Assertions.checkNotNull;
+import static org.apache.commons.graph.utils.Objects.eq;
+import static org.apache.commons.graph.utils.Objects.hash;
 
 /**
  *
@@ -53,10 +55,7 @@ public class BaseLabeledWeightedEdge<W>
     @Override
     public int hashCode()
     {
-        final int prime = 31;
-        int result = super.hashCode();
-        result = prime * result + weight.hashCode();
-        return result;
+        return hash( super.hashCode(), 31, weight );
     }
 
     /**
@@ -81,12 +80,7 @@ public class BaseLabeledWeightedEdge<W>
         }
         @SuppressWarnings( "unchecked" )
         BaseLabeledWeightedEdge<W> other = (BaseLabeledWeightedEdge<W>) obj;
-        if ( !weight.equals( other.getWeight() ) )
-        {
-            return false;
-        }
-
-        return true;
+        return eq( weight, other.getWeight() );
     }
 
     /**