Add missing hashCode() implementation

Summary:
Whenever a Java class implements equals(), it has to implement hashCode(), otherwise
there might be weird behavior when inserting instances of the class in a hash map for
example. This adds two missing hashCode() implementations and extends tests to test
the hashCode() implementations.

Test Plan: make jtest

Reviewers: rven, igor, sdong, yhchiang

Reviewed By: yhchiang

Subscribers: anthony, dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D43017
This commit is contained in:
Andres Noetzli 2015-07-24 17:07:19 -07:00
parent f73c801432
commit 6a82fba75f
5 changed files with 20 additions and 1 deletions

View File

@ -1037,7 +1037,7 @@ rocksdbjava: $(java_libobjects)
jclean:
cd java;$(MAKE) clean;
jtest:
jtest: rocksdbjava
cd java;$(MAKE) sample;$(MAKE) test;
jdb_bench:

View File

@ -105,6 +105,11 @@ abstract class AbstractSlice<T> extends RocksObject {
return compare0(nativeHandle_, other.nativeHandle_);
}
@Override
public int hashCode() {
return toString().hashCode();
}
/**
* If other is a slice object, then
* we defer to {@link #compare(AbstractSlice) compare}

View File

@ -117,6 +117,18 @@ public class WBWIRocksIterator extends AbstractRocksIterator<WriteBatchWithIndex
}
}
/**
* Generates a hash code for the Write Entry. NOTE: The hash code is based
* on the string representation of the key, so it may not work correctly
* with exotic custom comparators.
*
* @return The hash code for the Write Entry
*/
@Override
public int hashCode() {
return (key == null) ? 0 : key.hashCode();
}
@Override
public boolean equals(Object other) {
if(other == null) {

View File

@ -52,6 +52,7 @@ public class SliceTest {
slice = new Slice("abc");
slice2 = new Slice("abc");
assertThat(slice.equals(slice2)).isTrue();
assertThat(slice.hashCode() == slice2.hashCode()).isTrue();
} finally {
if (slice != null) {
slice.dispose();

View File

@ -252,6 +252,7 @@ public class WriteBatchWithIndexTest {
it = wbwi.newIterator();
it.seekToFirst();
assertThat(it.entry().equals(expected[0])).isTrue();
assertThat(it.entry().hashCode() == expected[0].hashCode()).isTrue();
} finally {
if(it != null) {
it.dispose();