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
main
Andres Noetzli 9 years ago
parent f73c801432
commit 6a82fba75f
  1. 2
      Makefile
  2. 5
      java/src/main/java/org/rocksdb/AbstractSlice.java
  3. 12
      java/src/main/java/org/rocksdb/WBWIRocksIterator.java
  4. 1
      java/src/test/java/org/rocksdb/SliceTest.java
  5. 1
      java/src/test/java/org/rocksdb/WriteBatchWithIndexTest.java

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

@ -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}

@ -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) {

@ -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();

@ -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();

Loading…
Cancel
Save