summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2025-05-30 19:48:42 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2025-05-30 19:54:19 -0500
commite86fb71e1665cb9e8511bafd54df0fb8cbca5adc (patch)
treecbc3aa8c4a4848c0a0f9e332fcab576ad0c10e27
parent8df414ced2018e685f034424c3d8c7813d18805c (diff)
Remove Unitlike/MultiUnit
I ended up never using this code - it was simpler to just use lists of units and values. Making a whole new object for lists of units, and an abstract class for things that convert things other than doubles, is needlessly complicated, and doesn't solve any major issues. For example, I still need to store each Unitlike type in a different collection, because it will have a different type.
-rw-r--r--src/main/java/sevenUnits/unit/FunctionalUnitlike.java73
-rw-r--r--src/main/java/sevenUnits/unit/LinearUnit.java9
-rw-r--r--src/main/java/sevenUnits/unit/MultiUnit.java161
-rw-r--r--src/main/java/sevenUnits/unit/Unit.java52
-rw-r--r--src/main/java/sevenUnits/unit/UnitValue.java21
-rw-r--r--src/main/java/sevenUnits/unit/Unitlike.java262
-rw-r--r--src/main/java/sevenUnits/unit/UnitlikeValue.java176
-rw-r--r--src/test/java/sevenUnits/unit/MultiUnitTest.java110
8 files changed, 0 insertions, 864 deletions
diff --git a/src/main/java/sevenUnits/unit/FunctionalUnitlike.java b/src/main/java/sevenUnits/unit/FunctionalUnitlike.java
deleted file mode 100644
index e9b4d1f..0000000
--- a/src/main/java/sevenUnits/unit/FunctionalUnitlike.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/**
- * Copyright (C) 2020 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-package sevenUnits.unit;
-
-import java.util.function.DoubleFunction;
-import java.util.function.ToDoubleFunction;
-
-import sevenUnits.utils.NameSymbol;
-import sevenUnits.utils.ObjectProduct;
-
-/**
- * A unitlike form that converts using two conversion functions.
- *
- * @since 2020-09-07
- */
-final class FunctionalUnitlike<V> extends Unitlike<V> {
- /**
- * A function that accepts a value in the unitlike form's base and returns a
- * value in the unitlike form.
- *
- * @since 2020-09-07
- */
- private final DoubleFunction<V> converterFrom;
-
- /**
- * A function that accepts a value in the unitlike form and returns a value
- * in the unitlike form's base.
- */
- private final ToDoubleFunction<V> converterTo;
-
- /**
- * Creates the {@code FunctionalUnitlike}.
- *
- * @param base unitlike form's base
- * @param converterFrom function that accepts a value in the unitlike form's
- * base and returns a value in the unitlike form.
- * @param converterTo function that accepts a value in the unitlike form
- * and returns a value in the unitlike form's base.
- * @throws NullPointerException if any argument is null
- * @since 2019-05-22
- */
- protected FunctionalUnitlike(ObjectProduct<BaseUnit> unitBase, NameSymbol ns,
- DoubleFunction<V> converterFrom, ToDoubleFunction<V> converterTo) {
- super(unitBase, ns);
- this.converterFrom = converterFrom;
- this.converterTo = converterTo;
- }
-
- @Override
- protected V convertFromBase(double value) {
- return this.converterFrom.apply(value);
- }
-
- @Override
- protected double convertToBase(V value) {
- return this.converterTo.applyAsDouble(value);
- }
-
-}
diff --git a/src/main/java/sevenUnits/unit/LinearUnit.java b/src/main/java/sevenUnits/unit/LinearUnit.java
index a230f28..3c3703c 100644
--- a/src/main/java/sevenUnits/unit/LinearUnit.java
+++ b/src/main/java/sevenUnits/unit/LinearUnit.java
@@ -75,15 +75,6 @@ public final class LinearUnit extends Unit {
}
/**
- * @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}.
diff --git a/src/main/java/sevenUnits/unit/MultiUnit.java b/src/main/java/sevenUnits/unit/MultiUnit.java
deleted file mode 100644
index c6d3b97..0000000
--- a/src/main/java/sevenUnits/unit/MultiUnit.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/**
- * Copyright (C) 2020 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-package sevenUnits.unit;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import sevenUnits.utils.NameSymbol;
-import sevenUnits.utils.ObjectProduct;
-
-/**
- * A combination of units, like "5 foot + 7 inch". All but the last units should
- * have a whole number value associated with them.
- *
- * @since 2020-10-02
- */
-public final class MultiUnit extends Unitlike<List<Double>> {
- /**
- * Creates a {@code MultiUnit} from its units. It will not have a name or
- * symbol.
- *
- * @since 2020-10-03
- */
- public static final MultiUnit of(LinearUnit... units) {
- return of(Arrays.asList(units));
- }
-
- /**
- * Creates a {@code MultiUnit} from its units. It will not have a name or
- * symbol.
- *
- * @since 2020-10-03
- */
- public static final MultiUnit of(List<LinearUnit> units) {
- if (units.size() < 1)
- throw new IllegalArgumentException("Must have at least one unit");
- final ObjectProduct<BaseUnit> unitBase = units.get(0).getBase();
- for (final LinearUnit unit : units) {
- if (!unitBase.equals(unit.getBase()))
- throw new IllegalArgumentException(
- "All units must have the same base.");
- }
- return new MultiUnit(new ArrayList<>(units), unitBase, NameSymbol.EMPTY);
- }
-
- /**
- * The units that make up this value.
- */
- private final List<LinearUnit> units;
-
- /**
- * Creates a {@code MultiUnit}.
- *
- * @since 2020-10-03
- */
- private MultiUnit(List<LinearUnit> units, ObjectProduct<BaseUnit> unitBase,
- NameSymbol ns) {
- super(unitBase, ns);
- this.units = units;
- }
-
- @Override
- protected List<Double> convertFromBase(double value) {
- final List<Double> values = new ArrayList<>(this.units.size());
- double temp = value;
-
- for (final LinearUnit unit : this.units.subList(0,
- this.units.size() - 1)) {
- values.add(Math.floor(temp / unit.getConversionFactor()));
- temp %= unit.getConversionFactor();
- }
-
- values.add(this.units.size() - 1,
- this.units.get(this.units.size() - 1).convertFromBase(temp));
-
- return values;
- }
-
- /**
- * Converts a value expressed in this unitlike form to a value expressed in
- * {@code other}.
- *
- * @implSpec If conversion is possible, this implementation returns
- * {@code other.convertFromBase(this.convertToBase(value))}.
- * Therefore, overriding either of those methods will change the
- * output of this method.
- *
- * @param other unit to convert to
- * @param values values to convert
- * @return converted value
- * @since 2020-10-03
- * @throws IllegalArgumentException if {@code other} is incompatible for
- * conversion with this unitlike form (as
- * tested by {@link Unit#canConvertTo}).
- * @throws NullPointerException if other is null
- */
- public final <U extends Unitlike<V>, V> V convertTo(U other,
- double... values) {
- final List<Double> valueList = new ArrayList<>(values.length);
- for (final double d : values) {
- valueList.add(d);
- }
-
- return this.convertTo(other, valueList);
- }
-
- /**
- * Converts a value expressed in this unitlike form to a value expressed in
- * {@code other}.
- *
- * @implSpec If conversion is possible, this implementation returns
- * {@code other.convertFromBase(this.convertToBase(value))}.
- * Therefore, overriding either of those methods will change the
- * output of this method.
- *
- * @param other unit to convert to
- * @param values values to convert
- * @return converted value
- * @since 2020-10-03
- * @throws IllegalArgumentException if {@code other} is incompatible for
- * conversion with this unitlike form (as
- * tested by {@link Unit#canConvertTo}).
- * @throws NullPointerException if other is null
- */
- public final double convertTo(Unit other, double... values) {
- final List<Double> valueList = new ArrayList<>(values.length);
- for (final double d : values) {
- valueList.add(d);
- }
-
- return this.convertTo(other, valueList);
- }
-
- @Override
- protected double convertToBase(List<Double> value) {
- if (value.size() != this.units.size())
- throw new IllegalArgumentException("Wrong number of values for "
- + this.units.size() + "-unit MultiUnit.");
-
- double baseValue = 0;
- for (int i = 0; i < this.units.size(); i++) {
- baseValue += value.get(i) * this.units.get(i).getConversionFactor();
- }
- return baseValue;
- }
-}
diff --git a/src/main/java/sevenUnits/unit/Unit.java b/src/main/java/sevenUnits/unit/Unit.java
index 59e928a..d25b362 100644
--- a/src/main/java/sevenUnits/unit/Unit.java
+++ b/src/main/java/sevenUnits/unit/Unit.java
@@ -137,15 +137,6 @@ public abstract class Unit implements Nameable {
}
/**
- * @return this unit as a {@link Unitlike}
- * @since 2020-09-07
- */
- public final Unitlike<Double> asUnitlike() {
- return Unitlike.fromConversionFunctions(this.getBase(),
- this::convertFromBase, this::convertToBase, this.getNameSymbol());
- }
-
- /**
* Checks if a value expressed in this unit can be converted to a value
* expressed in {@code other}
*
@@ -161,21 +152,6 @@ public abstract class Unit implements Nameable {
}
/**
- * Checks if a value expressed in this unit can be converted to a value
- * expressed in {@code other}
- *
- * @param other unit or unitlike form to test with
- * @return true if they are compatible
- * @since 2019-01-13
- * @since v0.1.0
- * @throws NullPointerException if other is null
- */
- public final <W> boolean canConvertTo(final Unitlike<W> other) {
- Objects.requireNonNull(other, "other must not be null.");
- return Objects.equals(this.getBase(), other.getBase());
- }
-
- /**
* Converts from a value expressed in this unit's base unit to a value
* expressed in this unit.
* <p>
@@ -226,34 +202,6 @@ public abstract class Unit implements Nameable {
}
/**
- * Converts a value expressed in this unit to a value expressed in
- * {@code other}.
- *
- * @implSpec If conversion is possible, this implementation returns
- * {@code other.convertFromBase(this.convertToBase(value))}.
- * Therefore, overriding either of those methods will change the
- * output of this method.
- *
- * @param other unitlike form to convert to
- * @param value value to convert
- * @param <W> type of value to convert to
- * @return converted value
- * @since 2020-09-07
- * @throws IllegalArgumentException if {@code other} is incompatible for
- * conversion with this unit (as tested by
- * {@link Unit#canConvertTo}).
- * @throws NullPointerException if other is null
- */
- public final <W> W convertTo(final Unitlike<W> other, final double value) {
- Objects.requireNonNull(other, "other must not be null.");
- if (this.canConvertTo(other))
- return other.convertFromBase(this.convertToBase(value));
- else
- throw new IllegalArgumentException(
- String.format("Cannot convert from %s to %s.", this, other));
- }
-
- /**
* Converts from a value expressed in this unit to a value expressed in this
* unit's base unit.
* <p>
diff --git a/src/main/java/sevenUnits/unit/UnitValue.java b/src/main/java/sevenUnits/unit/UnitValue.java
index 2d01831..9b485e3 100644
--- a/src/main/java/sevenUnits/unit/UnitValue.java
+++ b/src/main/java/sevenUnits/unit/UnitValue.java
@@ -64,27 +64,6 @@ public final class UnitValue {
}
/**
- * @return true if this value can be converted to {@code other}.
- * @since 2020-10-01
- */
- public final <W> boolean canConvertTo(Unitlike<W> 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
*
diff --git a/src/main/java/sevenUnits/unit/Unitlike.java b/src/main/java/sevenUnits/unit/Unitlike.java
deleted file mode 100644
index fef424e..0000000
--- a/src/main/java/sevenUnits/unit/Unitlike.java
+++ /dev/null
@@ -1,262 +0,0 @@
-/**
- * Copyright (C) 2020 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-package sevenUnits.unit;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
-import java.util.function.DoubleFunction;
-import java.util.function.ToDoubleFunction;
-
-import sevenUnits.utils.NameSymbol;
-import sevenUnits.utils.Nameable;
-import sevenUnits.utils.ObjectProduct;
-
-/**
- * An object that can convert a value between multiple forms (instances of the
- * object); like a unit but the "converted value" can be any type.
- *
- * @since 2020-09-07
- */
-public abstract class Unitlike<V> implements Nameable {
- /**
- * Returns a unitlike form from its base and the functions it uses to convert
- * to and from its base.
- *
- * @param base unitlike form's base
- * @param converterFrom function that accepts a value expressed in the
- * unitlike form's base and returns that value expressed
- * in this unitlike form.
- * @param converterTo function that accepts a value expressed in the
- * unitlike form and returns that value expressed in the
- * unit's base.
- * @return a unitlike form that uses the provided functions to convert.
- * @since 2020-09-07
- * @throws NullPointerException if any argument is null
- */
- public static final <W> Unitlike<W> fromConversionFunctions(
- final ObjectProduct<BaseUnit> base,
- final DoubleFunction<W> converterFrom,
- final ToDoubleFunction<W> converterTo) {
- return new FunctionalUnitlike<>(base, NameSymbol.EMPTY, converterFrom,
- converterTo);
- }
-
- /**
- * Returns a unitlike form from its base and the functions it uses to convert
- * to and from its base.
- *
- * @param base unitlike form's base
- * @param converterFrom function that accepts a value expressed in the
- * unitlike form's base and returns that value expressed
- * in this unitlike form.
- * @param converterTo function that accepts a value expressed in the
- * unitlike form and returns that value expressed in the
- * unit's base.
- * @param ns names and symbol of unit
- * @return a unitlike form that uses the provided functions to convert.
- * @since 2020-09-07
- * @throws NullPointerException if any argument is null
- */
- public static final <W> Unitlike<W> fromConversionFunctions(
- final ObjectProduct<BaseUnit> base,
- final DoubleFunction<W> converterFrom,
- final ToDoubleFunction<W> converterTo, final NameSymbol ns) {
- return new FunctionalUnitlike<>(base, ns, converterFrom, converterTo);
- }
-
- /**
- * The combination of units that this unit is based on.
- *
- * @since 2019-10-16
- */
- private final ObjectProduct<BaseUnit> unitBase;
-
- /**
- * This unit's name(s) and symbol
- *
- * @since 2020-09-07
- */
- private final NameSymbol nameSymbol;
-
- /**
- * Cache storing the result of getDimension()
- *
- * @since 2019-10-16
- */
- private transient ObjectProduct<BaseDimension> dimension = null;
-
- /**
- * @param unitBase
- * @since 2020-09-07
- */
- protected Unitlike(ObjectProduct<BaseUnit> unitBase, NameSymbol ns) {
- this.unitBase = Objects.requireNonNull(unitBase,
- "unitBase may not be null");
- this.nameSymbol = Objects.requireNonNull(ns, "ns may not be null");
- }
-
- /**
- * Checks if a value expressed in this unitlike form can be converted to a
- * value expressed in {@code other}
- *
- * @param other unit or unitlike form to test with
- * @return true if they are compatible
- * @since 2019-01-13
- * @since v0.1.0
- * @throws NullPointerException if other is null
- */
- public final boolean canConvertTo(final Unit other) {
- Objects.requireNonNull(other, "other must not be null.");
- return Objects.equals(this.getBase(), other.getBase());
- }
-
- /**
- * Checks if a value expressed in this unitlike form can be converted to a
- * value expressed in {@code other}
- *
- * @param other unit or unitlike form to test with
- * @return true if they are compatible
- * @since 2019-01-13
- * @since v0.1.0
- * @throws NullPointerException if other is null
- */
- public final <W> boolean canConvertTo(final Unitlike<W> other) {
- Objects.requireNonNull(other, "other must not be null.");
- return Objects.equals(this.getBase(), other.getBase());
- }
-
- protected abstract V convertFromBase(double value);
-
- /**
- * Converts a value expressed in this unitlike form to a value expressed in
- * {@code other}.
- *
- * @implSpec If conversion is possible, this implementation returns
- * {@code other.convertFromBase(this.convertToBase(value))}.
- * Therefore, overriding either of those methods will change the
- * output of this method.
- *
- * @param other unit to convert to
- * @param value value to convert
- * @return converted value
- * @since 2019-05-22
- * @throws IllegalArgumentException if {@code other} is incompatible for
- * conversion with this unitlike form (as
- * tested by {@link Unit#canConvertTo}).
- * @throws NullPointerException if other is null
- */
- public final double convertTo(final Unit other, final V value) {
- Objects.requireNonNull(other, "other must not be null.");
- if (this.canConvertTo(other))
- return other.convertFromBase(this.convertToBase(value));
- else
- throw new IllegalArgumentException(
- String.format("Cannot convert from %s to %s.", this, other));
- }
-
- /**
- * Converts a value expressed in this unitlike form to a value expressed in
- * {@code other}.
- *
- * @implSpec If conversion is possible, this implementation returns
- * {@code other.convertFromBase(this.convertToBase(value))}.
- * Therefore, overriding either of those methods will change the
- * output of this method.
- *
- * @param other unitlike form to convert to
- * @param value value to convert
- * @param <W> type of value to convert to
- * @return converted value
- * @since 2020-09-07
- * @throws IllegalArgumentException if {@code other} is incompatible for
- * conversion with this unitlike form (as
- * tested by {@link Unit#canConvertTo}).
- * @throws NullPointerException if other is null
- */
- public final <W> W convertTo(final Unitlike<W> other, final V value) {
- Objects.requireNonNull(other, "other must not be null.");
- if (this.canConvertTo(other))
- return other.convertFromBase(this.convertToBase(value));
- else
- throw new IllegalArgumentException(
- String.format("Cannot convert from %s to %s.", this, other));
- }
-
- protected abstract double convertToBase(V value);
-
- /**
- * @return combination of units that this unit is based on
- * @since 2018-12-22
- * @since v0.1.0
- */
- public final ObjectProduct<BaseUnit> getBase() {
- return this.unitBase;
- }
-
- /**
- * @return dimension measured by this unit
- * @since 2018-12-22
- * @since v0.1.0
- */
- public final ObjectProduct<BaseDimension> getDimension() {
- if (this.dimension == null) {
- final Map<BaseUnit, Integer> mapping = this.unitBase.exponentMap();
- final Map<BaseDimension, Integer> dimensionMap = new HashMap<>();
-
- for (final BaseUnit key : mapping.keySet()) {
- dimensionMap.put(key.getBaseDimension(), mapping.get(key));
- }
-
- this.dimension = ObjectProduct.fromExponentMapping(dimensionMap);
- }
- return this.dimension;
- }
-
- /**
- * @return the nameSymbol
- * @since 2020-09-07
- */
- @Override
- public final NameSymbol getNameSymbol() {
- return this.nameSymbol;
- }
-
- @Override
- public String toString() {
- return this.getPrimaryName().orElse("Unnamed unitlike form")
- + (this.getSymbol().isPresent()
- ? String.format(" (%s)", this.getSymbol().get())
- : "")
- + ", derived from "
- + this.getBase().toString(u -> u.getSymbol().get())
- + (this.getOtherNames().isEmpty() ? ""
- : ", also called " + String.join(", ", this.getOtherNames()));
- }
-
- /**
- * @param ns name(s) and symbol to use
- * @return a copy of this unitlike form with provided name(s) and symbol
- * @since 2020-09-07
- * @throws NullPointerException if ns is null
- */
- public Unitlike<V> withName(final NameSymbol ns) {
- return fromConversionFunctions(this.getBase(), this::convertFromBase,
- this::convertToBase,
- Objects.requireNonNull(ns, "ns must not be null."));
- }
-}
diff --git a/src/main/java/sevenUnits/unit/UnitlikeValue.java b/src/main/java/sevenUnits/unit/UnitlikeValue.java
deleted file mode 100644
index ad0d1ea..0000000
--- a/src/main/java/sevenUnits/unit/UnitlikeValue.java
+++ /dev/null
@@ -1,176 +0,0 @@
-/**
- * Copyright (C) 2020 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-package sevenUnits.unit;
-
-import java.util.Optional;
-
-import sevenUnits.utils.NameSymbol;
-
-/**
- *
- * @since 2020-09-07
- */
-final class UnitlikeValue<T extends Unitlike<V>, V> {
- /**
- * Gets a {@code UnitlikeValue<V>}.
- *
- * @since 2020-10-02
- */
- public static <T extends Unitlike<V>, V> UnitlikeValue<T, V> of(T unitlike,
- V value) {
- return new UnitlikeValue<>(unitlike, value);
- }
-
- private final T unitlike;
- private final V value;
-
- /**
- * @param unitlike
- * @param value
- * @since 2020-09-07
- */
- private UnitlikeValue(T unitlike, V value) {
- this.unitlike = unitlike;
- this.value = value;
- }
-
- /**
- * @return true if this value can be converted to {@code other}.
- * @since 2020-10-01
- */
- public final boolean canConvertTo(Unit other) {
- return this.unitlike.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) {
- return this.unitlike.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.unitlike.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) {
- return UnitValue.of(other,
- this.unitlike.convertTo(other, this.getValue()));
- }
-
- /**
- * Returns this unit value represented as a {@code 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.
- * @since 2020-09-29
- */
- public final LinearUnitValue convertToBase(NameSymbol ns) {
- final LinearUnit base = LinearUnit.getBase(this.unitlike).withName(ns);
- return this.convertToLinear(base);
- }
-
- /**
- * @return a {@code LinearUnitValue} that is equivalent to this value. It
- * will have zero uncertainty.
- * @since 2020-09-29
- */
- public final LinearUnitValue convertToLinear(LinearUnit other) {
- return LinearUnitValue.getExact(other,
- this.getUnitlike().convertTo(other, this.getValue()));
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!(obj instanceof UnitlikeValue))
- return false;
- final UnitlikeValue<?, ?> other = (UnitlikeValue<?, ?>) obj;
- if (this.getUnitlike() == null) {
- if (other.getUnitlike() != null)
- return false;
- } else if (!this.getUnitlike().equals(other.getUnitlike()))
- return false;
- if (this.getValue() == null) {
- if (other.getValue() != null)
- return false;
- } else if (!this.getValue().equals(other.getValue()))
- return false;
- return true;
- }
-
- /**
- * @return the unitlike
- * @since 2020-09-29
- */
- public final Unitlike<V> getUnitlike() {
- return this.unitlike;
- }
-
- /**
- * @return the value
- * @since 2020-09-29
- */
- public final V getValue() {
- return this.value;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result
- + (this.getUnitlike() == null ? 0 : this.getUnitlike().hashCode());
- result = prime * result
- + (this.getValue() == null ? 0 : this.getValue().hashCode());
- return result;
- }
-
- @Override
- public String toString() {
- final Optional<String> primaryName = this.getUnitlike().getPrimaryName();
- final Optional<String> symbol = this.getUnitlike().getSymbol();
- if (primaryName.isEmpty() && symbol.isEmpty()) {
- final double baseValue = this.getUnitlike()
- .convertToBase(this.getValue());
- return String.format("%s unnamed unit (= %s %s)", this.getValue(),
- baseValue, this.getUnitlike().getBase());
- } else {
- final String unitName = symbol.orElse(primaryName.get());
- return this.getValue() + " " + unitName;
- }
- }
-}
diff --git a/src/test/java/sevenUnits/unit/MultiUnitTest.java b/src/test/java/sevenUnits/unit/MultiUnitTest.java
deleted file mode 100644
index 949a1f1..0000000
--- a/src/test/java/sevenUnits/unit/MultiUnitTest.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/**
- * Copyright (C) 2020 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-package sevenUnits.unit;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Random;
-import java.util.concurrent.ThreadLocalRandom;
-
-import org.junit.jupiter.api.Test;
-
-/**
- * Tests related to the {@code MultiUnit}.
- *
- * @since 2020-10-03
- */
-class MultiUnitTest {
-
- /**
- * Ensures that the {@code MultiUnit} can convert properly.
- */
- @Test
- final void testConvert() {
- final Random rng = ThreadLocalRandom.current();
- final MultiUnit footInch = MultiUnit.of(BritishImperial.Length.FOOT,
- BritishImperial.Length.INCH);
-
- assertEquals(1702.0,
- footInch.convertTo(Metric.METRE.withPrefix(Metric.MILLI),
- Arrays.asList(5.0, 7.0)),
- 1.0);
-
- for (int i = 0; i < 1000; i++) {
- final double feet = rng.nextInt(1000);
- final double inches = rng.nextDouble() * 12;
- final double millimetres = feet * 304.8 + inches * 25.4;
-
- final List<Double> feetAndInches = Metric.METRE
- .withPrefix(Metric.MILLI).convertTo(footInch, millimetres);
- assertEquals(feet, feetAndInches.get(0), 1e-10);
- assertEquals(inches, feetAndInches.get(1), 1e-10);
- }
- }
-
- /**
- * Test method for {@link sevenUnits.unit.MultiUnit#convertFromBase(double)}.
- */
- @Test
- final void testConvertFromBase() {
- final Random rng = ThreadLocalRandom.current();
- final MultiUnit footInch = MultiUnit.of(BritishImperial.Length.FOOT,
- BritishImperial.Length.INCH);
-
- // 1.7 m =~ 5' + 7"
- final List<Double> values = footInch.convertFromBase(1.7018);
-
- assertEquals(5, values.get(0));
- assertEquals(7, values.get(1), 1e-12);
-
- for (int i = 0; i < 1000; i++) {
- final double feet = rng.nextInt(1000);
- final double inches = rng.nextDouble() * 12;
- final double metres = feet * 0.3048 + inches * 0.0254;
-
- final List<Double> feetAndInches = footInch.convertFromBase(metres);
- assertEquals(feet, feetAndInches.get(0), 1e-10);
- assertEquals(inches, feetAndInches.get(1), 1e-10);
- }
- }
-
- /**
- * Test method for
- * {@link sevenUnits.unit.MultiUnit#convertToBase(java.util.List)}.
- */
- @Test
- final void testConvertToBase() {
- final Random rng = ThreadLocalRandom.current();
- final MultiUnit footInch = MultiUnit.of(BritishImperial.Length.FOOT,
- BritishImperial.Length.INCH);
-
- // 1.7 m =~ 5' + 7"
- assertEquals(1.7018, footInch.convertToBase(Arrays.asList(5.0, 7.0)),
- 1e-12);
-
- for (int i = 0; i < 1000; i++) {
- final double feet = rng.nextInt(1000);
- final double inches = rng.nextDouble() * 12;
- final double metres = feet * 0.3048 + inches * 0.0254;
-
- assertEquals(metres,
- footInch.convertToBase(Arrays.asList(feet, inches)), 1e-12);
- }
- }
-}