groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Simplify hashCode of Tuple
Date Sat, 16 Sep 2017 20:15:05 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 6d3060d45 -> 46f0416f6


Simplify hashCode of Tuple

(cherry picked from commit 6a8518d)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/46f0416f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/46f0416f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/46f0416f

Branch: refs/heads/GROOVY_2_5_X
Commit: 46f0416f6027c67c6c324a0dd366e14371eb7d4b
Parents: 6d3060d
Author: sunlan <sunlan@apache.org>
Authored: Sun Sep 17 04:06:00 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Sun Sep 17 04:14:57 2017 +0800

----------------------------------------------------------------------
 src/main/groovy/lang/Tuple.java | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/46f0416f/src/main/groovy/lang/Tuple.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/Tuple.java b/src/main/groovy/lang/Tuple.java
index 06d501a..aef53f6 100644
--- a/src/main/groovy/lang/Tuple.java
+++ b/src/main/groovy/lang/Tuple.java
@@ -22,6 +22,7 @@ import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 
 import java.util.AbstractList;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * Represents a list of Objects.
@@ -30,7 +31,6 @@ import java.util.List;
  */
 public class Tuple<E> extends AbstractList<E> {
     private final E[] contents;
-    private int hashCode;
 
     public Tuple(E... contents) {
         if (contents == null) throw new NullPointerException();
@@ -47,6 +47,7 @@ public class Tuple<E> extends AbstractList<E> {
         return contents.length;
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public List<E> subList(int fromIndex, int toIndex) {
         int size = toIndex - fromIndex;
@@ -65,8 +66,9 @@ public class Tuple<E> extends AbstractList<E> {
         if (o == null || !(o instanceof Tuple)) return false;
 
         Tuple that = (Tuple) o;
-        if (size() != that.size()) return false;
-        for (int i = 0; i < size(); i++) {
+        int size = size();
+        if (size != that.size()) return false;
+        for (int i = 0; i < size; i++) {
             if (!DefaultTypeTransformation.compareEqual(get(i), that.get(i))) {
                 return false;
             }
@@ -76,16 +78,6 @@ public class Tuple<E> extends AbstractList<E> {
 
     @Override
     public int hashCode() {
-        if (hashCode == 0) {
-            for (int i = 0; i < size(); i++) {
-                Object value = get(i);
-                int hash = (value != null) ? value.hashCode() : 0xbabe;
-                hashCode ^= hash;
-            }
-            if (hashCode == 0) {
-                hashCode = 0xbabe;
-            }
-        }
-        return hashCode;
+        return Objects.hash(contents);
     }
 }


Mime
View raw message