summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/unit/UnitValue.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/sevenUnits/unit/UnitValue.java')
-rw-r--r--src/main/java/sevenUnits/unit/UnitValue.java74
1 files changed, 30 insertions, 44 deletions
diff --git a/src/main/java/sevenUnits/unit/UnitValue.java b/src/main/java/sevenUnits/unit/UnitValue.java
index 2d01831..e24b6e2 100644
--- a/src/main/java/sevenUnits/unit/UnitValue.java
+++ b/src/main/java/sevenUnits/unit/UnitValue.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
@@ -17,23 +17,23 @@
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
*/
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
@@ -56,67 +56,52 @@ public final class UnitValue {
}
/**
+ * @param other unit to try to convert to
* @return true if this value can be converted to {@code other}.
* @since 2020-10-01
+ * @since v0.3.0
*/
- public final boolean canConvertTo(Unit other) {
- return this.unit.canConvertTo(other);
- }
-
- /**
- * @return true if this value can be converted to {@code other}.
- * @since 2020-10-01
- */
- public final <W> boolean canConvertTo(Unitlike<W> other) {
+ public boolean canConvertTo(Unit other) {
return this.unit.canConvertTo(other);
}
/**
- * Returns a UnitlikeValue that represents the same value expressed in a
- * different unitlike form.
- *
- * @param other new unit to express value in
- * @return value expressed in {@code other}
- */
- public final <U extends Unitlike<W>, W> UnitlikeValue<U, W> convertTo(
- U other) {
- return UnitlikeValue.of(other,
- this.unit.convertTo(other, this.getValue()));
- }
-
- /**
* 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()));
}
/**
- * Returns this unit value represented as a {@code LinearUnitValue} with this
+ * Returns this unit value represented as a {@link LinearUnitValue} with this
* unit's base unit as the base.
*
* @param ns name and symbol for the base unit, use NameSymbol.EMPTY if not
* needed.
+ * @return this unit as a {@link LinearUnitValue}
* @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);
}
/**
+ * @param newUnit unit to use for this value
* @return a {@code LinearUnitValue} that is equivalent to this value. It
* will have zero uncertainty.
* @since 2020-09-29
+ * @since v0.3.0
*/
- public final LinearUnitValue convertToLinear(LinearUnit other) {
- return LinearUnitValue.getExact(other,
- this.getUnit().convertTo(other, this.getValue()));
+ public LinearUnitValue convertToLinear(LinearUnit newUnit) {
+ return LinearUnitValue.getExact(newUnit,
+ this.getUnit().convertTo(newUnit, this.getValue()));
}
/**
@@ -128,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
@@ -139,16 +124,18 @@ public final class UnitValue {
/**
* @return the unit
* @since 2020-09-29
+ * @since v0.3.0
*/
- public final Unit getUnit() {
+ public Unit getUnit() {
return this.unit;
}
/**
* @return the value
* @since 2020-09-29
+ * @since v0.3.0
*/
- public final double getValue() {
+ public double getValue() {
return this.value;
}
@@ -160,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;
}
}