summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/unit
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/sevenUnits/unit')
-rw-r--r--src/main/java/sevenUnits/unit/BaseDimension.java51
-rw-r--r--src/main/java/sevenUnits/unit/BaseUnit.java2
-rw-r--r--src/main/java/sevenUnits/unit/BritishImperial.java4
-rw-r--r--src/main/java/sevenUnits/unit/FunctionalUnit.java1
-rw-r--r--src/main/java/sevenUnits/unit/FunctionalUnitlike.java1
-rw-r--r--src/main/java/sevenUnits/unit/LinearUnit.java22
-rw-r--r--src/main/java/sevenUnits/unit/LinearUnitValue.java10
-rw-r--r--src/main/java/sevenUnits/unit/Metric.java133
-rw-r--r--src/main/java/sevenUnits/unit/MultiUnit.java1
-rw-r--r--src/main/java/sevenUnits/unit/NameSymbol.java280
-rw-r--r--src/main/java/sevenUnits/unit/Nameable.java59
-rw-r--r--src/main/java/sevenUnits/unit/Unit.java44
-rw-r--r--src/main/java/sevenUnits/unit/UnitDatabase.java67
-rw-r--r--src/main/java/sevenUnits/unit/UnitPrefix.java136
-rw-r--r--src/main/java/sevenUnits/unit/UnitType.java58
-rw-r--r--src/main/java/sevenUnits/unit/UnitValue.java2
-rw-r--r--src/main/java/sevenUnits/unit/Unitlike.java2
-rw-r--r--src/main/java/sevenUnits/unit/UnitlikeValue.java2
18 files changed, 322 insertions, 553 deletions
diff --git a/src/main/java/sevenUnits/unit/BaseDimension.java b/src/main/java/sevenUnits/unit/BaseDimension.java
index d5e98ca..820d48c 100644
--- a/src/main/java/sevenUnits/unit/BaseDimension.java
+++ b/src/main/java/sevenUnits/unit/BaseDimension.java
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2019 Adrien Hopkins
+ * Copyright (C) 2019, 2022 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
@@ -18,70 +18,61 @@ package sevenUnits.unit;
import java.util.Objects;
+import sevenUnits.utils.NameSymbol;
+import sevenUnits.utils.Nameable;
+
/**
* A dimension that defines a {@code BaseUnit}
*
* @author Adrien Hopkins
* @since 2019-10-16
*/
-public final class BaseDimension {
+public final class BaseDimension implements Nameable {
/**
* Gets a {@code BaseDimension} with the provided name and symbol.
*
- * @param name
- * name of dimension
- * @param symbol
- * symbol used for dimension
+ * @param name name of dimension
+ * @param symbol symbol used for dimension
* @return dimension
* @since 2019-10-16
*/
public static BaseDimension valueOf(final String name, final String symbol) {
return new BaseDimension(name, symbol);
}
-
+
/**
* The name of the dimension.
*/
private final String name;
/**
- * The symbol used by the dimension. Symbols should be short, generally one or two characters.
+ * The symbol used by the dimension. Symbols should be short, generally one
+ * or two characters.
*/
private final String symbol;
-
+
/**
* Creates the {@code BaseDimension}.
*
- * @param name
- * name of unit
- * @param symbol
- * symbol of unit
- * @throws NullPointerException
- * if any argument is null
+ * @param name name of unit
+ * @param symbol symbol of unit
+ * @throws NullPointerException if any argument is null
* @since 2019-10-16
*/
private BaseDimension(final String name, final String symbol) {
this.name = Objects.requireNonNull(name, "name must not be null.");
this.symbol = Objects.requireNonNull(symbol, "symbol must not be null.");
}
-
- /**
- * @return name
- * @since 2019-10-16
- */
- public final String getName() {
- return this.name;
- }
-
+
/**
- * @return symbol
- * @since 2019-10-16
+ * @since v0.4.0
*/
- public final String getSymbol() {
- return this.symbol;
+ @Override
+ public NameSymbol getNameSymbol() {
+ return NameSymbol.of(this.name, this.symbol);
}
-
+
@Override
public String toString() {
- return String.format("%s (%s)", this.getName(), this.getSymbol());
+ return String.format("%s (%s)", this.name, this.symbol);
}
}
diff --git a/src/main/java/sevenUnits/unit/BaseUnit.java b/src/main/java/sevenUnits/unit/BaseUnit.java
index ee2c277..dba7f52 100644
--- a/src/main/java/sevenUnits/unit/BaseUnit.java
+++ b/src/main/java/sevenUnits/unit/BaseUnit.java
@@ -20,6 +20,8 @@ import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
+import sevenUnits.utils.NameSymbol;
+
/**
* A unit that other units are defined by.
* <p>
diff --git a/src/main/java/sevenUnits/unit/BritishImperial.java b/src/main/java/sevenUnits/unit/BritishImperial.java
index 743beeb..0ecba6d 100644
--- a/src/main/java/sevenUnits/unit/BritishImperial.java
+++ b/src/main/java/sevenUnits/unit/BritishImperial.java
@@ -16,6 +16,8 @@
*/
package sevenUnits.unit;
+import sevenUnits.utils.NameSymbol;
+
/**
* A static utility class that contains units in the British Imperial system.
*
@@ -119,5 +121,5 @@ public final class BritishImperial {
public static final Unit FAHRENHEIT = Unit
.fromConversionFunctions(Metric.KELVIN.getBase(),
tempK -> tempK * 1.8 - 459.67, tempF -> (tempF + 459.67) / 1.8)
- .withName(NameSymbol.of("degrees Fahrenheit", "\u00B0F"));
+ .withName(NameSymbol.of("degree Fahrenheit", "\u00B0F"));
}
diff --git a/src/main/java/sevenUnits/unit/FunctionalUnit.java b/src/main/java/sevenUnits/unit/FunctionalUnit.java
index df457e4..720b0af 100644
--- a/src/main/java/sevenUnits/unit/FunctionalUnit.java
+++ b/src/main/java/sevenUnits/unit/FunctionalUnit.java
@@ -19,6 +19,7 @@ package sevenUnits.unit;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
+import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.ObjectProduct;
/**
diff --git a/src/main/java/sevenUnits/unit/FunctionalUnitlike.java b/src/main/java/sevenUnits/unit/FunctionalUnitlike.java
index 2ee9e19..d6046c0 100644
--- a/src/main/java/sevenUnits/unit/FunctionalUnitlike.java
+++ b/src/main/java/sevenUnits/unit/FunctionalUnitlike.java
@@ -19,6 +19,7 @@ package sevenUnits.unit;
import java.util.function.DoubleFunction;
import java.util.function.ToDoubleFunction;
+import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.ObjectProduct;
/**
diff --git a/src/main/java/sevenUnits/unit/LinearUnit.java b/src/main/java/sevenUnits/unit/LinearUnit.java
index 25c2e2e..103b7f6 100644
--- a/src/main/java/sevenUnits/unit/LinearUnit.java
+++ b/src/main/java/sevenUnits/unit/LinearUnit.java
@@ -19,6 +19,7 @@ package sevenUnits.unit;
import java.util.Objects;
import sevenUnits.utils.DecimalComparison;
+import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.ObjectProduct;
import sevenUnits.utils.UncertainDouble;
@@ -369,6 +370,13 @@ public final class LinearUnit extends Unit {
this.getConversionFactor() * multiplier.getConversionFactor());
}
+ @Override
+ public String toDefinitionString() {
+ return Double.toString(this.conversionFactor)
+ + (this.getBase().equals(ObjectProduct.empty()) ? ""
+ : " " + this.getBase().toString(BaseUnit::getShortName));
+ }
+
/**
* Returns this unit but to an exponent.
*
@@ -382,20 +390,6 @@ public final class LinearUnit extends Unit {
Math.pow(this.conversionFactor, exponent));
}
- /**
- * @return a string providing a definition of this unit
- * @since 2019-10-21
- */
- @Override
- public String toString() {
- return this.getPrimaryName().orElse("Unnamed unit")
- + (this.getSymbol().isPresent()
- ? String.format(" (%s)", this.getSymbol().get())
- : "")
- + ", " + Double.toString(this.conversionFactor) + " * "
- + this.getBase().toString(u -> u.getSymbol().get());
- }
-
@Override
public LinearUnit withName(final NameSymbol ns) {
return valueOf(this.getBase(), this.getConversionFactor(), ns);
diff --git a/src/main/java/sevenUnits/unit/LinearUnitValue.java b/src/main/java/sevenUnits/unit/LinearUnitValue.java
index a50e1f5..f91d30b 100644
--- a/src/main/java/sevenUnits/unit/LinearUnitValue.java
+++ b/src/main/java/sevenUnits/unit/LinearUnitValue.java
@@ -16,6 +16,7 @@
*/
package sevenUnits.unit;
+import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;
@@ -300,7 +301,7 @@ public final class LinearUnitValue {
@Override
public String toString() {
- return this.toString(!this.value.isExact());
+ return this.toString(!this.value.isExact(), RoundingMode.HALF_EVEN);
}
/**
@@ -315,7 +316,8 @@ public final class LinearUnitValue {
*
* @since 2020-07-26
*/
- public String toString(final boolean showUncertainty) {
+ public String toString(final boolean showUncertainty,
+ RoundingMode roundingMode) {
final Optional<String> primaryName = this.unit.getPrimaryName();
final Optional<String> symbol = this.unit.getSymbol();
final String chosenName = symbol.orElse(primaryName.orElse(null));
@@ -325,10 +327,10 @@ public final class LinearUnitValue {
// get rounded strings
// if showUncertainty is true, add brackets around the string
final String valueString = (showUncertainty ? "(" : "")
- + this.value.toString(showUncertainty)
+ + this.value.toString(showUncertainty, roundingMode)
+ (showUncertainty ? ")" : "");
final String baseValueString = (showUncertainty ? "(" : "")
- + baseValue.toString(showUncertainty)
+ + baseValue.toString(showUncertainty, roundingMode)
+ (showUncertainty ? ")" : "");
// create string
diff --git a/src/main/java/sevenUnits/unit/Metric.java b/src/main/java/sevenUnits/unit/Metric.java
index 3c4d291..05e82ba 100644
--- a/src/main/java/sevenUnits/unit/Metric.java
+++ b/src/main/java/sevenUnits/unit/Metric.java
@@ -18,6 +18,7 @@ package sevenUnits.unit;
import java.util.Set;
+import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.ObjectProduct;
/**
@@ -114,23 +115,30 @@ public final class Metric {
public static final ObjectProduct<BaseDimension> EMPTY = ObjectProduct
.empty();
public static final ObjectProduct<BaseDimension> LENGTH = ObjectProduct
- .oneOf(BaseDimensions.LENGTH);
+ .oneOf(BaseDimensions.LENGTH)
+ .withName(NameSymbol.of("Length", "L"));
public static final ObjectProduct<BaseDimension> MASS = ObjectProduct
- .oneOf(BaseDimensions.MASS);
+ .oneOf(BaseDimensions.MASS).withName(NameSymbol.of("Mass", "M"));
public static final ObjectProduct<BaseDimension> TIME = ObjectProduct
- .oneOf(BaseDimensions.TIME);
+ .oneOf(BaseDimensions.TIME).withName(NameSymbol.of("Time", "T"));
public static final ObjectProduct<BaseDimension> ELECTRIC_CURRENT = ObjectProduct
- .oneOf(BaseDimensions.ELECTRIC_CURRENT);
+ .oneOf(BaseDimensions.ELECTRIC_CURRENT)
+ .withName(NameSymbol.of("Current", "I"));
public static final ObjectProduct<BaseDimension> TEMPERATURE = ObjectProduct
- .oneOf(BaseDimensions.TEMPERATURE);
+ .oneOf(BaseDimensions.TEMPERATURE)
+ .withName(NameSymbol.of("Temperature", "\u0398"));
public static final ObjectProduct<BaseDimension> QUANTITY = ObjectProduct
- .oneOf(BaseDimensions.QUANTITY);
+ .oneOf(BaseDimensions.QUANTITY)
+ .withName(NameSymbol.of("Quantity", "N"));
public static final ObjectProduct<BaseDimension> LUMINOUS_INTENSITY = ObjectProduct
- .oneOf(BaseDimensions.LUMINOUS_INTENSITY);
+ .oneOf(BaseDimensions.LUMINOUS_INTENSITY)
+ .withName(NameSymbol.of("Luminous Intensity", "J"));
public static final ObjectProduct<BaseDimension> INFORMATION = ObjectProduct
- .oneOf(BaseDimensions.INFORMATION);
+ .oneOf(BaseDimensions.INFORMATION)
+ .withName(NameSymbol.ofName("Information"));
public static final ObjectProduct<BaseDimension> CURRENCY = ObjectProduct
- .oneOf(BaseDimensions.CURRENCY);
+ .oneOf(BaseDimensions.CURRENCY)
+ .withName(NameSymbol.ofName("Currency"));
// derived dimensions without named SI units
public static final ObjectProduct<BaseDimension> AREA = LENGTH
@@ -138,7 +146,7 @@ public final class Metric {
public static final ObjectProduct<BaseDimension> VOLUME = AREA
.times(LENGTH);
public static final ObjectProduct<BaseDimension> VELOCITY = LENGTH
- .dividedBy(TIME);
+ .dividedBy(TIME).withName(NameSymbol.ofName("Velocity"));
public static final ObjectProduct<BaseDimension> ACCELERATION = VELOCITY
.dividedBy(TIME);
public static final ObjectProduct<BaseDimension> WAVENUMBER = EMPTY
@@ -402,54 +410,89 @@ public final class Metric {
.withName(NameSymbol.of("exbi", "Ei"));
// a few prefixed units
- public static final LinearUnit MICROMETRE = Metric.METRE.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLIMETRE = Metric.METRE.withPrefix(Metric.MILLI);
- public static final LinearUnit KILOMETRE = Metric.METRE.withPrefix(Metric.KILO);
- public static final LinearUnit MEGAMETRE = Metric.METRE.withPrefix(Metric.MEGA);
+ public static final LinearUnit MICROMETRE = Metric.METRE
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLIMETRE = Metric.METRE
+ .withPrefix(Metric.MILLI);
+ public static final LinearUnit KILOMETRE = Metric.METRE
+ .withPrefix(Metric.KILO);
+ public static final LinearUnit MEGAMETRE = Metric.METRE
+ .withPrefix(Metric.MEGA);
- public static final LinearUnit MICROLITRE = Metric.LITRE.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLILITRE = Metric.LITRE.withPrefix(Metric.MILLI);
- public static final LinearUnit KILOLITRE = Metric.LITRE.withPrefix(Metric.KILO);
- public static final LinearUnit MEGALITRE = Metric.LITRE.withPrefix(Metric.MEGA);
+ public static final LinearUnit MICROLITRE = Metric.LITRE
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLILITRE = Metric.LITRE
+ .withPrefix(Metric.MILLI);
+ public static final LinearUnit KILOLITRE = Metric.LITRE
+ .withPrefix(Metric.KILO);
+ public static final LinearUnit MEGALITRE = Metric.LITRE
+ .withPrefix(Metric.MEGA);
- public static final LinearUnit MICROSECOND = Metric.SECOND.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLISECOND = Metric.SECOND.withPrefix(Metric.MILLI);
- public static final LinearUnit KILOSECOND = Metric.SECOND.withPrefix(Metric.KILO);
- public static final LinearUnit MEGASECOND = Metric.SECOND.withPrefix(Metric.MEGA);
+ public static final LinearUnit MICROSECOND = Metric.SECOND
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLISECOND = Metric.SECOND
+ .withPrefix(Metric.MILLI);
+ public static final LinearUnit KILOSECOND = Metric.SECOND
+ .withPrefix(Metric.KILO);
+ public static final LinearUnit MEGASECOND = Metric.SECOND
+ .withPrefix(Metric.MEGA);
- public static final LinearUnit MICROGRAM = Metric.GRAM.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLIGRAM = Metric.GRAM.withPrefix(Metric.MILLI);
- public static final LinearUnit MEGAGRAM = Metric.GRAM.withPrefix(Metric.MEGA);
+ public static final LinearUnit MICROGRAM = Metric.GRAM
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLIGRAM = Metric.GRAM
+ .withPrefix(Metric.MILLI);
+ public static final LinearUnit MEGAGRAM = Metric.GRAM
+ .withPrefix(Metric.MEGA);
- public static final LinearUnit MICRONEWTON = Metric.NEWTON.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLINEWTON = Metric.NEWTON.withPrefix(Metric.MILLI);
- public static final LinearUnit KILONEWTON = Metric.NEWTON.withPrefix(Metric.KILO);
- public static final LinearUnit MEGANEWTON = Metric.NEWTON.withPrefix(Metric.MEGA);
+ public static final LinearUnit MICRONEWTON = Metric.NEWTON
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLINEWTON = Metric.NEWTON
+ .withPrefix(Metric.MILLI);
+ public static final LinearUnit KILONEWTON = Metric.NEWTON
+ .withPrefix(Metric.KILO);
+ public static final LinearUnit MEGANEWTON = Metric.NEWTON
+ .withPrefix(Metric.MEGA);
- public static final LinearUnit MICROJOULE = Metric.JOULE.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLIJOULE = Metric.JOULE.withPrefix(Metric.MILLI);
- public static final LinearUnit KILOJOULE = Metric.JOULE.withPrefix(Metric.KILO);
- public static final LinearUnit MEGAJOULE = Metric.JOULE.withPrefix(Metric.MEGA);
+ public static final LinearUnit MICROJOULE = Metric.JOULE
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLIJOULE = Metric.JOULE
+ .withPrefix(Metric.MILLI);
+ public static final LinearUnit KILOJOULE = Metric.JOULE
+ .withPrefix(Metric.KILO);
+ public static final LinearUnit MEGAJOULE = Metric.JOULE
+ .withPrefix(Metric.MEGA);
- public static final LinearUnit MICROWATT = Metric.WATT.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLIWATT = Metric.WATT.withPrefix(Metric.MILLI);
- public static final LinearUnit KILOWATT = Metric.WATT.withPrefix(Metric.KILO);
- public static final LinearUnit MEGAWATT = Metric.WATT.withPrefix(Metric.MEGA);
+ public static final LinearUnit MICROWATT = Metric.WATT
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLIWATT = Metric.WATT
+ .withPrefix(Metric.MILLI);
+ public static final LinearUnit KILOWATT = Metric.WATT
+ .withPrefix(Metric.KILO);
+ public static final LinearUnit MEGAWATT = Metric.WATT
+ .withPrefix(Metric.MEGA);
public static final LinearUnit MICROCOULOMB = Metric.COULOMB
.withPrefix(Metric.MICRO);
public static final LinearUnit MILLICOULOMB = Metric.COULOMB
.withPrefix(Metric.MILLI);
- public static final LinearUnit KILOCOULOMB = Metric.COULOMB.withPrefix(Metric.KILO);
- public static final LinearUnit MEGACOULOMB = Metric.COULOMB.withPrefix(Metric.MEGA);
+ public static final LinearUnit KILOCOULOMB = Metric.COULOMB
+ .withPrefix(Metric.KILO);
+ public static final LinearUnit MEGACOULOMB = Metric.COULOMB
+ .withPrefix(Metric.MEGA);
- public static final LinearUnit MICROAMPERE = Metric.AMPERE.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLIAMPERE = Metric.AMPERE.withPrefix(Metric.MILLI);
+ public static final LinearUnit MICROAMPERE = Metric.AMPERE
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLIAMPERE = Metric.AMPERE
+ .withPrefix(Metric.MILLI);
- public static final LinearUnit MICROVOLT = Metric.VOLT.withPrefix(Metric.MICRO);
- public static final LinearUnit MILLIVOLT = Metric.VOLT.withPrefix(Metric.MILLI);
- public static final LinearUnit KILOVOLT = Metric.VOLT.withPrefix(Metric.KILO);
- public static final LinearUnit MEGAVOLT = Metric.VOLT.withPrefix(Metric.MEGA);
+ public static final LinearUnit MICROVOLT = Metric.VOLT
+ .withPrefix(Metric.MICRO);
+ public static final LinearUnit MILLIVOLT = Metric.VOLT
+ .withPrefix(Metric.MILLI);
+ public static final LinearUnit KILOVOLT = Metric.VOLT
+ .withPrefix(Metric.KILO);
+ public static final LinearUnit MEGAVOLT = Metric.VOLT
+ .withPrefix(Metric.MEGA);
public static final LinearUnit KILOOHM = Metric.OHM.withPrefix(Metric.KILO);
public static final LinearUnit MEGAOHM = Metric.OHM.withPrefix(Metric.MEGA);
diff --git a/src/main/java/sevenUnits/unit/MultiUnit.java b/src/main/java/sevenUnits/unit/MultiUnit.java
index 83cdb03..bc240e3 100644
--- a/src/main/java/sevenUnits/unit/MultiUnit.java
+++ b/src/main/java/sevenUnits/unit/MultiUnit.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.ObjectProduct;
/**
diff --git a/src/main/java/sevenUnits/unit/NameSymbol.java b/src/main/java/sevenUnits/unit/NameSymbol.java
deleted file mode 100644
index 3e26138..0000000
--- a/src/main/java/sevenUnits/unit/NameSymbol.java
+++ /dev/null
@@ -1,280 +0,0 @@
-/**
- * Copyright (C) 2019 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.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Objects;
-import java.util.Optional;
-import java.util.Set;
-
-/**
- * A class that can be used to specify names and a symbol for a unit.
- *
- * @author Adrien Hopkins
- * @since 2019-10-21
- */
-public final class NameSymbol {
- public static final NameSymbol EMPTY = new NameSymbol(Optional.empty(),
- Optional.empty(), new HashSet<>());
-
- /**
- * Creates a {@code NameSymbol}, ensuring that if primaryName is null and
- * otherNames is not empty, one name is moved from otherNames to primaryName
- *
- * Ensure that otherNames is a copy of the inputted argument.
- */
- private static final NameSymbol create(final String name,
- final String symbol, final Set<String> otherNames) {
- final Optional<String> primaryName;
-
- if (name == null && !otherNames.isEmpty()) {
- // get primary name and remove it from savedNames
- final Iterator<String> it = otherNames.iterator();
- assert it.hasNext();
- primaryName = Optional.of(it.next());
- otherNames.remove(primaryName.get());
- } else {
- primaryName = Optional.ofNullable(name);
- }
-
- return new NameSymbol(primaryName, Optional.ofNullable(symbol),
- otherNames);
- }
-
- /**
- * Gets a {@code NameSymbol} with a primary name, a symbol and no other
- * names.
- *
- * @param name name to use
- * @param symbol symbol to use
- * @return NameSymbol instance
- * @since 2019-10-21
- * @throws NullPointerException if name or symbol is null
- */
- public static final NameSymbol of(final String name, final String symbol) {
- return new NameSymbol(Optional.of(name), Optional.of(symbol),
- new HashSet<>());
- }
-
- /**
- * Gets a {@code NameSymbol} with a primary name, a symbol and additional
- * names.
- *
- * @param name name to use
- * @param symbol symbol to use
- * @param otherNames other names to use
- * @return NameSymbol instance
- * @since 2019-10-21
- * @throws NullPointerException if any argument is null
- */
- public static final NameSymbol of(final String name, final String symbol,
- final Set<String> otherNames) {
- return new NameSymbol(Optional.of(name), Optional.of(symbol),
- new HashSet<>(Objects.requireNonNull(otherNames,
- "otherNames must not be null.")));
- }
-
- /**
- * h * Gets a {@code NameSymbol} with a primary name, a symbol and additional
- * names.
- *
- * @param name name to use
- * @param symbol symbol to use
- * @param otherNames other names to use
- * @return NameSymbol instance
- * @since 2019-10-21
- * @throws NullPointerException if any argument is null
- */
- public static final NameSymbol of(final String name, final String symbol,
- final String... otherNames) {
- return new NameSymbol(Optional.of(name), Optional.of(symbol),
- new HashSet<>(Arrays.asList(Objects.requireNonNull(otherNames,
- "otherNames must not be null."))));
- }
-
- /**
- * Gets a {@code NameSymbol} with a primary name, no symbol, and no other
- * names.
- *
- * @param name name to use
- * @return NameSymbol instance
- * @since 2019-10-21
- * @throws NullPointerException if name is null
- */
- public static final NameSymbol ofName(final String name) {
- return new NameSymbol(Optional.of(name), Optional.empty(),
- new HashSet<>());
- }
-
- /**
- * Gets a {@code NameSymbol} with a primary name, a symbol and additional
- * names.
- * <p>
- * If any argument is null, this static factory replaces it with an empty
- * Optional or empty Set.
- * <p>
- * If {@code name} is null and {@code otherNames} is not empty, a primary
- * name will be picked from {@code otherNames}. This name will not appear in
- * getOtherNames().
- *
- * @param name name to use
- * @param symbol symbol to use
- * @param otherNames other names to use
- * @return NameSymbol instance
- * @since 2019-11-26
- */
- public static final NameSymbol ofNullable(final String name,
- final String symbol, final Set<String> otherNames) {
- return NameSymbol.create(name, symbol,
- otherNames == null ? new HashSet<>() : new HashSet<>(otherNames));
- }
-
- /**
- * h * Gets a {@code NameSymbol} with a primary name, a symbol and additional
- * names.
- * <p>
- * If any argument is null, this static factory replaces it with an empty
- * Optional or empty Set.
- * <p>
- * If {@code name} is null and {@code otherNames} is not empty, a primary
- * name will be picked from {@code otherNames}. This name will not appear in
- * getOtherNames().
- *
- * @param name name to use
- * @param symbol symbol to use
- * @param otherNames other names to use
- * @return NameSymbol instance
- * @since 2019-11-26
- */
- public static final NameSymbol ofNullable(final String name,
- final String symbol, final String... otherNames) {
- return create(name, symbol, otherNames == null ? new HashSet<>()
- : new HashSet<>(Arrays.asList(otherNames)));
- }
-
- /**
- * Gets a {@code NameSymbol} with a symbol and no names.
- *
- * @param symbol symbol to use
- * @return NameSymbol instance
- * @since 2019-10-21
- * @throws NullPointerException if symbol is null
- */
- public static final NameSymbol ofSymbol(final String symbol) {
- return new NameSymbol(Optional.empty(), Optional.of(symbol),
- new HashSet<>());
- }
-
- private final Optional<String> primaryName;
- private final Optional<String> symbol;
-
- private final Set<String> otherNames;
-
- /**
- * Creates the {@code NameSymbol}.
- *
- * @param primaryName primary name of unit
- * @param symbol symbol used to represent unit
- * @param otherNames other names and/or spellings, should be a mutable copy
- * of the argument
- * @since 2019-10-21
- */
- private NameSymbol(final Optional<String> primaryName,
- final Optional<String> symbol, final Set<String> otherNames) {
- this.primaryName = primaryName;
- this.symbol = symbol;
- otherNames.remove(null);
- this.otherNames = Collections.unmodifiableSet(otherNames);
-
- if (this.primaryName.isEmpty()) {
- assert this.otherNames.isEmpty();
- }
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!(obj instanceof NameSymbol))
- return false;
- final NameSymbol other = (NameSymbol) obj;
- if (this.otherNames == null) {
- if (other.otherNames != null)
- return false;
- } else if (!this.otherNames.equals(other.otherNames))
- return false;
- if (this.primaryName == null) {
- if (other.primaryName != null)
- return false;
- } else if (!this.primaryName.equals(other.primaryName))
- return false;
- if (this.symbol == null) {
- if (other.symbol != null)
- return false;
- } else if (!this.symbol.equals(other.symbol))
- return false;
- return true;
- }
-
- /**
- * @return otherNames
- * @since 2019-10-21
- */
- public final Set<String> getOtherNames() {
- return this.otherNames;
- }
-
- /**
- * @return primaryName
- * @since 2019-10-21
- */
- public final Optional<String> getPrimaryName() {
- return this.primaryName;
- }
-
- /**
- * @return symbol
- * @since 2019-10-21
- */
- public final Optional<String> getSymbol() {
- return this.symbol;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result
- + (this.otherNames == null ? 0 : this.otherNames.hashCode());
- result = prime * result
- + (this.primaryName == null ? 0 : this.primaryName.hashCode());
- result = prime * result
- + (this.symbol == null ? 0 : this.symbol.hashCode());
- return result;
- }
-
- /**
- * @return true iff this {@code NameSymbol} contains no names or symbols.
- */
- public final boolean isEmpty() {
- // if primaryName is empty, otherNames must also be empty
- return this.primaryName.isEmpty() && this.symbol.isEmpty();
- }
-} \ No newline at end of file
diff --git a/src/main/java/sevenUnits/unit/Nameable.java b/src/main/java/sevenUnits/unit/Nameable.java
deleted file mode 100644
index ed23687..0000000
--- a/src/main/java/sevenUnits/unit/Nameable.java
+++ /dev/null
@@ -1,59 +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 java.util.Set;
-
-/**
- * An object that can hold one or more names, and possibly a symbol. The name
- * and symbol data should be immutable.
- *
- * @since 2020-09-07
- */
-public interface Nameable {
- /**
- * @return a {@code NameSymbol} that contains this object's primary name,
- * symbol and other names
- * @since 2020-09-07
- */
- NameSymbol getNameSymbol();
-
- /**
- * @return set of alternate names
- * @since 2020-09-07
- */
- default Set<String> getOtherNames() {
- return this.getNameSymbol().getOtherNames();
- }
-
- /**
- * @return preferred name of object
- * @since 2020-09-07
- */
- default Optional<String> getPrimaryName() {
- return this.getNameSymbol().getPrimaryName();
- }
-
- /**
- * @return short symbol representing object
- * @since 2020-09-07
- */
- default Optional<String> getSymbol() {
- return this.getNameSymbol().getSymbol();
- }
-}
diff --git a/src/main/java/sevenUnits/unit/Unit.java b/src/main/java/sevenUnits/unit/Unit.java
index 005b6f7..14478ba 100644
--- a/src/main/java/sevenUnits/unit/Unit.java
+++ b/src/main/java/sevenUnits/unit/Unit.java
@@ -22,6 +22,8 @@ import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
import sevenUnits.utils.DecimalComparison;
+import sevenUnits.utils.NameSymbol;
+import sevenUnits.utils.Nameable;
import sevenUnits.utils.ObjectProduct;
/**
@@ -188,7 +190,7 @@ public abstract class Unit implements Nameable {
*
* @implSpec This method is used by {@link #convertTo}, and its behaviour
* affects the behaviour of {@code convertTo}.
- *
+ *
* @param value value expressed in <b>base</b> unit
* @return value expressed in <b>this</b> unit
* @since 2018-12-22
@@ -204,7 +206,7 @@ public abstract class Unit implements Nameable {
* {@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
@@ -231,7 +233,7 @@ public abstract class Unit implements Nameable {
* {@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
@@ -266,7 +268,7 @@ public abstract class Unit implements Nameable {
*
* @implSpec This method is used by {@link #convertTo}, and its behaviour
* affects the behaviour of {@code convertTo}.
- *
+ *
* @param value value expressed in <b>this</b> unit
* @return value expressed in <b>base</b> unit
* @since 2018-12-22
@@ -347,16 +349,34 @@ public abstract class Unit implements Nameable {
.equals(Math.log10(linear.getConversionFactor()) % 1.0, 0);
}
+ /**
+ * @return a string representing this unit's definition
+ * @since 2022-03-10
+ */
+ public String toDefinitionString() {
+ if (!this.unitBase.getNameSymbol().isEmpty())
+ return "derived from " + this.unitBase.getName();
+ else
+ return "derived from "
+ + this.getBase().toString(BaseUnit::getShortName);
+ }
+
+ /**
+ * @return a string containing both this unit's name and its definition
+ * @since 2022-03-10
+ */
+ public final String toFullString() {
+ return this.toString() + " (" + this.toDefinitionString() + ")";
+ }
+
@Override
public String toString() {
- return this.getPrimaryName().orElse("Unnamed unit")
- + (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()));
+ if (this.nameSymbol.getPrimaryName().isPresent()
+ && this.nameSymbol.getSymbol().isPresent())
+ return this.nameSymbol.getPrimaryName().orElseThrow() + " ("
+ + this.nameSymbol.getSymbol().orElseThrow() + ")";
+ else
+ return this.getName();
}
/**
diff --git a/src/main/java/sevenUnits/unit/UnitDatabase.java b/src/main/java/sevenUnits/unit/UnitDatabase.java
index 18ac619..12b78a7 100644
--- a/src/main/java/sevenUnits/unit/UnitDatabase.java
+++ b/src/main/java/sevenUnits/unit/UnitDatabase.java
@@ -19,7 +19,6 @@ package sevenUnits.unit;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.AbstractSet;
@@ -47,6 +46,7 @@ import java.util.regex.Pattern;
import sevenUnits.utils.ConditionalExistenceCollections;
import sevenUnits.utils.DecimalComparison;
import sevenUnits.utils.ExpressionParser;
+import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.ObjectProduct;
import sevenUnits.utils.UncertainDouble;
@@ -1160,16 +1160,16 @@ public final class UnitDatabase {
}
/**
- * @return true if entry represents a removable duplicate entry of unitMap.
+ * @return true if entry represents a removable duplicate entry of map.
* @since 2021-05-22
*/
- static boolean isRemovableDuplicate(Map<String, Unit> unitMap,
- Entry<String, Unit> entry) {
- for (final Entry<String, Unit> e : unitMap.entrySet()) {
+ static <T> boolean isRemovableDuplicate(Map<String, T> map,
+ Entry<String, T> entry) {
+ for (final Entry<String, T> e : map.entrySet()) {
final String name = e.getKey();
- final Unit value = e.getValue();
+ final T value = e.getValue();
if (lengthFirstComparator.compare(entry.getKey(), name) < 0
- && Objects.equals(unitMap.get(entry.getKey()), value))
+ && Objects.equals(map.get(entry.getKey()), value))
return true;
}
return false;
@@ -1313,9 +1313,11 @@ public final class UnitDatabase {
*/
public void addDimension(final String name,
final ObjectProduct<BaseDimension> dimension) {
- this.dimensions.put(
- Objects.requireNonNull(name, "name must not be null."),
- Objects.requireNonNull(dimension, "dimension must not be null."));
+ Objects.requireNonNull(name, "name may not be null");
+ Objects.requireNonNull(dimension, "dimension may not be null");
+ final ObjectProduct<BaseDimension> namedDimension = dimension
+ .withName(dimension.getNameSymbol().withExtraName(name));
+ this.dimensions.put(name, namedDimension);
}
/**
@@ -1381,8 +1383,11 @@ public final class UnitDatabase {
* @since v0.1.0
*/
public void addPrefix(final String name, final UnitPrefix prefix) {
+ Objects.requireNonNull(prefix, "prefix may not be null");
+ final var namedPrefix = prefix
+ .withName(prefix.getNameSymbol().withExtraName(name));
this.prefixes.put(Objects.requireNonNull(name, "name must not be null."),
- Objects.requireNonNull(prefix, "prefix must not be null."));
+ namedPrefix);
}
/**
@@ -1395,9 +1400,11 @@ public final class UnitDatabase {
* @since v0.1.0
*/
public void addUnit(final String name, final Unit unit) {
+ Objects.requireNonNull(unit, "unit may not be null");
+ final var namedUnit = unit
+ .withName(unit.getNameSymbol().withExtraName(name));
this.prefixlessUnits.put(
- Objects.requireNonNull(name, "name must not be null."),
- Objects.requireNonNull(unit, "unit must not be null."));
+ Objects.requireNonNull(name, "name must not be null."), namedUnit);
}
/**
@@ -1451,7 +1458,8 @@ public final class UnitDatabase {
System.err.printf("Parsing error on line %d:%n", lineCounter);
throw e;
}
- this.addPrefix(name.substring(0, name.length() - 1), prefix);
+ final String prefixName = name.substring(0, name.length() - 1);
+ this.addPrefix(prefixName, prefix);
} else {
// it's a unit, get the unit
final Unit unit;
@@ -1462,13 +1470,23 @@ public final class UnitDatabase {
System.err.printf("Parsing error on line %d:%n", lineCounter);
throw e;
}
-
this.addUnit(name, unit);
}
}
}
/**
+ * Removes all units, prefixes and dimensions from this database.
+ *
+ * @since 2022-02-26
+ */
+ public void clear() {
+ this.dimensions.clear();
+ this.prefixes.clear();
+ this.prefixlessUnits.clear();
+ }
+
+ /**
* Tests if the database has a unit dimension with this name.
*
* @param name name to test
@@ -1685,11 +1703,8 @@ public final class UnitDatabase {
LinearUnitValue getLinearUnitValue(final String name) {
try {
// try to parse it as a number - otherwise it is not a number!
- final BigDecimal number = new BigDecimal(name);
-
- final double uncertainty = Math.pow(10, -number.scale());
return LinearUnitValue.of(Metric.ONE,
- UncertainDouble.of(number.doubleValue(), uncertainty));
+ UncertainDouble.fromRoundedString(name));
} catch (final NumberFormatException e) {
return LinearUnitValue.getExact(this.getLinearUnit(name), 1);
}
@@ -1994,12 +2009,18 @@ public final class UnitDatabase {
}
/**
+ * @param includeDuplicates if false, duplicates are removed from the map
* @return a map mapping prefix names to prefixes
- * @since 2019-04-13
- * @since v0.2.0
+ * @since 2022-04-18
+ * @since v0.4.0
*/
- public Map<String, UnitPrefix> prefixMap() {
- return Collections.unmodifiableMap(this.prefixes);
+ public Map<String, UnitPrefix> prefixMap(boolean includeDuplicates) {
+ if (includeDuplicates)
+ return Collections.unmodifiableMap(this.prefixes);
+ else
+ return Collections.unmodifiableMap(ConditionalExistenceCollections
+ .conditionalExistenceMap(this.prefixes,
+ entry -> !isRemovableDuplicate(this.prefixes, entry)));
}
/**
diff --git a/src/main/java/sevenUnits/unit/UnitPrefix.java b/src/main/java/sevenUnits/unit/UnitPrefix.java
index 308f4b0..e1f7788 100644
--- a/src/main/java/sevenUnits/unit/UnitPrefix.java
+++ b/src/main/java/sevenUnits/unit/UnitPrefix.java
@@ -17,68 +17,59 @@
package sevenUnits.unit;
import java.util.Objects;
-import java.util.Optional;
-import java.util.Set;
import sevenUnits.utils.DecimalComparison;
+import sevenUnits.utils.NameSymbol;
+import sevenUnits.utils.Nameable;
/**
- * A prefix that can be applied to a {@code LinearUnit} to multiply it by some value
+ * A prefix that can be applied to a {@code LinearUnit} to multiply it by some
+ * value
*
* @author Adrien Hopkins
* @since 2019-10-16
*/
-public final class UnitPrefix {
+public final class UnitPrefix implements Nameable {
/**
* Gets a {@code UnitPrefix} from a multiplier
*
- * @param multiplier
- * multiplier of prefix
+ * @param multiplier multiplier of prefix
* @return prefix
* @since 2019-10-16
*/
public static UnitPrefix valueOf(final double multiplier) {
return new UnitPrefix(multiplier, NameSymbol.EMPTY);
}
-
+
/**
* Gets a {@code UnitPrefix} from a multiplier and a name
*
- * @param multiplier
- * multiplier of prefix
- * @param ns
- * name(s) and symbol of prefix
+ * @param multiplier multiplier of prefix
+ * @param ns name(s) and symbol of prefix
* @return prefix
* @since 2019-10-16
- * @throws NullPointerException
- * if ns is null
+ * @throws NullPointerException if ns is null
*/
- public static UnitPrefix valueOf(final double multiplier, final NameSymbol ns) {
- return new UnitPrefix(multiplier, Objects.requireNonNull(ns, "ns must not be null."));
+ public static UnitPrefix valueOf(final double multiplier,
+ final NameSymbol ns) {
+ return new UnitPrefix(multiplier,
+ Objects.requireNonNull(ns, "ns must not be null."));
}
-
- /**
- * This prefix's primary name
- */
- private final Optional<String> primaryName;
-
- /**
- * This prefix's symbol
- */
- private final Optional<String> symbol;
-
+
/**
- * Other names and symbols used by this prefix
+ * This prefix's name(s) and symbol.
+ *
+ * @since 2022-04-16
*/
- private final Set<String> otherNames;
-
+ private final NameSymbol nameSymbol;
+
/**
* The number that this prefix multiplies units by
*
* @since 2019-10-16
*/
private final double multiplier;
-
+
/**
* Creates the {@code DefaultUnitPrefix}.
*
@@ -88,28 +79,24 @@ public final class UnitPrefix {
*/
private UnitPrefix(final double multiplier, final NameSymbol ns) {
this.multiplier = multiplier;
- this.primaryName = ns.getPrimaryName();
- this.symbol = ns.getSymbol();
- this.otherNames = ns.getOtherNames();
+ this.nameSymbol = ns;
}
-
+
/**
* Divides this prefix by a scalar
*
- * @param divisor
- * number to divide by
+ * @param divisor number to divide by
* @return quotient of prefix and scalar
* @since 2019-10-16
*/
public UnitPrefix dividedBy(final double divisor) {
return valueOf(this.getMultiplier() / divisor);
}
-
+
/**
* Divides this prefix by {@code other}.
*
- * @param other
- * prefix to divide by
+ * @param other prefix to divide by
* @return quotient of prefixes
* @since 2019-04-13
* @since v0.2.0
@@ -117,7 +104,7 @@ public final class UnitPrefix {
public UnitPrefix dividedBy(final UnitPrefix other) {
return valueOf(this.getMultiplier() / other.getMultiplier());
}
-
+
/**
* {@inheritDoc}
*
@@ -132,9 +119,10 @@ public final class UnitPrefix {
if (!(obj instanceof UnitPrefix))
return false;
final UnitPrefix other = (UnitPrefix) obj;
- return DecimalComparison.equals(this.getMultiplier(), other.getMultiplier());
+ return DecimalComparison.equals(this.getMultiplier(),
+ other.getMultiplier());
}
-
+
/**
* @return prefix's multiplier
* @since 2019-11-26
@@ -142,31 +130,12 @@ public final class UnitPrefix {
public double getMultiplier() {
return this.multiplier;
}
-
- /**
- * @return other names
- * @since 2019-11-26
- */
- public final Set<String> getOtherNames() {
- return this.otherNames;
- }
-
- /**
- * @return primary name
- * @since 2019-11-26
- */
- public final Optional<String> getPrimaryName() {
- return this.primaryName;
- }
-
- /**
- * @return symbol
- * @since 2019-11-26
- */
- public final Optional<String> getSymbol() {
- return this.symbol;
+
+ @Override
+ public NameSymbol getNameSymbol() {
+ return this.nameSymbol;
}
-
+
/**
* {@inheritDoc}
*
@@ -176,24 +145,22 @@ public final class UnitPrefix {
public int hashCode() {
return DecimalComparison.hash(this.getMultiplier());
}
-
+
/**
* Multiplies this prefix by a scalar
*
- * @param multiplicand
- * number to multiply by
+ * @param multiplicand number to multiply by
* @return product of prefix and scalar
* @since 2019-10-16
*/
public UnitPrefix times(final double multiplicand) {
return valueOf(this.getMultiplier() * multiplicand);
}
-
+
/**
* Multiplies this prefix by {@code other}.
*
- * @param other
- * prefix to multiply by
+ * @param other prefix to multiply by
* @return product of prefixes
* @since 2019-04-13
* @since v0.2.0
@@ -201,12 +168,11 @@ public final class UnitPrefix {
public UnitPrefix times(final UnitPrefix other) {
return valueOf(this.getMultiplier() * other.getMultiplier());
}
-
+
/**
* Raises this prefix to an exponent.
*
- * @param exponent
- * exponent to raise to
+ * @param exponent exponent to raise to
* @return result of exponentiation.
* @since 2019-04-13
* @since v0.2.0
@@ -214,27 +180,27 @@ public final class UnitPrefix {
public UnitPrefix toExponent(final double exponent) {
return valueOf(Math.pow(this.getMultiplier(), exponent));
}
-
+
/**
* @return a string describing the prefix and its multiplier
*/
@Override
public String toString() {
- if (this.primaryName.isPresent())
- return String.format("%s (\u00D7 %s)", this.primaryName.get(), this.multiplier);
- else if (this.symbol.isPresent())
- return String.format("%s (\u00D7 %s)", this.symbol.get(), this.multiplier);
+ if (this.getPrimaryName().isPresent())
+ return String.format("%s (\u00D7 %s)", this.getPrimaryName().get(),
+ this.multiplier);
+ else if (this.getSymbol().isPresent())
+ return String.format("%s (\u00D7 %s)", this.getSymbol().get(),
+ this.multiplier);
else
return String.format("Unit Prefix (\u00D7 %s)", this.multiplier);
}
-
+
/**
- * @param ns
- * name(s) and symbol to use
+ * @param ns name(s) and symbol to use
* @return copy of this prefix with provided name(s) and symbol
* @since 2019-11-26
- * @throws NullPointerException
- * if ns is null
+ * @throws NullPointerException if ns is null
*/
public UnitPrefix withName(final NameSymbol ns) {
return valueOf(this.multiplier, ns);
diff --git a/src/main/java/sevenUnits/unit/UnitType.java b/src/main/java/sevenUnits/unit/UnitType.java
new file mode 100644
index 0000000..7cebf2d
--- /dev/null
+++ b/src/main/java/sevenUnits/unit/UnitType.java
@@ -0,0 +1,58 @@
+/**
+ * Copyright (C) 2022 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.Predicate;
+
+/**
+ * A type of unit, as chosen by the type of system it is in.
+ * <ul>
+ * <li>{@code METRIC} refers to metric/SI units that pass {@link Unit#isMetric}
+ * <li>{@code SEMI_METRIC} refers to the degree Celsius (which is an official SI
+ * unit but does not pass {@link Unit#isMetric}) and non-metric units intended
+ * for use with the SI.
+ * <li>{@code NON_METRIC} refers to units that are neither metric nor intended
+ * for use with the metric system (e.g. imperial and customary units)
+ * </ul>
+ *
+ * @since 2022-04-10
+ */
+public enum UnitType {
+ METRIC, SEMI_METRIC, NON_METRIC;
+
+ /**
+ * Determines which type a unit is. The type will be:
+ * <ul>
+ * <li>{@code SEMI_METRIC} if the unit passes the provided predicate
+ * <li>{@code METRIC} if it fails the predicate but is metric
+ * <li>{@code NON_METRIC} if it fails the predicate and is not metric
+ * </ul>
+ *
+ * @param u unit to test
+ * @param isSemiMetric predicate to determine if a unit is semi-metric
+ * @return type of unit
+ * @since 2022-04-18
+ */
+ public static final UnitType getType(Unit u, Predicate<Unit> isSemiMetric) {
+ if (isSemiMetric.test(u))
+ return SEMI_METRIC;
+ else if (u.isMetric())
+ return METRIC;
+ else
+ return NON_METRIC;
+ }
+}
diff --git a/src/main/java/sevenUnits/unit/UnitValue.java b/src/main/java/sevenUnits/unit/UnitValue.java
index f6d18f8..339263d 100644
--- a/src/main/java/sevenUnits/unit/UnitValue.java
+++ b/src/main/java/sevenUnits/unit/UnitValue.java
@@ -19,6 +19,8 @@ package sevenUnits.unit;
import java.util.Objects;
import java.util.Optional;
+import sevenUnits.utils.NameSymbol;
+
/**
* A value expressed in a unit.
*
diff --git a/src/main/java/sevenUnits/unit/Unitlike.java b/src/main/java/sevenUnits/unit/Unitlike.java
index d2dcbbb..68de2c2 100644
--- a/src/main/java/sevenUnits/unit/Unitlike.java
+++ b/src/main/java/sevenUnits/unit/Unitlike.java
@@ -22,6 +22,8 @@ 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;
/**
diff --git a/src/main/java/sevenUnits/unit/UnitlikeValue.java b/src/main/java/sevenUnits/unit/UnitlikeValue.java
index edc13ca..26354b1 100644
--- a/src/main/java/sevenUnits/unit/UnitlikeValue.java
+++ b/src/main/java/sevenUnits/unit/UnitlikeValue.java
@@ -18,6 +18,8 @@ package sevenUnits.unit;
import java.util.Optional;
+import sevenUnits.utils.NameSymbol;
+
/**
*
* @since 2020-09-07