diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-12-02 16:46:15 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-12-02 16:46:15 -0500 |
commit | 345fa45ae562e4699cc9c2e039dc82c7a66b0451 (patch) | |
tree | 970052482622edd7e7fa950dfcee66b165818a4f /src/main/java/sevenUnits/unit/UnitDatabase.java | |
parent | 54e481cdb7115b236bcddb86b785d88dcc757c39 (diff) | |
parent | 8ea77520ce58e948eeffc4c2e8c25c69e88ed00a (diff) |
Merge branch 'testing-upgrades' into develop
Diffstat (limited to 'src/main/java/sevenUnits/unit/UnitDatabase.java')
-rw-r--r-- | src/main/java/sevenUnits/unit/UnitDatabase.java | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/src/main/java/sevenUnits/unit/UnitDatabase.java b/src/main/java/sevenUnits/unit/UnitDatabase.java index b45d9cf..18ac619 100644 --- a/src/main/java/sevenUnits/unit/UnitDatabase.java +++ b/src/main/java/sevenUnits/unit/UnitDatabase.java @@ -243,7 +243,8 @@ public final class UnitDatabase { return false; else { if (this.prefixNames.isEmpty()) - return this.unitNamePosition >= this.unitNames.size() - 1; + return this.prefixCoordinates.isEmpty() + && this.unitNamePosition < this.unitNames.size(); else return true; } @@ -557,7 +558,8 @@ public final class UnitDatabase { return false; else { if (this.prefixNames.isEmpty()) - return this.unitNamePosition >= this.unitNames.size() - 1; + return this.prefixCoordinates.isEmpty() + && this.unitNamePosition < this.unitNames.size(); else return true; } @@ -1038,7 +1040,7 @@ public final class UnitDatabase { @Override public String toString() { if (this.units.isEmpty() || this.prefixes.isEmpty()) - return super.toString(); + return new HashMap<>(this).toString(); else return String.format( "Infinite map of name-unit entries created from units %s and prefixes %s", @@ -1161,7 +1163,7 @@ public final class UnitDatabase { * @return true if entry represents a removable duplicate entry of unitMap. * @since 2021-05-22 */ - private static boolean isRemovableDuplicate(Map<String, Unit> unitMap, + static boolean isRemovableDuplicate(Map<String, Unit> unitMap, Entry<String, Unit> entry) { for (final Entry<String, Unit> e : unitMap.entrySet()) { final String name = e.getKey(); @@ -1344,10 +1346,10 @@ public final class UnitDatabase { 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 (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 @@ -1360,7 +1362,7 @@ public final class UnitDatabase { final ObjectProduct<BaseDimension> dimension; try { dimension = this.getDimensionFromExpression(expression); - } catch (final IllegalArgumentException e) { + } catch (final IllegalArgumentException | NoSuchElementException e) { System.err.printf("Parsing error on line %d:%n", lineCounter); throw e; } @@ -1427,10 +1429,11 @@ public final class UnitDatabase { final String expression = lineMatcher.group(2); - if (name.endsWith(" ")) { - System.err.printf("Warning - line %d's unit name ends in a space", - lineCounter); - } + // 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 @@ -1443,7 +1446,8 @@ public final class UnitDatabase { final UnitPrefix prefix; try { prefix = this.getPrefixFromExpression(expression); - } catch (final IllegalArgumentException e) { + } catch (final IllegalArgumentException + | NoSuchElementException e) { System.err.printf("Parsing error on line %d:%n", lineCounter); throw e; } @@ -1453,7 +1457,8 @@ public final class UnitDatabase { final Unit unit; try { unit = this.getUnitFromExpression(expression); - } catch (final IllegalArgumentException e) { + } catch (final IllegalArgumentException + | NoSuchElementException e) { System.err.printf("Parsing error on line %d:%n", lineCounter); throw e; } @@ -1581,8 +1586,15 @@ public final class UnitDatabase { } return base.toExponent(exponent); + } else { + final ObjectProduct<BaseDimension> dimension = this.dimensions + .get(name); + if (dimension == null) + throw new NoSuchElementException( + "No dimension with name \"" + name + "\"."); + else + return dimension; } - return this.dimensions.get(name); } /** @@ -1635,7 +1647,7 @@ public final class UnitDatabase { * @since 2019-03-22 * @since v0.2.0 */ - private LinearUnit getLinearUnit(final String name) { + LinearUnit getLinearUnit(final String name) { // see if I am using a function-unit like tempC(100) Objects.requireNonNull(name, "name may not be null"); if (name.contains("(") && name.contains(")")) { @@ -1670,7 +1682,7 @@ public final class UnitDatabase { * @return {@code LinearUnitValue} instance * @since 2020-08-04 */ - private LinearUnitValue getLinearUnitValue(final String name) { + 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); @@ -1695,7 +1707,12 @@ public final class UnitDatabase { try { return UnitPrefix.valueOf(Double.parseDouble(name)); } catch (final NumberFormatException e) { - return this.prefixes.get(name); + final UnitPrefix prefix = this.prefixes.get(name); + if (prefix == null) + throw new NoSuchElementException( + "No prefix with name \"" + name + "\"."); + else + return prefix; } } |