You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/09/16 20:06:10 UTC

groovy git commit: Simplify hashCode of Tuple

Repository: groovy
Updated Branches:
  refs/heads/master de56fe3c7 -> 6a8518dbf


Simplify hashCode of Tuple


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

Branch: refs/heads/master
Commit: 6a8518dbf78575adc5e9602614b9a739e0e3d086
Parents: de56fe3
Author: sunlan <su...@apache.org>
Authored: Sun Sep 17 04:06:00 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sun Sep 17 04:06:00 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/6a8518db/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);
     }
 }