Skip to content

Commit

Permalink
Added hashcode to domain and changed equals method a bit, added Test (#…
Browse files Browse the repository at this point in the history
…2412)

This commit is one of many prepare PRs / commits for a bigger PR:
SumStateAlert #2260
By Seperating the big #2260 into smaller commits we would like to make the review easier.

Co-authored-by: Kai Jeschek <[email protected]>
  • Loading branch information
DerStoecki and da-Kai authored Nov 1, 2023
1 parent d248034 commit 4870b41
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,21 @@ public Domain(Field[] fields, Operator operator, Object value) {
this.value = value;
}

@Override
public int hashCode() {
return Objects.hash(this.field, this.operator, this.value);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!Domain.class.isInstance(obj)) {
} else if (obj instanceof Domain other) {
return Objects.equals(this.field, other.field) //
&& Objects.equals(this.operator, other.operator) //
&& Objects.equals(this.value, other.value);
} else {
return false;
}
var other = (Domain) obj;
return Objects.equals(this.field, other.field) && Objects.equals(this.operator, other.operator)
&& Objects.equals(this.value, other.value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThrows;

import org.junit.Test;

Expand Down Expand Up @@ -33,20 +34,31 @@ public boolean isQuery() {
}
};

@Test
public void testDummy() {
assertThrows(UnsupportedOperationException.class, TEST_FIELD::name);
assertThrows(UnsupportedOperationException.class, TEST_FIELD::index);
assertThrows(UnsupportedOperationException.class, TEST_FIELD::isQuery);
}

@Test
public void testConstructor() {
final var domain_1 = new Domain(TEST_FIELD, Operator.LIKE, "Lorem ipsum");
final var domain1 = new Domain(TEST_FIELD, Operator.LIKE, "Lorem ipsum");

assertEquals("like", domain1.operator);
assertEquals("Lorem ipsum", domain1.value);
assertEquals(TEST_FIELD.id(), domain1.field);

assertEquals("like", domain_1.operator);
assertEquals("Lorem ipsum", domain_1.value);
assertEquals(TEST_FIELD.id(), domain_1.field);
final var domain2 = new Domain(new Field[] { TEST_FIELD, TEST_FIELD }, Operator.EQ, "Lorem ipsum");
final var domain3 = new Domain(TEST_FIELD_ID + "." + TEST_FIELD_ID, Operator.EQ, "Lorem ipsum");

final var domain_3 = new Domain(new Field[] { TEST_FIELD, TEST_FIELD }, Operator.EQ, "Lorem ipsum");
final var domain_4 = new Domain(TEST_FIELD_ID + "." + TEST_FIELD_ID, Operator.EQ, "Lorem ipsum");
assertNotEquals(domain1, domain3);
assertEquals(domain3, domain3);
assertEquals(domain2, domain3);
assertNotEquals(domain3, null);
assertNotEquals(domain3, 1);

assertEquals(domain_3, domain_3);
assertEquals(domain_3, domain_4);
assertNotEquals(domain_3, null);
assertNotEquals(domain_3, 1);
assertEquals(domain3.hashCode(), domain2.hashCode());
assertNotEquals(domain1.hashCode(), domain2.hashCode());
}
}

0 comments on commit 4870b41

Please sign in to comment.