diff --git a/src/main/java/org/elasticsearch/indices/recovery/RecoveryState.java b/src/main/java/org/elasticsearch/indices/recovery/RecoveryState.java index a51d779b41b..3e6e5c47828 100644 --- a/src/main/java/org/elasticsearch/indices/recovery/RecoveryState.java +++ b/src/main/java/org/elasticsearch/indices/recovery/RecoveryState.java @@ -655,6 +655,15 @@ public class RecoveryState implements ToXContent, Streamable { return false; } + @Override + public int hashCode() { + int result = name.hashCode(); + result = 31 * result + (int) (length ^ (length >>> 32)); + result = 31 * result + (int) (recovered ^ (recovered >>> 32)); + result = 31 * result + (reused ? 1 : 0); + return result; + } + @Override public String toString() { return "file (name [" + name + "], reused [" + reused + "], length [" + length + "], recovered [" + recovered + "])"; diff --git a/src/test/java/org/elasticsearch/indices/recovery/RecoveryStateTest.java b/src/test/java/org/elasticsearch/indices/recovery/RecoveryStateTest.java index ad57f0d8c9a..91850bb6b9f 100644 --- a/src/test/java/org/elasticsearch/indices/recovery/RecoveryStateTest.java +++ b/src/test/java/org/elasticsearch/indices/recovery/RecoveryStateTest.java @@ -507,4 +507,23 @@ public class RecoveryStateTest extends ElasticsearchTestCase { readWriteIndex.join(); assertThat(readWriteIndex.error.get(), equalTo(null)); } + + @Test + public void testFileHashCodeAndEquals() { + File f = new File("foo", randomIntBetween(0, 100), randomBoolean()); + File anotherFile = new File(f.name(), f.length(), f.reused()); + assertEquals(f, anotherFile); + assertEquals(f.hashCode(), anotherFile.hashCode()); + int iters = randomIntBetween(10, 100); + for (int i = 0; i < iters; i++) { + f = new File("foo", randomIntBetween(0, 100), randomBoolean()); + anotherFile = new File(f.name(), randomIntBetween(0, 100), randomBoolean()); + if (f.equals(anotherFile)) { + assertEquals(f.hashCode(), anotherFile.hashCode()); + } else if (f.hashCode() != anotherFile.hashCode()) { + assertFalse(f.equals(anotherFile)); + } + } + + } }