groovy git commit: Simplify hashCode of Tuple
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: sunlanAuthored: Sun Sep 17 04:06:00 2017 +0800 Committer: sunlan 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 extends AbstractList { private final E[] contents; -private int hashCode; public Tuple(E... contents) { if (contents == null) throw new NullPointerException(); @@ -47,6 +47,7 @@ public class Tuple extends AbstractList { return contents.length; } +@SuppressWarnings("unchecked") @Override public List subList(int fromIndex, int toIndex) { int size = toIndex - fromIndex; @@ -65,8 +66,9 @@ public class Tuple extends AbstractList { 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 extends AbstractList { @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); } }
groovy git commit: Simplify hashCode of Tuple
Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X bcf47ad3a -> 995479cd5 Simplify hashCode of Tuple (cherry picked from commit 6a8518dbf78575adc5e9602614b9a739e0e3d086) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/995479cd Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/995479cd Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/995479cd Branch: refs/heads/GROOVY_2_6_X Commit: 995479cd575f8186e8855c1a6c6c324949afafd1 Parents: bcf47ad Author: sunlanAuthored: Sun Sep 17 04:10:08 2017 +0800 Committer: sunlan Committed: Sun Sep 17 04:10:08 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/995479cd/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 extends AbstractList { private final E[] contents; -private int hashCode; public Tuple(E... contents) { if (contents == null) throw new NullPointerException(); @@ -47,6 +47,7 @@ public class Tuple extends AbstractList { return contents.length; } +@SuppressWarnings("unchecked") @Override public List subList(int fromIndex, int toIndex) { int size = toIndex - fromIndex; @@ -65,8 +66,9 @@ public class Tuple extends AbstractList { 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 extends AbstractList { @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); } }