summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnits/unit/UnitDatabase.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/sevenUnits/unit/UnitDatabase.java')
-rw-r--r--src/main/java/sevenUnits/unit/UnitDatabase.java67
1 files changed, 44 insertions, 23 deletions
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)));
}
/**