diff --git a/lucene/core/src/java/org/apache/lucene/util/Version.java b/lucene/core/src/java/org/apache/lucene/util/Version.java index 3f1293672ed..5afa49ed38a 100644 --- a/lucene/core/src/java/org/apache/lucene/util/Version.java +++ b/lucene/core/src/java/org/apache/lucene/util/Version.java @@ -349,4 +349,9 @@ public final class Version { public boolean equals(Object o) { return o != null && o instanceof Version && ((Version)o).encodedValue == encodedValue; } + + @Override + public int hashCode() { + return encodedValue; + } } diff --git a/lucene/core/src/test/org/apache/lucene/util/TestVersion.java b/lucene/core/src/test/org/apache/lucene/util/TestVersion.java index 8e15efe040e..48c3709680f 100644 --- a/lucene/core/src/test/org/apache/lucene/util/TestVersion.java +++ b/lucene/core/src/test/org/apache/lucene/util/TestVersion.java @@ -19,6 +19,7 @@ package org.apache.lucene.util; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.Random; public class TestVersion extends LuceneTestCase { @@ -186,4 +187,23 @@ public class TestVersion extends LuceneTestCase { assertEquals("Version.LATEST does not match the one given in common-build.xml", Version.LATEST.toString(), commonBuildVersion); } + + public void testEqualsHashCode() { + Random random = random(); + String version = "" + (4 + random.nextInt(1)) + "." + random.nextInt(10) + "." + random.nextInt(10); + Version v1 = Version.parseLeniently(version); + Version v2 = Version.parseLeniently(version); + assertEquals(v1.hashCode(), v2.hashCode()); + assertEquals(v1, v2); + final int iters = 10 + random.nextInt(20); + for (int i = 0; i < iters; i++) { + String v = "" + (4 + random.nextInt(1)) + "." + random.nextInt(10) + "." + random.nextInt(10); + if (v.equals(version)) { + assertEquals(Version.parseLeniently(v).hashCode(), v1.hashCode()); + assertEquals(Version.parseLeniently(v), v1); + } else { + assertFalse(Version.parseLeniently(v).equals(v1)); + } + } + } }