diff options
Diffstat (limited to 'src/main/java/sevenUnits/unit/UnitValue.java')
-rw-r--r-- | src/main/java/sevenUnits/unit/UnitValue.java | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/src/main/java/sevenUnits/unit/UnitValue.java b/src/main/java/sevenUnits/unit/UnitValue.java index 4003c17..e24b6e2 100644 --- a/src/main/java/sevenUnits/unit/UnitValue.java +++ b/src/main/java/sevenUnits/unit/UnitValue.java @@ -17,16 +17,15 @@ package sevenUnits.unit; import java.util.Objects; -import java.util.Optional; import sevenUnits.utils.NameSymbol; /** * A value expressed in a unit. - * + * * Unless otherwise indicated, all methods in this class throw a * {@code NullPointerException} when an argument is null. - * + * * @author Adrien Hopkins * @since 2020-07-26 * @since v0.3.0 @@ -34,7 +33,7 @@ import sevenUnits.utils.NameSymbol; public final class UnitValue { /** * Creates a {@code UnitValue} from a unit and the associated value. - * + * * @param unit unit to use * @param value value to use * @return {@code UnitValue} instance @@ -62,18 +61,18 @@ public final class UnitValue { * @since 2020-10-01 * @since v0.3.0 */ - public final boolean canConvertTo(Unit other) { + public boolean canConvertTo(Unit other) { return this.unit.canConvertTo(other); } /** * Returns a UnitValue that represents the same value expressed in a * different unit - * + * * @param other new unit to express value in * @return value expressed in {@code other} */ - public final UnitValue convertTo(Unit other) { + public UnitValue convertTo(Unit other) { return UnitValue.of(other, this.getUnit().convertTo(other, this.getValue())); } @@ -88,8 +87,8 @@ public final class UnitValue { * @since 2020-09-29 * @since v0.3.0 */ - public final LinearUnitValue convertToBase(NameSymbol ns) { - final LinearUnit base = LinearUnit.getBase(this.unit).withName(ns); + public LinearUnitValue convertToBase(NameSymbol ns) { + final var base = LinearUnit.getBase(this.unit).withName(ns); return this.convertToLinear(base); } @@ -100,7 +99,7 @@ public final class UnitValue { * @since 2020-09-29 * @since v0.3.0 */ - public final LinearUnitValue convertToLinear(LinearUnit newUnit) { + public LinearUnitValue convertToLinear(LinearUnit newUnit) { return LinearUnitValue.getExact(newUnit, this.getUnit().convertTo(newUnit, this.getValue())); } @@ -114,7 +113,7 @@ public final class UnitValue { public boolean equals(Object obj) { if (!(obj instanceof UnitValue)) return false; - final UnitValue other = (UnitValue) obj; + final var other = (UnitValue) obj; return Objects.equals(this.getUnit().getBase(), other.getUnit().getBase()) && Double.doubleToLongBits( this.getUnit().convertToBase(this.getValue())) == Double @@ -127,7 +126,7 @@ public final class UnitValue { * @since 2020-09-29 * @since v0.3.0 */ - public final Unit getUnit() { + public Unit getUnit() { return this.unit; } @@ -136,7 +135,7 @@ public final class UnitValue { * @since 2020-09-29 * @since v0.3.0 */ - public final double getValue() { + public double getValue() { return this.value; } @@ -148,16 +147,15 @@ public final class UnitValue { @Override public String toString() { - final Optional<String> primaryName = this.getUnit().getPrimaryName(); - final Optional<String> symbol = this.getUnit().getSymbol(); + final var primaryName = this.getUnit().getPrimaryName(); + final var symbol = this.getUnit().getSymbol(); if (primaryName.isEmpty() && symbol.isEmpty()) { - final double baseValue = this.getUnit().convertToBase(this.getValue()); + final var baseValue = this.getUnit().convertToBase(this.getValue()); return String.format("%s unnamed unit (= %s %s)", this.getValue(), baseValue, this.getUnit().getBase() .toString(unit -> unit.getSymbol().orElseThrow())); - } else { - final String unitName = symbol.orElse(primaryName.get()); - return this.getValue() + " " + unitName; } + final var unitName = symbol.orElse(primaryName.get()); + return this.getValue() + " " + unitName; } } |