diff options
Diffstat (limited to 'src/main/java/sevenUnits/unit/LinearUnit.java')
-rw-r--r-- | src/main/java/sevenUnits/unit/LinearUnit.java | 115 |
1 files changed, 73 insertions, 42 deletions
diff --git a/src/main/java/sevenUnits/unit/LinearUnit.java b/src/main/java/sevenUnits/unit/LinearUnit.java index 6489229..85f6dd9 100644 --- a/src/main/java/sevenUnits/unit/LinearUnit.java +++ b/src/main/java/sevenUnits/unit/LinearUnit.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2019 Adrien Hopkins + * Copyright (C) 2019, 2021, 2022, 2024, 2025 Adrien Hopkins * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -26,19 +26,21 @@ import sevenUnits.utils.UncertainDouble; /** * A unit that can be expressed as a product of its base and a number. For * example, kilometres, inches and pounds. - * + * * @author Adrien Hopkins * @since 2019-10-16 + * @since v0.3.0 */ public final class LinearUnit extends Unit { /** * Gets a {@code LinearUnit} from a unit and a value. For example, converts * '59 °F' to a linear unit with the value of '288.15 K' - * + * * @param unit unit to convert * @param value value to convert * @return value expressed as a {@code LinearUnit} * @since 2019-10-16 + * @since v0.3.0 * @throws NullPointerException if unit is null */ public static LinearUnit fromUnitValue(final Unit unit, final double value) { @@ -50,12 +52,13 @@ public final class LinearUnit extends Unit { /** * Gets a {@code LinearUnit} from a unit and a value. For example, converts * '59 °F' to a linear unit with the value of '288.15 K' - * + * * @param unit unit to convert * @param value value to convert * @param ns name(s) and symbol of unit * @return value expressed as a {@code LinearUnit} * @since 2019-10-21 + * @since v0.3.0 * @throws NullPointerException if unit or ns is null */ public static LinearUnit fromUnitValue(final Unit unit, final double value, @@ -66,32 +69,26 @@ public final class LinearUnit extends Unit { } /** + * @param unit unit to get base version of * @return the base unit associated with {@code unit}, as a * {@code LinearUnit}. * @since 2020-10-02 + * @since v0.3.0 */ public static LinearUnit getBase(final Unit unit) { return new LinearUnit(unit.getBase(), 1, NameSymbol.EMPTY); } /** - * @return the base unit associated with {@code unitlike}, as a - * {@code LinearUnit}. - * @since 2020-10-02 - */ - public static LinearUnit getBase(final Unitlike<?> unit) { - return new LinearUnit(unit.getBase(), 1, NameSymbol.EMPTY); - } - - /** * Gets a {@code LinearUnit} from a unit base and a conversion factor. In * other words, gets the product of {@code unitBase} and * {@code conversionFactor}, expressed as a {@code LinearUnit}. - * + * * @param unitBase unit base to multiply by * @param conversionFactor number to multiply base by * @return product of base and conversion factor * @since 2019-10-16 + * @since v0.3.0 * @throws NullPointerException if unitBase is null */ public static LinearUnit valueOf(final ObjectProduct<BaseUnit> unitBase, @@ -103,12 +100,13 @@ public final class LinearUnit extends Unit { * Gets a {@code LinearUnit} from a unit base and a conversion factor. In * other words, gets the product of {@code unitBase} and * {@code conversionFactor}, expressed as a {@code LinearUnit}. - * + * * @param unitBase unit base to multiply by * @param conversionFactor number to multiply base by * @param ns name(s) and symbol of unit * @return product of base and conversion factor * @since 2019-10-21 + * @since v0.3.0 * @throws NullPointerException if unitBase is null */ public static LinearUnit valueOf(final ObjectProduct<BaseUnit> unitBase, @@ -118,21 +116,23 @@ public final class LinearUnit extends Unit { /** * The value of this unit as represented in its base form. Mathematically, - * + * * <pre> * this = conversionFactor * getBase() * </pre> - * + * * @since 2019-10-16 + * @since v0.3.0 */ private final double conversionFactor; /** * Creates the {@code LinearUnit}. - * + * * @param unitBase base of linear unit * @param conversionFactor conversion factor between base and unit * @since 2019-10-16 + * @since v0.3.0 */ private LinearUnit(final ObjectProduct<BaseUnit> unitBase, final double conversionFactor, final NameSymbol ns) { @@ -142,7 +142,7 @@ public final class LinearUnit extends Unit { /** * {@inheritDoc} - * + * * Converts by dividing by {@code conversionFactor} */ @Override @@ -153,11 +153,12 @@ public final class LinearUnit extends Unit { /** * Converts an {@code UncertainDouble} value expressed in this unit to an * {@code UncertainValue} value expressed in {@code other}. - * + * * @param other unit to convert to * @param value value to convert * @return converted value * @since 2019-09-07 + * @since v0.3.0 * @throws IllegalArgumentException if {@code other} is incompatible for * conversion with this unit (as tested by * {@link Unit#canConvertTo}). @@ -169,15 +170,14 @@ public final class LinearUnit extends Unit { if (this.canConvertTo(other)) return value.timesExact( this.getConversionFactor() / other.getConversionFactor()); - else - throw new IllegalArgumentException( - String.format("Cannot convert from %s to %s.", this, other)); + throw new IllegalArgumentException( + String.format("Cannot convert from %s to %s.", this, other)); } /** * {@inheritDoc} - * + * * Converts by multiplying by {@code conversionFactor} */ @Override @@ -187,8 +187,9 @@ public final class LinearUnit extends Unit { /** * Converts an {@code UncertainDouble} to the base unit. - * + * * @since 2020-09-07 + * @since v0.3.0 */ UncertainDouble convertToBase(final UncertainDouble value) { return value.timesExact(this.getConversionFactor()); @@ -196,7 +197,7 @@ public final class LinearUnit extends Unit { /** * Divides this unit by a scalar. - * + * * @param divisor scalar to divide by * @return quotient * @since 2018-12-23 @@ -208,7 +209,7 @@ public final class LinearUnit extends Unit { /** * Returns the quotient of this unit and another. - * + * * @param divisor unit to divide by * @return quotient of two units * @throws NullPointerException if {@code divisor} is null @@ -219,22 +220,35 @@ public final class LinearUnit extends Unit { Objects.requireNonNull(divisor, "other must not be null"); // divide the units - final ObjectProduct<BaseUnit> base = this.getBase() - .dividedBy(divisor.getBase()); + final var base = this.getBase().dividedBy(divisor.getBase()); return valueOf(base, this.getConversionFactor() / divisor.getConversionFactor()); } /** * {@inheritDoc} - * + * * Uses the base and conversion factor of units to test for equality. */ @Override public boolean equals(final Object obj) { if (!(obj instanceof LinearUnit)) return false; - final LinearUnit other = (LinearUnit) obj; + final var other = (LinearUnit) obj; + return Objects.equals(this.getBase(), other.getBase()) + && Double.compare(this.getConversionFactor(), + other.getConversionFactor()) == 0; + } + + /** + * @param other unit to test equality with + * @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()); @@ -243,6 +257,7 @@ public final class LinearUnit extends Unit { /** * @return conversion factor * @since 2019-10-16 + * @since v0.3.0 */ public double getConversionFactor() { return this.conversionFactor; @@ -250,13 +265,13 @@ public final class LinearUnit extends Unit { /** * {@inheritDoc} - * + * * Uses the base and conversion factor to compute a hash code. */ @Override public int hashCode() { return 31 * this.getBase().hashCode() - + DecimalComparison.hash(this.getConversionFactor()); + + Double.hashCode(this.getConversionFactor()); } /** @@ -264,6 +279,7 @@ public final class LinearUnit extends Unit { * is a {@code BaseUnit b} where * {@code b.asLinearUnit().equals(this)} returns {@code true}.) * @since 2019-10-16 + * @since v0.3.0 */ public boolean isBase() { return this.isCoherent() && this.getBase().isSingleObject(); @@ -272,6 +288,7 @@ public final class LinearUnit extends Unit { /** * @return whether this unit is coherent (i.e. has conversion factor 1) * @since 2019-10-16 + * @since v0.3.0 */ public boolean isCoherent() { return this.getConversionFactor() == 1; @@ -285,7 +302,7 @@ public final class LinearUnit extends Unit { * does not meet this condition, an {@code IllegalArgumentException} will be * thrown. * </p> - * + * * @param subtrahend unit to subtract * @return difference of units * @throws IllegalArgumentException if {@code subtrahend} is not compatible @@ -316,7 +333,7 @@ public final class LinearUnit extends Unit { * does not meet this condition, an {@code IllegalArgumentException} will be * thrown. * </p> - * + * * @param addend unit to add * @return sum of units * @throws IllegalArgumentException if {@code addend} is not compatible for @@ -341,7 +358,7 @@ public final class LinearUnit extends Unit { /** * Multiplies this unit by a scalar. - * + * * @param multiplier scalar to multiply by * @return product * @since 2018-12-23 @@ -353,7 +370,7 @@ public final class LinearUnit extends Unit { /** * Returns the product of this unit and another. - * + * * @param multiplier unit to multiply by * @return product of two units * @throws NullPointerException if {@code multiplier} is null @@ -364,8 +381,7 @@ public final class LinearUnit extends Unit { Objects.requireNonNull(multiplier, "other must not be null"); // multiply the units - final ObjectProduct<BaseUnit> base = this.getBase() - .times(multiplier.getBase()); + final var base = this.getBase().times(multiplier.getBase()); return valueOf(base, this.getConversionFactor() * multiplier.getConversionFactor()); } @@ -379,7 +395,7 @@ public final class LinearUnit extends Unit { /** * Returns this unit but to an exponent. - * + * * @param exponent exponent to exponentiate unit to * @return exponentiated unit * @since 2019-01-15 @@ -390,6 +406,21 @@ public final class LinearUnit extends Unit { Math.pow(this.conversionFactor, exponent)); } + /** + * Returns this unit to an exponent, rounding the resulting dimensions to the + * nearest integer. + * + * @param exponent exponent to raise unit to + * @return result of rounded exponentation + * @since 2024-08-22 + * @since v1.0.0 + * @see ObjectProduct#toExponentRounded + */ + public LinearUnit toExponentRounded(final double exponent) { + return valueOf(this.getBase().toExponentRounded(exponent), + Math.pow(this.conversionFactor, exponent)); + } + @Override public LinearUnit withName(final NameSymbol ns) { return valueOf(this.getBase(), this.getConversionFactor(), ns); @@ -404,7 +435,7 @@ public final class LinearUnit extends Unit { * have a symbol. <br> * This method ignores alternate names of both this unit and the provided * prefix. - * + * * @param prefix prefix to apply * @return unit with prefix * @since 2019-03-18 @@ -412,7 +443,7 @@ public final class LinearUnit extends Unit { * @throws NullPointerException if prefix is null */ public LinearUnit withPrefix(final UnitPrefix prefix) { - final LinearUnit unit = this.times(prefix.getMultiplier()); + final var unit = this.times(prefix.getMultiplier()); // create new name and symbol, if possible final String name; |