diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/sevenUnits/unit/LinearUnit.java | 15 | ||||
-rw-r--r-- | src/main/java/sevenUnits/unit/UnitPrefix.java | 18 | ||||
-rw-r--r-- | src/main/java/sevenUnits/utils/DecimalComparison.java | 15 |
3 files changed, 31 insertions, 17 deletions
diff --git a/src/main/java/sevenUnits/unit/LinearUnit.java b/src/main/java/sevenUnits/unit/LinearUnit.java index 3c3703c..d453a43 100644 --- a/src/main/java/sevenUnits/unit/LinearUnit.java +++ b/src/main/java/sevenUnits/unit/LinearUnit.java @@ -227,6 +227,19 @@ public final class LinearUnit extends Unit { return false; final LinearUnit other = (LinearUnit) obj; return Objects.equals(this.getBase(), other.getBase()) + && Double.compare(this.getConversionFactor(), + other.getConversionFactor()) == 0; + } + + /** + * @return true iff this unit 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 LinearUnit other) { + return Objects.equals(this.getBase(), other.getBase()) && DecimalComparison.equals(this.getConversionFactor(), other.getConversionFactor()); } @@ -247,7 +260,7 @@ public final class LinearUnit extends Unit { @Override public int hashCode() { return 31 * this.getBase().hashCode() - + DecimalComparison.hash(this.getConversionFactor()); + + Double.hashCode(this.getConversionFactor()); } /** 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()); } /** diff --git a/src/main/java/sevenUnits/utils/DecimalComparison.java b/src/main/java/sevenUnits/utils/DecimalComparison.java index 62c3720..03dd15b 100644 --- a/src/main/java/sevenUnits/utils/DecimalComparison.java +++ b/src/main/java/sevenUnits/utils/DecimalComparison.java @@ -69,7 +69,6 @@ public final class DecimalComparison { * @return whether they are equal * @since 2019-03-18 * @since v0.2.0 - * @see #hash(double) */ public static final boolean equals(final double a, final double b) { return DecimalComparison.equals(a, b, DOUBLE_EPSILON); @@ -194,7 +193,6 @@ public final class DecimalComparison { * @param b second value to test * @return whether they are equal * @since 2020-09-07 - * @see #hash(double) */ public static final boolean equals(final UncertainDouble a, final UncertainDouble b) { @@ -236,19 +234,6 @@ public final class DecimalComparison { epsilon); } - /** - * Takes the hash code of doubles. Values that are equal according to - * {@link #equals(double, double)} will probably have the same hash code. - * - * @param d double to hash - * @return hash code of double - * @since 2019-10-16 - */ - // TODO reconsider using this - public static final int hash(final double d) { - return Float.hashCode((float) d); - } - // You may NOT get any DecimalComparison instances private DecimalComparison() { throw new AssertionError(); |