From 7db19d307970b73559239ec343c92c7876510c2a Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 30 May 2025 20:10:09 -0500 Subject: Ensure LinearUnit&Prefix ==/hash obey contracts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, these classes' equals() and hashCode() methods did not obey the contracts: For equals(), I considered two values equal even if there was a very small deviation, in order to avoid floating-point error. This equals relation is not transitive (i.e. it is possible that a = b && b = c but a ≠ c), violating the contract of equals. This also makes it impossible to properly implement hashCode, as if two values are equal, they must have the same hash code. The solution I had provided is an ineffective hack, which could mess with hash maps and sets. I have changed the implementation to demand exact equality. I have also provided equalsApproximately() methods to both classes that use the old behaviour. Hash codes are only really used for hash maps, and the old implementation doesn't even achieve its purpose, so I did not add a method to return the old hash behaviour. --- src/main/java/sevenUnits/unit/UnitPrefix.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/main/java/sevenUnits/unit/UnitPrefix.java') diff --git a/src/main/java/sevenUnits/unit/UnitPrefix.java b/src/main/java/sevenUnits/unit/UnitPrefix.java index 9035969..ec4be48 100644 --- a/src/main/java/sevenUnits/unit/UnitPrefix.java +++ b/src/main/java/sevenUnits/unit/UnitPrefix.java @@ -119,6 +119,22 @@ public final class UnitPrefix implements Nameable { if (!(obj instanceof UnitPrefix)) return false; final UnitPrefix other = (UnitPrefix) obj; + return Double.compare(this.getMultiplier(), + other.getMultiplier()) == 0; + } + + /** + * @return true iff this prefix and other are equal, + * ignoring small differences caused by floating-point error. + * + * @apiNote This method is not transitive, + * so it cannot be used as an equals method. + */ + public boolean equalsApproximately(final UnitPrefix other) { + if (this == other) + return true; + if (other == null) + return false; return DecimalComparison.equals(this.getMultiplier(), other.getMultiplier()); } @@ -143,7 +159,7 @@ public final class UnitPrefix implements Nameable { */ @Override public int hashCode() { - return DecimalComparison.hash(this.getMultiplier()); + return Double.hashCode(this.getMultiplier()); } /** -- cgit v1.2.3