You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by no...@apache.org on 2010/09/01 15:03:31 UTC

svn commit: r991519 - in /pivot/trunk: core/src/org/apache/pivot/beans/ core/src/org/apache/pivot/collections/ core/src/org/apache/pivot/util/ core/src/org/apache/pivot/xml/ wtk/src/org/apache/pivot/wtk/

Author: noelgrandin
Date: Wed Sep  1 13:03:30 2010
New Revision: 991519

URL: http://svn.apache.org/viewvc?rev=991519&view=rev
Log:
improve the hashCode() implementations

Modified:
    pivot/trunk/core/src/org/apache/pivot/beans/NamespaceBinding.java
    pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java
    pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java
    pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java
    pivot/trunk/core/src/org/apache/pivot/util/Time.java
    pivot/trunk/core/src/org/apache/pivot/xml/Element.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Point.java

Modified: pivot/trunk/core/src/org/apache/pivot/beans/NamespaceBinding.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/NamespaceBinding.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/NamespaceBinding.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/NamespaceBinding.java Wed Sep  1 13:03:30 2010
@@ -260,7 +260,12 @@ public class NamespaceBinding {
 
     @Override
     public int hashCode() {
-        return (source.hashCode() * sourceKey.hashCode()
-            + target.hashCode() * targetKey.hashCode());
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + source.hashCode();
+        result = prime * result + sourceKey.hashCode();
+        result = prime * result + target.hashCode();
+        result = prime * result + targetKey.hashCode();
+        return result;
     }
 }

Modified: pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java Wed Sep  1 13:03:30 2010
@@ -459,10 +459,10 @@ public class HashMap<K, V> implements Ma
 
     @Override
     public int hashCode() {
-        int hashCode = 0;
+        int hashCode = 1;
 
         for (K key : this) {
-            hashCode += key.hashCode();
+            hashCode = 31 * hashCode + key.hashCode();
         }
 
         return hashCode;

Modified: pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java Wed Sep  1 13:03:30 2010
@@ -213,10 +213,10 @@ public class HashSet<E> implements Set<E
 
     @Override
     public int hashCode() {
-        int hashCode = 0;
+        int hashCode = 1;
 
         for (E element : this) {
-            hashCode += element.hashCode();
+            hashCode = 31 * hashCode + element.hashCode();
         }
 
         return hashCode;

Modified: pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/CalendarDate.java Wed Sep  1 13:03:30 2010
@@ -398,8 +398,12 @@ public final class CalendarDate implemen
      */
     @Override
     public int hashCode() {
-        Integer hashKey = year + month + day;
-        return hashKey.hashCode();
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + year;
+        result = prime * result + month;
+        result = prime * result + day;
+        return result;
     }
 
     /**

Modified: pivot/trunk/core/src/org/apache/pivot/util/Time.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/Time.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/Time.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/Time.java Wed Sep  1 13:03:30 2010
@@ -345,8 +345,13 @@ public final class Time implements Compa
 
     @Override
     public int hashCode() {
-        Integer hashKey = hour + minute + second + millisecond;
-        return hashKey.hashCode();
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + hour;
+        result = prime * result + minute;
+        result = prime * result + second;
+        result = prime * result + millisecond;
+        return result;
     }
 
     @Override

Modified: pivot/trunk/core/src/org/apache/pivot/xml/Element.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/xml/Element.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/xml/Element.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/xml/Element.java Wed Sep  1 13:03:30 2010
@@ -148,14 +148,13 @@ public class Element extends Node implem
 
         @Override
         public int hashCode() {
-            int hashCode = 0;
+            final int prime = 31;
+            int result = 1;
             if (namespacePrefix != null) {
-                hashCode += namespacePrefix.hashCode();
+                result = 31 * result + namespacePrefix.hashCode();
             }
-
-            hashCode += localName.hashCode() + value.hashCode();
-
-            return hashCode;
+            result = prime * result + localName.hashCode();
+            return result;
         }
 
         @Override
@@ -981,14 +980,15 @@ public class Element extends Node implem
 
     @Override
     public int hashCode() {
-        int hashCode = 0;
+        final int prime = 31;
+        int result = 1;
         if (namespacePrefix != null) {
-            hashCode += namespacePrefix.hashCode();
+            result = 31 * result + namespacePrefix.hashCode();
         }
-
-        hashCode += localName.hashCode() + attributes.hashCode() + nodes.hashCode();
-
-        return hashCode;
+        result = prime * result + localName.hashCode();
+        result = prime * result + attributes.hashCode();
+        result = prime * result + nodes.hashCode();
+        return result;
     }
 
     @Override

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java Wed Sep  1 13:03:30 2010
@@ -224,10 +224,16 @@ public final class Bounds implements Ser
 
     @Override
     public int hashCode() {
-        // TODO This may not be the most optimal hashing function
-        return (x * y) ^ (width * height);
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + height;
+        result = prime * result + width;
+        result = prime * result + x;
+        result = prime * result + y;
+        return result;
     }
 
+
     public java.awt.Rectangle toRectangle() {
         return new java.awt.Rectangle(x, y, width, height);
     }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/CornerRadii.java Wed Sep  1 13:03:30 2010
@@ -160,8 +160,13 @@ public final class CornerRadii implement
 
     @Override
     public int hashCode() {
-        // TODO This may not be the most optimal hashing function
-        return (topLeft * topRight) ^ (bottomLeft * bottomRight);
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + topLeft;
+        result = prime * result + topRight;
+        result = prime * result + bottomLeft;
+        result = prime * result + bottomRight;
+        return result;
     }
 
     @Override

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java Wed Sep  1 13:03:30 2010
@@ -81,8 +81,7 @@ public final class Dimensions implements
 
     @Override
     public int hashCode() {
-        // TODO This may not be the most optimal hashing function
-        return width * height;
+        return 31 * width + height;
     }
 
     @Override

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java Wed Sep  1 13:03:30 2010
@@ -112,8 +112,13 @@ public final class Insets implements Ser
 
     @Override
     public int hashCode() {
-        // TODO This may not be the most optimal hashing function
-        return (top * left) ^ (bottom * right);
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + top;
+        result = prime * result + left;
+        result = prime * result + bottom;
+        result = prime * result + right;
+        return result;
     }
 
     @Override

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Point.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Point.java?rev=991519&r1=991518&r2=991519&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Point.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Point.java Wed Sep  1 13:03:30 2010
@@ -85,8 +85,7 @@ public final class Point implements Seri
 
     @Override
     public int hashCode() {
-        // TODO This may not be the most optimal hashing function
-        return x * y;
+        return 31 * x  + y;
     }
 
     @Override