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: sunlan <sun...@apache.org> Authored: Sun Sep 17 04:10:08 2017 +0800 Committer: sunlan <sun...@apache.org> 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<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); } }