Skip to content

Commit

Permalink
jqno#540 BigDecimal equality using compareTo
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Clark committed Dec 1, 2021
1 parent 1360dc4 commit c58c9e7
Showing 1 changed file with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
public class BigDecimalTest {

@Test
public void fail_whenBigDecimalsComparedUsingEquals() {
public void fail_whenBigDecimalsComparedUsingEqualsWithComparablyConsistentHashCode() {
ExpectedException
.when(() -> EqualsVerifier.forClass(BigDecimalEquals.class).verify())
.when(() ->
EqualsVerifier
.forClass(BigDecimalEqualsWithComparablyConsistentHashCode.class)
.verify()
)
.assertFailure()
.assertMessageContains(
"BigDecimal",
Expand All @@ -25,19 +29,6 @@ public void fail_whenBigDecimalsComparedUsingEquals() {
);
}

@Test
public void succeed_whenBigDecimalsComparedUsingEquals_givenBigDecimalEqualsWarningIsSuppressed() {
EqualsVerifier
.forClass(BigDecimalEquals.class)
.suppress(Warning.BIGDECIMAL_EQUALITY)
.verify();
}

@Test
public void succeed_whenBigDecimalsComparedUsingCompareTo() {
EqualsVerifier.forClass(BigDecimalCompareTo.class).verify();
}

@Test
public void fail_whenBigDecimalsComparedUsingCompareToWithInconsistentHashCode() {
ExpectedException
Expand All @@ -52,6 +43,19 @@ public void fail_whenBigDecimalsComparedUsingCompareToWithInconsistentHashCode()
);
}

@Test
public void succeed_whenBigDecimalsComparedUsingEquals_givenBigDecimalEqualsWarningIsSuppressed() {
EqualsVerifier
.forClass(BigDecimalEquals.class)
.suppress(Warning.BIGDECIMAL_EQUALITY)
.verify();
}

@Test
public void succeed_whenBigDecimalsComparedUsingCompareTo() {
EqualsVerifier.forClass(BigDecimalCompareTo.class).verify();
}

/**
* Uses standard equals and hashCode for objects.
* 0 and 0.0 are not equal.
Expand Down Expand Up @@ -107,8 +111,39 @@ public int hashCode() {
}

/**
* Uses compareTo for BigDecimal equality but has hashCode that may be inconsistent.
* 0 and 0.0 are equal but may produce a different hashCode.
* Uses standard equals but with a consistent hashCode for comparably equal instances.
* 0 and 0.0 are not equal but produce the same hashCode.
*/
private static final class BigDecimalEqualsWithComparablyConsistentHashCode {

private final BigDecimal bd;

public BigDecimalEqualsWithComparablyConsistentHashCode(BigDecimal bd) {
this.bd = bd;
}

public BigDecimal getBd() {
return bd;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BigDecimalEqualsWithComparablyConsistentHashCode)) {
return false;
}
BigDecimalEqualsWithComparablyConsistentHashCode other = (BigDecimalEqualsWithComparablyConsistentHashCode) obj;
return Objects.equals(bd, other.bd);
}

@Override
public int hashCode() {
return Objects.hashCode(comparablyHashed(bd));
}
}

/**
* Uses compareTo for BigDecimal equality but has hashCode that is inconsistent.
* 0 and 0.0 are equal but produce different hashCodes.
*/
private static final class BigDecimalInconsistentHashCode {

Expand Down

0 comments on commit c58c9e7

Please sign in to comment.