You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by jw...@apache.org on 2016/08/23 02:15:59 UTC
[4/5] groovy git commit: GROOVY-7834: Calculating hashcode of
IntRange by pairing function (closes #388)
GROOVY-7834: Calculating hashcode of IntRange by pairing function (closes #388)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/c2da84b2
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c2da84b2
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c2da84b2
Branch: refs/heads/master
Commit: c2da84b2ac05b478fee41639848489a94d8db179
Parents: 454158d
Author: Anand upadhyay <an...@nielsen.com>
Authored: Fri Aug 19 17:13:08 2016 +0530
Committer: John Wagenleitner <jw...@apache.org>
Committed: Mon Aug 22 19:11:19 2016 -0700
----------------------------------------------------------------------
src/main/groovy/lang/IntRange.java | 10 ++++++++++
src/test/groovy/lang/IntRangeTest.groovy | 11 +++++++++++
2 files changed, 21 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/c2da84b2/src/main/groovy/lang/IntRange.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/IntRange.java b/src/main/groovy/lang/IntRange.java
index 3855f43..8493d1f 100644
--- a/src/main/groovy/lang/IntRange.java
+++ b/src/main/groovy/lang/IntRange.java
@@ -415,4 +415,14 @@ public class IntRange extends AbstractList<Integer> implements Range<Integer> {
step(step, adapter);
return adapter.asList();
}
+
+ @Override
+ public int hashCode(){
+ int hashCode;
+ final int from = this.getFrom();
+ final int to = this.getTo();
+
+ hashCode = new Integer(((from+to+1)*(from+to))/2).intValue()+to;
+ return hashCode;
+ }
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/c2da84b2/src/test/groovy/lang/IntRangeTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/lang/IntRangeTest.groovy b/src/test/groovy/lang/IntRangeTest.groovy
index 73c5a5e..c1afa76 100644
--- a/src/test/groovy/lang/IntRangeTest.groovy
+++ b/src/test/groovy/lang/IntRangeTest.groovy
@@ -138,4 +138,15 @@ class IntRangeTest extends GroovyTestCase {
assert bs[-1..<-7].toString() == '{0, 5}'
assert bs[20..<-8].toString() == '{2, 3}'
}
+
+ void testHashCode(){
+ def maxRange = new IntRange(1,Integer.MAX_VALUE)
+ def rangeWithName = [:]
+ rangeWithName.put(maxRange, "maxRange")
+ def dupRange = new IntRange(1,Integer.MAX_VALUE)
+ assertEquals(rangeWithName.get(dupRange), "maxRange")
+ }
+
+
+
}