groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject [4/5] groovy git commit: GROOVY-7834: Calculating hashcode of IntRange by pairing function (closes #388)
Date Tue, 23 Aug 2016 02:15:59 GMT
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 <anand.upadhyay.consultant@nielsen.com>
Authored: Fri Aug 19 17:13:08 2016 +0530
Committer: John Wagenleitner <jwagenleitner@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")
+    }
+
+
+
 }


Mime
View raw message