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.java574
1 files changed, 251 insertions, 323 deletions
diff --git a/src/main/java/sevenUnits/unit/UnitDatabase.java b/src/main/java/sevenUnits/unit/UnitDatabase.java
index b0d026f..a85ec5f 100644
--- a/src/main/java/sevenUnits/unit/UnitDatabase.java
+++ b/src/main/java/sevenUnits/unit/UnitDatabase.java
@@ -122,7 +122,7 @@ public final class UnitDatabase {
implements Entry<String, Unit> {
private final String key;
private final Unit value;
-
+
/**
* Creates the {@code PrefixedUnitEntry}.
*
@@ -135,7 +135,7 @@ public final class UnitDatabase {
this.key = key;
this.value = value;
}
-
+
/**
* @since 2019-05-03
* @since v0.3.0
@@ -148,17 +148,17 @@ public final class UnitDatabase {
return Objects.equals(this.getKey(), other.getKey())
&& Objects.equals(this.getValue(), other.getValue());
}
-
+
@Override
public String getKey() {
return this.key;
}
-
+
@Override
public Unit getValue() {
return this.value;
}
-
+
/**
* @since 2019-05-03
* @since v0.3.0
@@ -169,13 +169,13 @@ public final class UnitDatabase {
^ (this.getValue() == null ? 0
: this.getValue().hashCode());
}
-
+
@Override
public Unit setValue(final Unit value) {
throw new UnsupportedOperationException(
"Cannot set value in an immutable entry");
}
-
+
/**
* Returns a string representation of the entry. The format of the
* string is the string representation of the key, then the equals
@@ -190,7 +190,7 @@ public final class UnitDatabase {
return this.getKey() + "=" + this.getValue();
}
}
-
+
/**
* An iterator that iterates over the units of a
* {@code PrefixedUnitNameSet}.
@@ -205,12 +205,12 @@ public final class UnitDatabase {
private int unitNamePosition = 0;
// the indices of the prefixes attached to the current unit
private final List<Integer> prefixCoordinates = new ArrayList<>();
-
+
// values from the unit entry set
private final Map<String, Unit> map;
private transient final List<String> unitNames;
private transient final List<String> prefixNames;
-
+
/**
* Creates the
* {@code UnitsDatabase.PrefixedUnitMap.PrefixedUnitNameSet.PrefixedUnitNameIterator}.
@@ -225,7 +225,7 @@ public final class UnitDatabase {
this.unitNames = new ArrayList<>(map.units.keySet());
this.prefixNames = new ArrayList<>(map.prefixes.keySet());
}
-
+
/**
* @return current unit name
* @since 2019-04-14
@@ -237,10 +237,10 @@ public final class UnitDatabase {
unitName.append(this.prefixNames.get(i));
}
unitName.append(this.unitNames.get(this.unitNamePosition));
-
+
return unitName.toString();
}
-
+
@Override
public boolean hasNext() {
if (this.unitNames.isEmpty())
@@ -253,7 +253,7 @@ public final class UnitDatabase {
return true;
}
}
-
+
/**
* Changes this iterator's position to the next available one.
*
@@ -262,11 +262,11 @@ public final class UnitDatabase {
*/
private void incrementPosition() {
this.unitNamePosition++;
-
+
if (this.unitNamePosition >= this.unitNames.size()) {
// we have used all of our units, go to a different prefix
this.unitNamePosition = 0;
-
+
// if the prefix coordinates are empty, then set it to [0]
if (this.prefixCoordinates.isEmpty()) {
this.prefixCoordinates.add(0, 0);
@@ -275,7 +275,7 @@ public final class UnitDatabase {
int i = this.prefixCoordinates.size() - 1;
this.prefixCoordinates.set(i,
this.prefixCoordinates.get(i) + 1);
-
+
// fix any carrying errors
while (i >= 0 && this.prefixCoordinates
.get(i) >= this.prefixNames.size()) {
@@ -283,7 +283,7 @@ public final class UnitDatabase {
this.prefixCoordinates.set(i--, 0); // null and
// decrement at the
// same time
-
+
if (i < 0) { // we need to add a new coordinate
this.prefixCoordinates.add(0, 0);
} else { // increment an existing one
@@ -294,18 +294,18 @@ public final class UnitDatabase {
}
}
}
-
+
@Override
public Entry<String, Unit> next() {
// get next element
final Entry<String, Unit> nextEntry = this.peek();
-
+
// iterate to next position
this.incrementPosition();
-
+
return nextEntry;
}
-
+
/**
* @return the next element in the iterator, without iterating over
* it
@@ -315,7 +315,7 @@ public final class UnitDatabase {
private Entry<String, Unit> peek() {
if (!this.hasNext())
throw new NoSuchElementException("No units left!");
-
+
// if I have prefixes, ensure I'm not using a nonlinear unit
// since all of the unprefixed stuff is done, just remove
// nonlinear units
@@ -326,12 +326,12 @@ public final class UnitDatabase {
this.unitNames.remove(this.unitNamePosition);
}
}
-
+
final String nextName = this.getCurrentUnitName();
-
+
return new PrefixedUnitEntry(nextName, this.map.get(nextName));
}
-
+
/**
* Returns a string representation of the object. The exact details
* of the representation are unspecified and subject to change.
@@ -346,10 +346,10 @@ public final class UnitDatabase {
this.peek());
}
}
-
+
// the map that created this set
private final PrefixedUnitMap map;
-
+
/**
* Creates the {@code PrefixedUnitNameSet}.
*
@@ -360,31 +360,31 @@ public final class UnitDatabase {
public PrefixedUnitEntrySet(final PrefixedUnitMap map) {
this.map = map;
}
-
+
@Override
public boolean add(final Map.Entry<String, Unit> e) {
throw new UnsupportedOperationException(
"Cannot add to an immutable set");
}
-
+
@Override
public boolean addAll(
final Collection<? extends Map.Entry<String, Unit>> c) {
throw new UnsupportedOperationException(
"Cannot add to an immutable set");
}
-
+
@Override
public void clear() {
throw new UnsupportedOperationException(
"Cannot clear an immutable set");
}
-
+
@Override
public boolean contains(final Object o) {
// get the entry
final Entry<String, Unit> entry;
-
+
try {
// This is OK because I'm in a try-catch block, catching the
// exact exception that would be thrown.
@@ -395,11 +395,11 @@ public final class UnitDatabase {
throw new IllegalArgumentException(
"Attempted to test for an entry using a non-entry.");
}
-
+
return this.map.containsKey(entry.getKey())
&& this.map.get(entry.getKey()).equals(entry.getValue());
}
-
+
@Override
public boolean containsAll(final Collection<?> c) {
for (final Object o : c)
@@ -407,42 +407,42 @@ public final class UnitDatabase {
return false;
return true;
}
-
+
@Override
public boolean isEmpty() {
return this.map.isEmpty();
}
-
+
@Override
public Iterator<Entry<String, Unit>> iterator() {
return new PrefixedUnitEntryIterator(this.map);
}
-
+
@Override
public boolean remove(final Object o) {
throw new UnsupportedOperationException(
"Cannot remove from an immutable set");
}
-
+
@Override
public boolean removeAll(final Collection<?> c) {
throw new UnsupportedOperationException(
"Cannot remove from an immutable set");
}
-
+
@Override
public boolean removeIf(
final Predicate<? super Entry<String, Unit>> filter) {
throw new UnsupportedOperationException(
"Cannot remove from an immutable set");
}
-
+
@Override
public boolean retainAll(final Collection<?> c) {
throw new UnsupportedOperationException(
"Cannot remove from an immutable set");
}
-
+
@Override
public int size() {
if (this.map.units.isEmpty())
@@ -455,7 +455,7 @@ public final class UnitDatabase {
return Integer.MAX_VALUE;
}
}
-
+
/**
* @throws IllegalStateException if the set is infinite in size
*/
@@ -468,7 +468,7 @@ public final class UnitDatabase {
throw new IllegalStateException(
"Cannot make an infinite set into an array.");
}
-
+
/**
* @throws IllegalStateException if the set is infinite in size
*/
@@ -481,7 +481,7 @@ public final class UnitDatabase {
throw new IllegalStateException(
"Cannot make an infinite set into an array.");
}
-
+
@Override
public String toString() {
if (this.map.units.isEmpty() || this.map.prefixes.isEmpty())
@@ -492,7 +492,7 @@ public final class UnitDatabase {
this.map.units, this.map.prefixes);
}
}
-
+
/**
* The class used for unit name sets.
*
@@ -524,12 +524,12 @@ public final class UnitDatabase {
private int unitNamePosition = 0;
// the indices of the prefixes attached to the current unit
private final List<Integer> prefixCoordinates = new ArrayList<>();
-
+
// values from the unit name set
private final Map<String, Unit> map;
private transient final List<String> unitNames;
private transient final List<String> prefixNames;
-
+
/**
* Creates the
* {@code UnitsDatabase.PrefixedUnitMap.PrefixedUnitNameSet.PrefixedUnitNameIterator}.
@@ -544,7 +544,7 @@ public final class UnitDatabase {
this.unitNames = new ArrayList<>(map.units.keySet());
this.prefixNames = new ArrayList<>(map.prefixes.keySet());
}
-
+
/**
* @return current unit name
* @since 2019-04-14
@@ -556,10 +556,10 @@ public final class UnitDatabase {
unitName.append(this.prefixNames.get(i));
}
unitName.append(this.unitNames.get(this.unitNamePosition));
-
+
return unitName.toString();
}
-
+
@Override
public boolean hasNext() {
if (this.unitNames.isEmpty())
@@ -572,7 +572,7 @@ public final class UnitDatabase {
return true;
}
}
-
+
/**
* Changes this iterator's position to the next available one.
*
@@ -581,11 +581,11 @@ public final class UnitDatabase {
*/
private void incrementPosition() {
this.unitNamePosition++;
-
+
if (this.unitNamePosition >= this.unitNames.size()) {
// we have used all of our units, go to a different prefix
this.unitNamePosition = 0;
-
+
// if the prefix coordinates are empty, then set it to [0]
if (this.prefixCoordinates.isEmpty()) {
this.prefixCoordinates.add(0, 0);
@@ -594,7 +594,7 @@ public final class UnitDatabase {
int i = this.prefixCoordinates.size() - 1;
this.prefixCoordinates.set(i,
this.prefixCoordinates.get(i) + 1);
-
+
// fix any carrying errors
while (i >= 0 && this.prefixCoordinates
.get(i) >= this.prefixNames.size()) {
@@ -602,7 +602,7 @@ public final class UnitDatabase {
this.prefixCoordinates.set(i--, 0); // null and
// decrement at the
// same time
-
+
if (i < 0) { // we need to add a new coordinate
this.prefixCoordinates.add(0, 0);
} else { // increment an existing one
@@ -613,16 +613,16 @@ public final class UnitDatabase {
}
}
}
-
+
@Override
public String next() {
final String nextName = this.peek();
-
+
this.incrementPosition();
-
+
return nextName;
}
-
+
/**
* @return the next element in the iterator, without iterating over
* it
@@ -642,10 +642,10 @@ public final class UnitDatabase {
this.unitNames.remove(this.unitNamePosition);
}
}
-
+
return this.getCurrentUnitName();
}
-
+
/**
* Returns a string representation of the object. The exact details
* of the representation are unspecified and subject to change.
@@ -660,10 +660,10 @@ public final class UnitDatabase {
this.peek());
}
}
-
+
// the map that created this set
private final PrefixedUnitMap map;
-
+
/**
* Creates the {@code PrefixedUnitNameSet}.
*
@@ -674,30 +674,30 @@ public final class UnitDatabase {
public PrefixedUnitNameSet(final PrefixedUnitMap map) {
this.map = map;
}
-
+
@Override
public boolean add(final String e) {
throw new UnsupportedOperationException(
"Cannot add to an immutable set");
}
-
+
@Override
public boolean addAll(final Collection<? extends String> c) {
throw new UnsupportedOperationException(
"Cannot add to an immutable set");
}
-
+
@Override
public void clear() {
throw new UnsupportedOperationException(
"Cannot clear an immutable set");
}
-
+
@Override
public boolean contains(final Object o) {
return this.map.containsKey(o);
}
-
+
@Override
public boolean containsAll(final Collection<?> c) {
for (final Object o : c)
@@ -705,41 +705,41 @@ public final class UnitDatabase {
return false;
return true;
}
-
+
@Override
public boolean isEmpty() {
return this.map.isEmpty();
}
-
+
@Override
public Iterator<String> iterator() {
return new PrefixedUnitNameIterator(this.map);
}
-
+
@Override
public boolean remove(final Object o) {
throw new UnsupportedOperationException(
"Cannot remove from an immutable set");
}
-
+
@Override
public boolean removeAll(final Collection<?> c) {
throw new UnsupportedOperationException(
"Cannot remove from an immutable set");
}
-
+
@Override
public boolean removeIf(final Predicate<? super String> filter) {
throw new UnsupportedOperationException(
"Cannot remove from an immutable set");
}
-
+
@Override
public boolean retainAll(final Collection<?> c) {
throw new UnsupportedOperationException(
"Cannot remove from an immutable set");
}
-
+
@Override
public int size() {
if (this.map.units.isEmpty())
@@ -752,7 +752,7 @@ public final class UnitDatabase {
return Integer.MAX_VALUE;
}
}
-
+
/**
* @throws IllegalStateException if the set is infinite in size
*/
@@ -764,9 +764,9 @@ public final class UnitDatabase {
// infinite set
throw new IllegalStateException(
"Cannot make an infinite set into an array.");
-
+
}
-
+
/**
* @throws IllegalStateException if the set is infinite in size
*/
@@ -779,7 +779,7 @@ public final class UnitDatabase {
throw new IllegalStateException(
"Cannot make an infinite set into an array.");
}
-
+
@Override
public String toString() {
if (this.map.units.isEmpty() || this.map.prefixes.isEmpty())
@@ -790,7 +790,7 @@ public final class UnitDatabase {
this.map.units, this.map.prefixes);
}
}
-
+
/**
* The units stored in this collection, without prefixes.
*
@@ -798,7 +798,7 @@ public final class UnitDatabase {
* @since v0.2.0
*/
private final Map<String, Unit> units;
-
+
/**
* The available prefixes for use.
*
@@ -806,12 +806,12 @@ public final class UnitDatabase {
* @since v0.2.0
*/
private final Map<String, UnitPrefix> prefixes;
-
+
// caches
private transient Collection<Unit> values = null;
private transient Set<String> keySet = null;
private transient Set<Entry<String, Unit>> entrySet = null;
-
+
/**
* Creates the {@code PrefixedUnitMap}.
*
@@ -827,50 +827,50 @@ public final class UnitDatabase {
this.units = Collections.unmodifiableMap(units);
this.prefixes = Collections.unmodifiableMap(prefixes);
}
-
+
@Override
public void clear() {
throw new UnsupportedOperationException(
"Cannot clear an immutable map");
}
-
+
@Override
public Unit compute(final String key,
final BiFunction<? super String, ? super Unit, ? extends Unit> remappingFunction) {
throw new UnsupportedOperationException(
"Cannot edit an immutable map");
}
-
+
@Override
public Unit computeIfAbsent(final String key,
final Function<? super String, ? extends Unit> mappingFunction) {
throw new UnsupportedOperationException(
"Cannot edit an immutable map");
}
-
+
@Override
public Unit computeIfPresent(final String key,
final BiFunction<? super String, ? super Unit, ? extends Unit> remappingFunction) {
throw new UnsupportedOperationException(
"Cannot edit an immutable map");
}
-
+
@Override
public boolean containsKey(final Object key) {
// First, test if there is a unit with the key
if (this.units.containsKey(key))
return true;
-
+
// Next, try to cast it to String
if (!(key instanceof String))
throw new IllegalArgumentException(
"Attempted to test for a unit using a non-string name.");
final String unitName = (String) key;
-
+
// Then, look for the longest prefix that is attached to a valid unit
String longestPrefix = null;
int longestLength = 0;
-
+
for (final String prefixName : this.prefixes.keySet()) {
// a prefix name is valid if:
// - it is prefixed (i.e. the unit name starts with it)
@@ -889,10 +889,10 @@ public final class UnitDatabase {
}
}
}
-
+
return longestPrefix != null;
}
-
+
/**
* {@inheritDoc}
*
@@ -905,7 +905,7 @@ public final class UnitDatabase {
public boolean containsValue(final Object value) {
return this.units.containsValue(value);
}
-
+
@Override
public Set<Entry<String, Unit>> entrySet() {
if (this.entrySet == null) {
@@ -913,23 +913,23 @@ public final class UnitDatabase {
}
return this.entrySet;
}
-
+
@Override
public Unit get(final Object key) {
// First, test if there is a unit with the key
if (this.units.containsKey(key))
return this.units.get(key);
-
+
// Next, try to cast it to String
if (!(key instanceof String))
throw new IllegalArgumentException(
"Attempted to obtain a unit using a non-string name.");
final String unitName = (String) key;
-
+
// Then, look for the longest prefix that is attached to a valid unit
String longestPrefix = null;
int longestLength = 0;
-
+
for (final String prefixName : this.prefixes.keySet()) {
// a prefix name is valid if:
// - it is prefixed (i.e. the unit name starts with it)
@@ -948,7 +948,7 @@ public final class UnitDatabase {
}
}
}
-
+
// if none found, returns null
if (longestPrefix == null)
return null;
@@ -959,16 +959,16 @@ public final class UnitDatabase {
// before selecting this prefix
final LinearUnit unit = (LinearUnit) this.get(rest);
final UnitPrefix prefix = this.prefixes.get(longestPrefix);
-
+
return unit.withPrefix(prefix);
}
}
-
+
@Override
public boolean isEmpty() {
return this.units.isEmpty();
}
-
+
@Override
public Set<String> keySet() {
if (this.keySet == null) {
@@ -976,64 +976,64 @@ public final class UnitDatabase {
}
return this.keySet;
}
-
+
@Override
public Unit merge(final String key, final Unit value,
final BiFunction<? super Unit, ? super Unit, ? extends Unit> remappingFunction) {
throw new UnsupportedOperationException(
"Cannot merge into an immutable map");
}
-
+
@Override
public Unit put(final String key, final Unit value) {
throw new UnsupportedOperationException(
"Cannot add entries to an immutable map");
}
-
+
@Override
public void putAll(final Map<? extends String, ? extends Unit> m) {
throw new UnsupportedOperationException(
"Cannot add entries to an immutable map");
}
-
+
@Override
public Unit putIfAbsent(final String key, final Unit value) {
throw new UnsupportedOperationException(
"Cannot add entries to an immutable map");
}
-
+
@Override
public Unit remove(final Object key) {
throw new UnsupportedOperationException(
"Cannot remove entries from an immutable map");
}
-
+
@Override
public boolean remove(final Object key, final Object value) {
throw new UnsupportedOperationException(
"Cannot remove entries from an immutable map");
}
-
+
@Override
public Unit replace(final String key, final Unit value) {
throw new UnsupportedOperationException(
"Cannot replace entries in an immutable map");
}
-
+
@Override
public boolean replace(final String key, final Unit oldValue,
final Unit newValue) {
throw new UnsupportedOperationException(
"Cannot replace entries in an immutable map");
}
-
+
@Override
public void replaceAll(
final BiFunction<? super String, ? super Unit, ? extends Unit> function) {
throw new UnsupportedOperationException(
"Cannot replace entries in an immutable map");
}
-
+
@Override
public int size() {
if (this.units.isEmpty())
@@ -1046,7 +1046,7 @@ public final class UnitDatabase {
return Integer.MAX_VALUE;
}
}
-
+
@Override
public String toString() {
if (this.units.isEmpty() || this.prefixes.isEmpty())
@@ -1056,7 +1056,7 @@ public final class UnitDatabase {
"Infinite map of name-unit entries created from units %s and prefixes %s",
this.units, this.prefixes);
}
-
+
/**
* {@inheritDoc}
*
@@ -1074,48 +1074,20 @@ public final class UnitDatabase {
return this.values;
}
}
-
- /**
- * Replacements done to *all* expression types
- */
- private static final Map<Pattern, String> EXPRESSION_REPLACEMENTS = new HashMap<>();
-
- // add data to expression replacements
- static {
- // add spaces around operators
- for (final String operator : Arrays.asList("\\*", "/", "\\|", "\\^")) {
- EXPRESSION_REPLACEMENTS.put(Pattern.compile(operator),
- " " + operator + " ");
- }
-
- // replace multiple spaces with a single space
- EXPRESSION_REPLACEMENTS.put(Pattern.compile(" +"), " ");
- // place brackets around any expression of the form "number unit", with or
- // without the space
- EXPRESSION_REPLACEMENTS.put(Pattern.compile("((?:-?[1-9]\\d*|0)" // integer
- + "(?:\\.\\d+(?:[eE]\\d+))?)" // optional decimal point with numbers
- // after it
- + "\\s*" // optional space(s)
- + "([a-zA-Z]+(?:\\^\\d+)?" // any string of letters
- + "(?:\\s+[a-zA-Z]+(?:\\^\\d+)?))" // optional other letters
- + "(?!-?\\d)" // no number directly afterwards (avoids matching
- // "1e3")
- ), "\\($1 $2\\)");
- }
-
+
/**
* A regular expression that separates names and expressions in unit files.
*/
private static final Pattern NAME_EXPRESSION = Pattern
.compile("(\\S+)\\s+(\\S.*)");
-
+
/**
* Like normal string comparisons, but shorter strings are always less than
* longer strings.
*/
private static final Comparator<String> lengthFirstComparator = Comparator
.comparingInt(String::length).thenComparing(Comparator.naturalOrder());
-
+
/**
* The exponent operator
*
@@ -1131,11 +1103,11 @@ public final class UnitDatabase {
throw new IllegalArgumentException(String.format(
"Tried to exponentiate %s^%s, but exponents must be dimensionless numbers.",
base, exponentUnit));
-
+
final double exponent = exponentUnit.getConversionFactor();
return base.toExponentRounded(exponent);
}
-
+
/**
* The exponent operator
*
@@ -1151,11 +1123,33 @@ public final class UnitDatabase {
throw new IllegalArgumentException(String.format(
"Tried to exponentiate %s^%s, but exponents must be dimensionless numbers.",
base, exponentValue));
-
+
final double exponent = exponentValue.getValueExact();
return base.toExponentRounded(exponent);
}
-
+
+ /**
+ * Formats an expression so it can be parsed by the expression parser.
+ *
+ * Specifically, puts spaces around all operators so they can be parsed as
+ * words.
+ *
+ * @param expression expression to format
+ * @return formatted expression
+ * @since 2025-06-07
+ * @since v1.0.0
+ */
+ static final String formatExpression(String expression) {
+ String modifiedExpression = expression;
+ for (final String operator : Arrays.asList("\\*", "/", "\\|", "\\^")) {
+ modifiedExpression = modifiedExpression.replaceAll(
+ operator, " " + operator + " ");
+ }
+
+ modifiedExpression = modifiedExpression.replaceAll("\\s+", " ");
+ return modifiedExpression;
+ }
+
/**
* @return true if entry represents a removable duplicate entry of map.
* @since 2021-05-22
@@ -1172,7 +1166,7 @@ public final class UnitDatabase {
}
return false;
}
-
+
/**
* The units in this system, excluding prefixes.
*
@@ -1180,7 +1174,7 @@ public final class UnitDatabase {
* @since v0.1.0
*/
private final Map<String, Unit> prefixlessUnits;
-
+
/**
* The unit prefixes in this system.
*
@@ -1188,7 +1182,7 @@ public final class UnitDatabase {
* @since v0.1.0
*/
private final Map<String, UnitPrefix> prefixes;
-
+
/**
* The dimensions in this system.
*
@@ -1196,7 +1190,7 @@ public final class UnitDatabase {
* @since v0.2.0
*/
private final Map<String, ObjectProduct<BaseDimension>> dimensions;
-
+
/**
* A map mapping strings to units (including prefixes)
*
@@ -1204,7 +1198,7 @@ public final class UnitDatabase {
* @since v0.2.0
*/
private final Map<String, Unit> units;
-
+
/**
* A map mapping strings to unit sets
*
@@ -1212,7 +1206,7 @@ public final class UnitDatabase {
* @since v1.0.0
*/
private final Map<String, List<LinearUnit>> unitSets;
-
+
/**
* The rule that specifies when prefix repetition is allowed. It takes in one
* argument: a list of the prefixes being applied to the unit
@@ -1224,7 +1218,7 @@ public final class UnitDatabase {
* {@code prefixRepetitionRule.test(Arrays.asList(giga, mega, kilo))}
*/
private Predicate<List<UnitPrefix>> prefixRepetitionRule;
-
+
/**
* A parser that can parse unit expressions.
*
@@ -1233,14 +1227,14 @@ public final class UnitDatabase {
*/
private final ExpressionParser<LinearUnit> unitExpressionParser = new ExpressionParser.Builder<>(
this::getLinearUnit).addBinaryOperator("+", (o1, o2) -> o1.plus(o2), 0)
- .addBinaryOperator("-", (o1, o2) -> o1.minus(o2), 0)
- .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 1)
- .addSpaceFunction("*")
- .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1)
- .addBinaryOperator("|", (o1, o2) -> o1.dividedBy(o2), 3)
- .addBinaryOperator("^", UnitDatabase::exponentiateUnits, 2)
- .build();
-
+ .addBinaryOperator("-", (o1, o2) -> o1.minus(o2), 0)
+ .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 1)
+ .addBinaryOperator("space_times", (o1, o2) -> o1.times(o2), 2)
+ .addSpaceFunction("space_times")
+ .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1)
+ .addBinaryOperator("|", (o1, o2) -> o1.dividedBy(o2), 4)
+ .addBinaryOperator("^", UnitDatabase::exponentiateUnits, 3).build();
+
/**
* A parser that can parse unit value expressions.
*
@@ -1249,15 +1243,16 @@ public final class UnitDatabase {
*/
private final ExpressionParser<LinearUnitValue> unitValueExpressionParser = new ExpressionParser.Builder<>(
this::getLinearUnitValue)
- .addBinaryOperator("+", (o1, o2) -> o1.plus(o2), 0)
- .addBinaryOperator("-", (o1, o2) -> o1.minus(o2), 0)
- .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 1)
- .addSpaceFunction("*")
- .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1)
- .addBinaryOperator("|", (o1, o2) -> o1.dividedBy(o2), 3)
- .addBinaryOperator("^", UnitDatabase::exponentiateUnitValues, 2)
- .build();
-
+ .addBinaryOperator("+", (o1, o2) -> o1.plus(o2), 0)
+ .addBinaryOperator("-", (o1, o2) -> o1.minus(o2), 0)
+ .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 1)
+ .addBinaryOperator("space_times", (o1, o2) -> o1.times(o2), 2)
+ .addSpaceFunction("space_times")
+ .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1)
+ .addBinaryOperator("|", (o1, o2) -> o1.dividedBy(o2), 4)
+ .addBinaryOperator("^", UnitDatabase::exponentiateUnitValues, 3)
+ .build();
+
/**
* A parser that can parse unit prefix expressions
*
@@ -1266,15 +1261,15 @@ public final class UnitDatabase {
*/
private final ExpressionParser<UnitPrefix> prefixExpressionParser = new ExpressionParser.Builder<>(
this::getPrefix).addBinaryOperator("+", (o1, o2) -> o1.plus(o2), 0)
- .addBinaryOperator("-", (o1, o2) -> o1.minus(o2), 0)
- .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 1)
- .addSpaceFunction("*")
- .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1)
- .addBinaryOperator("|", (o1, o2) -> o1.dividedBy(o2), 3)
- .addBinaryOperator("^",
- (o1, o2) -> o1.toExponent(o2.getMultiplier()), 2)
- .build();
-
+ .addBinaryOperator("-", (o1, o2) -> o1.minus(o2), 0)
+ .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 1)
+ .addSpaceFunction("*")
+ .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1)
+ .addBinaryOperator("|", (o1, o2) -> o1.dividedBy(o2), 3)
+ .addBinaryOperator("^", (o1, o2) -> o1.toExponent(o2.getMultiplier()),
+ 2)
+ .build();
+
/**
* A parser that can parse unit dimension expressions.
*
@@ -1283,14 +1278,14 @@ public final class UnitDatabase {
*/
private final ExpressionParser<ObjectProduct<BaseDimension>> unitDimensionParser = new ExpressionParser.Builder<>(
this::getDimension).addBinaryOperator("*", (o1, o2) -> o1.times(o2), 0)
- .addSpaceFunction("*")
- .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 0)
- .addBinaryOperator("|", (o1, o2) -> o1.dividedBy(o2), 2)
- .addNumericOperator("^", (o1, o2) -> {
- final int exponent = (int) Math.round(o2.value());
- return o1.toExponent(exponent);
- }, 1).build();
-
+ .addSpaceFunction("*")
+ .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 0)
+ .addBinaryOperator("|", (o1, o2) -> o1.dividedBy(o2), 2)
+ .addNumericOperator("^", (o1, o2) -> {
+ final int exponent = (int) Math.round(o2.value());
+ return o1.toExponent(exponent);
+ }, 1).build();
+
/**
* Creates the {@code UnitsDatabase}.
*
@@ -1300,7 +1295,7 @@ public final class UnitDatabase {
public UnitDatabase() {
this(prefixes -> true);
}
-
+
/**
* Creates the {@code UnitsDatabase}
*
@@ -1320,7 +1315,7 @@ public final class UnitDatabase {
.test(this.getPrefixesFromName(entry.getKey())));
this.unitSets = new HashMap<>();
}
-
+
/**
* Adds a unit dimension to the database.
*
@@ -1338,7 +1333,7 @@ public final class UnitDatabase {
.withName(dimension.getNameSymbol().withExtraName(name));
this.dimensions.put(name, namedDimension);
}
-
+
/**
* Adds to the list from a line in a unit dimension file.
*
@@ -1357,7 +1352,7 @@ public final class UnitDatabase {
lineCounter);
return;
}
-
+
// divide line into name and expression
final Matcher lineMatcher = NAME_EXPRESSION.matcher(line);
if (!lineMatcher.matches())
@@ -1366,12 +1361,12 @@ public final class UnitDatabase {
lineCounter));
final String name = lineMatcher.group(1);
final String expression = lineMatcher.group(2);
-
+
// if (name.endsWith(" ")) {
// System.err.printf("Warning - line %d's dimension name ends in a space",
// lineCounter);
// }
-
+
// if expression is "!", search for an existing dimension
// if no unit found, throw an error
if (expression.equals("!")) {
@@ -1382,7 +1377,7 @@ public final class UnitDatabase {
this.addDimension(name, this.getDimensionFromExpression(expression));
}
}
-
+
/**
* Adds a unit prefix to the database.
*
@@ -1399,7 +1394,7 @@ public final class UnitDatabase {
this.prefixes.put(Objects.requireNonNull(name, "name must not be null."),
namedPrefix);
}
-
+
/**
* Adds a unit to the database.
*
@@ -1416,7 +1411,7 @@ public final class UnitDatabase {
this.prefixlessUnits.put(
Objects.requireNonNull(name, "name must not be null."), namedUnit);
}
-
+
/**
* Adds to the list from a line in a unit file.
*
@@ -1435,7 +1430,7 @@ public final class UnitDatabase {
lineCounter);
return;
}
-
+
// divide line into name and expression
final Matcher lineMatcher = NAME_EXPRESSION.matcher(line);
if (!lineMatcher.matches())
@@ -1443,15 +1438,15 @@ public final class UnitDatabase {
"Error at line %d: Lines of a unit file must consist of a unit name, then spaces or tabs, then a unit expression.",
lineCounter));
final String name = lineMatcher.group(1);
-
+
final String expression = lineMatcher.group(2);
-
+
// this code should never occur
// if (name.endsWith(" ")) {
// System.err.printf("Warning - line %d's unit name ends in a space",
// lineCounter);
// }
-
+
// if expression is "!", search for an existing unit
// if no unit found, throw an error
if (expression.equals("!")) {
@@ -1472,7 +1467,7 @@ public final class UnitDatabase {
}
}
}
-
+
/**
* Add a unit set to the database.
*
@@ -1490,10 +1485,10 @@ public final class UnitDatabase {
"Unit sets must be all the same dimension, " + value
+ " is not.");
}
-
+
this.unitSets.put(name, value);
}
-
+
/**
* Removes all units, unit sets, prefixes and dimensions from this database.
*
@@ -1506,7 +1501,7 @@ public final class UnitDatabase {
this.prefixlessUnits.clear();
this.unitSets.clear();
}
-
+
/**
* Tests if the database has a unit dimension with this name.
*
@@ -1518,7 +1513,7 @@ public final class UnitDatabase {
public boolean containsDimensionName(final String name) {
return this.dimensions.containsKey(name);
}
-
+
/**
* Tests if the database has a unit prefix with this name.
*
@@ -1530,7 +1525,7 @@ public final class UnitDatabase {
public boolean containsPrefixName(final String name) {
return this.prefixes.containsKey(name);
}
-
+
/**
* Tests if the database has a unit with this name, taking prefixes into
* consideration
@@ -1543,7 +1538,7 @@ public final class UnitDatabase {
public boolean containsUnitName(final String name) {
return this.units.containsKey(name);
}
-
+
/**
* Returns true iff there is a unit set with this name.
*
@@ -1556,7 +1551,7 @@ public final class UnitDatabase {
public boolean containsUnitSetName(String name) {
return this.unitSets.containsKey(name);
}
-
+
/**
* @return a map mapping dimension names to dimensions
* @since 2019-04-13
@@ -1565,7 +1560,7 @@ public final class UnitDatabase {
public Map<String, ObjectProduct<BaseDimension>> dimensionMap() {
return Collections.unmodifiableMap(this.dimensions);
}
-
+
/**
* Evaluates a unit expression, following the same rules as
* {@link #getUnitFromExpression}.
@@ -1577,39 +1572,15 @@ public final class UnitDatabase {
*/
public LinearUnitValue evaluateUnitExpression(final String expression) {
Objects.requireNonNull(expression, "expression must not be null.");
-
+
// attempt to get a unit as an alias, or a number with precision first
if (this.containsUnitName(expression))
return this.getLinearUnitValue(expression);
-
- // force operators to have spaces
- String modifiedExpression = expression;
- modifiedExpression = modifiedExpression.replaceAll("\\+", " \\+ ");
- modifiedExpression = modifiedExpression.replaceAll("-", " - ");
-
- // format expression
- for (final Entry<Pattern, String> replacement : EXPRESSION_REPLACEMENTS
- .entrySet()) {
- modifiedExpression = replacement.getKey().matcher(modifiedExpression)
- .replaceAll(replacement.getValue());
- }
-
- // the previous operation breaks negative numbers, fix them!
- // (i.e. -2 becomes - 2)
- // FIXME the previous operation also breaks stuff like "1e-5"
- for (int i = 0; i < modifiedExpression.length(); i++) {
- if (modifiedExpression.charAt(i) == '-'
- && (i < 2 || Arrays.asList('+', '-', '*', '/', '|', '^')
- .contains(modifiedExpression.charAt(i - 2)))) {
- // found a broken negative number
- modifiedExpression = modifiedExpression.substring(0, i + 1)
- + modifiedExpression.substring(i + 2);
- }
- }
-
- return this.unitValueExpressionParser.parseExpression(modifiedExpression);
+
+ return this.unitValueExpressionParser
+ .parseExpression(formatExpression(expression));
}
-
+
/**
* Gets a unit dimension from the database using its name.
*
@@ -1627,7 +1598,7 @@ public final class UnitDatabase {
else
return dimension;
}
-
+
/**
* Uses the database's data to parse an expression into a unit dimension
* <p>
@@ -1651,24 +1622,14 @@ public final class UnitDatabase {
public ObjectProduct<BaseDimension> getDimensionFromExpression(
final String expression) {
Objects.requireNonNull(expression, "expression must not be null.");
-
+
// attempt to get a dimension as an alias first
if (this.containsDimensionName(expression))
return this.getDimension(expression);
-
- // force operators to have spaces
- String modifiedExpression = expression;
-
- // format expression
- for (final Entry<Pattern, String> replacement : EXPRESSION_REPLACEMENTS
- .entrySet()) {
- modifiedExpression = replacement.getKey().matcher(modifiedExpression)
- .replaceAll(replacement.getValue());
- }
-
- return this.unitDimensionParser.parseExpression(modifiedExpression);
+
+ return this.unitDimensionParser.parseExpression(formatExpression(expression));
}
-
+
/**
* Gets a unit. If it is linear, cast it to a LinearUnit and return it.
* Otherwise, throw an {@code IllegalArgumentException}.
@@ -1687,7 +1648,7 @@ public final class UnitDatabase {
if (parts.size() != 2)
throw new IllegalArgumentException(
"Format nonlinear units like: unit(value).");
-
+
// solve the function
final Unit unit = this.getUnit(parts.get(0));
final double value = Double.parseDouble(
@@ -1696,7 +1657,7 @@ public final class UnitDatabase {
} else {
// get a linear unit
final Unit unit = this.getUnit(name);
-
+
if (unit instanceof LinearUnit)
return (LinearUnit) unit;
else
@@ -1704,7 +1665,7 @@ public final class UnitDatabase {
String.format("%s is not a linear unit.", name));
}
}
-
+
/**
* Gets a {@code LinearUnitValue} from a unit name. Nonlinear units will be
* converted to their base units.
@@ -1723,7 +1684,7 @@ public final class UnitDatabase {
return LinearUnitValue.getExact(this.getLinearUnit(name), 1);
}
}
-
+
/**
* Gets a unit prefix from the database from its name
*
@@ -1744,7 +1705,7 @@ public final class UnitDatabase {
return prefix;
}
}
-
+
/**
* Gets all of the prefixes that are on a unit name, in application order.
*
@@ -1756,12 +1717,12 @@ public final class UnitDatabase {
List<UnitPrefix> getPrefixesFromName(final String unitName) {
final List<UnitPrefix> prefixes = new ArrayList<>();
String name = unitName;
-
+
while (!this.prefixlessUnits.containsKey(name)) {
// find the longest prefix
String longestPrefixName = null;
int longestLength = name.length();
-
+
while (longestPrefixName == null) {
longestLength--;
if (longestLength <= 0)
@@ -1771,7 +1732,7 @@ public final class UnitDatabase {
longestPrefixName = name.substring(0, longestLength);
}
}
-
+
// longest prefix found!
final UnitPrefix prefix = this.getPrefix(longestPrefixName);
prefixes.add(0, prefix);
@@ -1779,7 +1740,7 @@ public final class UnitDatabase {
}
return prefixes;
}
-
+
/**
* Gets a unit prefix from a prefix expression
* <p>
@@ -1796,24 +1757,14 @@ public final class UnitDatabase {
*/
public UnitPrefix getPrefixFromExpression(final String expression) {
Objects.requireNonNull(expression, "expression must not be null.");
-
+
// attempt to get a unit as an alias first
if (this.containsUnitName(expression))
return this.getPrefix(expression);
-
- // force operators to have spaces
- String modifiedExpression = expression;
-
- // format expression
- for (final Entry<Pattern, String> replacement : EXPRESSION_REPLACEMENTS
- .entrySet()) {
- modifiedExpression = replacement.getKey().matcher(modifiedExpression)
- .replaceAll(replacement.getValue());
- }
-
- return this.prefixExpressionParser.parseExpression(modifiedExpression);
+
+ return this.prefixExpressionParser.parseExpression(formatExpression(expression));
}
-
+
/**
* @return the prefixRepetitionRule
* @since 2020-08-26
@@ -1822,7 +1773,7 @@ public final class UnitDatabase {
public final Predicate<List<UnitPrefix>> getPrefixRepetitionRule() {
return this.prefixRepetitionRule;
}
-
+
/**
* Gets a unit from the database from its name, looking for prefixes.
*
@@ -1855,9 +1806,9 @@ public final class UnitDatabase {
} else
return unit;
}
-
+
}
-
+
/**
* Uses the database's unit data to parse an expression into a unit
* <p>
@@ -1882,38 +1833,15 @@ public final class UnitDatabase {
*/
public Unit getUnitFromExpression(final String expression) {
Objects.requireNonNull(expression, "expression must not be null.");
-
+
// attempt to get a unit as an alias first
if (this.containsUnitName(expression))
return this.getUnit(expression);
-
- // force operators to have spaces
- String modifiedExpression = expression;
- modifiedExpression = modifiedExpression.replaceAll("\\+", " \\+ ");
- modifiedExpression = modifiedExpression.replaceAll("-", " - ");
-
- // format expression
- for (final Entry<Pattern, String> replacement : EXPRESSION_REPLACEMENTS
- .entrySet()) {
- modifiedExpression = replacement.getKey().matcher(modifiedExpression)
- .replaceAll(replacement.getValue());
- }
-
- // the previous operation breaks negative numbers, fix them!
- // (i.e. -2 becomes - 2)
- for (int i = 0; i < modifiedExpression.length(); i++) {
- if (modifiedExpression.charAt(i) == '-'
- && (i < 2 || Arrays.asList('+', '-', '*', '/', '|', '^')
- .contains(modifiedExpression.charAt(i - 2)))) {
- // found a broken negative number
- modifiedExpression = modifiedExpression.substring(0, i + 1)
- + modifiedExpression.substring(i + 2);
- }
- }
-
- return this.unitExpressionParser.parseExpression(modifiedExpression);
+
+ return this.unitExpressionParser
+ .parseExpression(formatExpression(expression));
}
-
+
/**
* Get a unit set from its name, throwing a {@link NoSuchElementException} if
* there is none.
@@ -1930,7 +1858,7 @@ public final class UnitDatabase {
throw new NoSuchElementException("No unit set with name " + name);
return unitSet;
}
-
+
/**
* Parses a semicolon-separated expression to get the unit set being used.
*
@@ -1942,7 +1870,7 @@ public final class UnitDatabase {
final List<LinearUnit> units = new ArrayList<>(parts.length);
for (final String unitName : parts) {
final Unit unit = this.getUnitFromExpression(unitName.trim());
-
+
if (!(unit instanceof LinearUnit)) {
throw new IllegalArgumentException(String.format(
"Unit '%s' is in a unit-set expression, but is not linear.",
@@ -1957,7 +1885,7 @@ public final class UnitDatabase {
}
return units;
}
-
+
/**
* Adds all dimensions from a file, using data from the database to parse
* them.
@@ -2003,7 +1931,7 @@ public final class UnitDatabase {
}
return errors;
}
-
+
/**
* Adds all dimensions from a {@code InputStream}. Otherwise, works like
* {@link #loadDimensionFile}.
@@ -2030,7 +1958,7 @@ public final class UnitDatabase {
}
return errors;
}
-
+
/**
* Adds all units from a file, using data from the database to parse them.
* <p>
@@ -2075,7 +2003,7 @@ public final class UnitDatabase {
}
return errors;
}
-
+
/**
* Adds all units from a {@code InputStream}. Otherwise, works like
* {@link #loadUnitsFile}.
@@ -2101,7 +2029,7 @@ public final class UnitDatabase {
}
return errors;
}
-
+
/**
* @param includeDuplicates if false, duplicates are removed from the map
* @return a map mapping prefix names to prefixes
@@ -2116,7 +2044,7 @@ public final class UnitDatabase {
.conditionalExistenceMap(this.prefixes,
entry -> !isRemovableDuplicate(this.prefixes, entry)));
}
-
+
/**
* @param prefixRepetitionRule the prefixRepetitionRule to set
* @since 2020-08-26
@@ -2126,7 +2054,7 @@ public final class UnitDatabase {
Predicate<List<UnitPrefix>> prefixRepetitionRule) {
this.prefixRepetitionRule = prefixRepetitionRule;
}
-
+
/**
* @return a string stating the number of units, prefixes and dimensions in
* the database
@@ -2138,7 +2066,7 @@ public final class UnitDatabase {
this.prefixlessUnits.size(), this.prefixes.size(),
this.dimensions.size());
}
-
+
/**
* Returns a map mapping unit names to units, including units with prefixes.
* <p>
@@ -2170,7 +2098,7 @@ public final class UnitDatabase {
return this.units; // PrefixedUnitMap is immutable so I don't need to make
// an unmodifiable map.
}
-
+
/**
* @param includeDuplicates if true, duplicate units will all exist in the
* map; if false, only one of each unit will exist,
@@ -2188,7 +2116,7 @@ public final class UnitDatabase {
entry -> !isRemovableDuplicate(this.prefixlessUnits,
entry)));
}
-
+
/**
* @return an unmodifiable map mapping names to unit sets
* @since 2024-08-16