Add missing hashCode method to RecoveryState#File

This commit is contained in:
Simon Willnauer 2015-04-09 11:05:56 +02:00
parent abc7de96ae
commit 3b41299273
2 changed files with 28 additions and 0 deletions

View File

@ -655,6 +655,15 @@ public class RecoveryState implements ToXContent, Streamable {
return false; 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 @Override
public String toString() { public String toString() {
return "file (name [" + name + "], reused [" + reused + "], length [" + length + "], recovered [" + recovered + "])"; return "file (name [" + name + "], reused [" + reused + "], length [" + length + "], recovered [" + recovered + "])";

View File

@ -507,4 +507,23 @@ public class RecoveryStateTest extends ElasticsearchTestCase {
readWriteIndex.join(); readWriteIndex.join();
assertThat(readWriteIndex.error.get(), equalTo(null)); 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));
}
}
}
} }