Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions java/core/src/main/java/sleeper/core/range/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,17 @@ public boolean isInCanonicalForm() {

@Override
public int hashCode() {
Range canonicalisedThis = RangeCanonicaliser.canonicaliseRange(this);
int hash = 5;
hash = 13 * hash + Objects.hashCode(this.field);
hash = 13 * hash + (this.minInclusive ? 1 : 0);
hash = 13 * hash + (this.maxInclusive ? 1 : 0);
hash = 13 * hash + Objects.hashCode(canonicalisedThis.field);
hash = 13 * hash + (canonicalisedThis.minInclusive ? 1 : 0);
hash = 13 * hash + (canonicalisedThis.maxInclusive ? 1 : 0);
if (field.getType() instanceof ByteArrayType) {
hash = 13 * hash + Objects.hashCode(ByteArray.wrap((byte[]) this.min));
hash = 13 * hash + Objects.hashCode(ByteArray.wrap((byte[]) this.max));
hash = 13 * hash + Objects.hashCode(ByteArray.wrap((byte[]) canonicalisedThis.min));
hash = 13 * hash + Objects.hashCode(ByteArray.wrap((byte[]) canonicalisedThis.max));
} else {
hash = 13 * hash + Objects.hashCode(this.min);
hash = 13 * hash + Objects.hashCode(this.max);
hash = 13 * hash + Objects.hashCode(canonicalisedThis.min);
hash = 13 * hash + Objects.hashCode(canonicalisedThis.max);
}
return hash;
}
Expand All @@ -206,30 +207,31 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass()) {
return false;
}
final Range other = (Range) obj;
final Range other = RangeCanonicaliser.canonicaliseRange((Range) obj);
final Range canonicalisedThis = RangeCanonicaliser.canonicaliseRange(this);

Object minTransformed;
Object otherMinTransformed;
Object maxTransformed;
Object otherMaxTransformed;
if (field.getType() instanceof ByteArrayType) {
minTransformed = ByteArray.wrap((byte[]) this.min);
minTransformed = ByteArray.wrap((byte[]) canonicalisedThis.min);
otherMinTransformed = ByteArray.wrap((byte[]) other.min);
maxTransformed = ByteArray.wrap((byte[]) this.max);
maxTransformed = ByteArray.wrap((byte[]) canonicalisedThis.max);
otherMaxTransformed = ByteArray.wrap((byte[]) other.max);
} else {
minTransformed = this.min;
minTransformed = canonicalisedThis.min;
otherMinTransformed = other.min;
maxTransformed = this.max;
maxTransformed = canonicalisedThis.max;
otherMaxTransformed = other.max;
}
if (this.minInclusive != other.minInclusive) {
if (canonicalisedThis.minInclusive != other.minInclusive) {
return false;
}
if (this.maxInclusive != other.maxInclusive) {
if (canonicalisedThis.maxInclusive != other.maxInclusive) {
return false;
}
if (!Objects.equals(this.field, other.field)) {
if (!Objects.equals(canonicalisedThis.field, other.field)) {
return false;
}
if (!Objects.equals(minTransformed, otherMinTransformed)) {
Expand Down
24 changes: 24 additions & 0 deletions java/core/src/test/java/sleeper/core/range/RangeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ public void testEqualsAndHashcode() {
assertThat(hashCode4).isNotEqualTo(hashCode3);
}

@Test
public void shouldReturnTrueForEqualsWhenExactRangeCreatedInTwoWays() {
// Given
Field field = new Field("key", new StringType());
Schema schema = Schema.builder().rowKeyFields(field).build();
RangeFactory rangeFactory = new RangeFactory(schema);
Range range1 = rangeFactory.createExactRange(field, "A");
Range range2 = rangeFactory.createRange(field, "A", true, "A", true);
Range range3 = rangeFactory.createRange(field, "A", true, "A" + '\u0000', true);

// When
boolean equals1 = range1.equals(range2);
boolean equals2 = range1.equals(range3);
int hashCode1 = range1.hashCode();
int hashCode2 = range2.hashCode();
int hashCode3 = range3.hashCode();

// Then
assertThat(equals1).isTrue();
assertThat(equals2).isFalse();
assertThat(hashCode2).isEqualTo(hashCode1);
assertThat(hashCode3).isNotEqualTo(hashCode1);
}

@Test
public void shouldThrowExceptionIfGivenWrongType() {
// Given
Expand Down
Loading