diff options
Diffstat (limited to 'src/unitConverter/UnitsDatabase.java')
-rwxr-xr-x | src/unitConverter/UnitsDatabase.java | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/unitConverter/UnitsDatabase.java b/src/unitConverter/UnitsDatabase.java index d479917..4816db1 100755 --- a/src/unitConverter/UnitsDatabase.java +++ b/src/unitConverter/UnitsDatabase.java @@ -41,12 +41,14 @@ import unitConverter.unit.UnitPrefix; * * @author Adrien Hopkins * @since 2019-01-07 + * @since v0.1.0 */ public final class UnitsDatabase { /** * The units in this system. * * @since 2019-01-07 + * @since v0.1.0 */ private final Map<String, Unit> units; @@ -54,6 +56,7 @@ public final class UnitsDatabase { * The unit prefixes in this system. * * @since 2019-01-14 + * @since v0.1.0 */ private final Map<String, UnitPrefix> prefixes; @@ -61,6 +64,7 @@ public final class UnitsDatabase { * Creates the {@code UnitsDatabase}. * * @since 2019-01-10 + * @since v0.1.0 */ public UnitsDatabase() { this.units = new HashMap<>(); @@ -90,6 +94,7 @@ public final class UnitsDatabase { * @throws NullPointerException * if file is null * @since 2019-01-13 + * @since v0.1.0 */ public void addAllFromFile(final File file) { Objects.requireNonNull(file, "file must not be null."); @@ -128,7 +133,7 @@ public final class UnitsDatabase { if (name.endsWith("-")) { final UnitPrefix prefix; try { - prefix = this.getPrefixFromExpression(expression, name.substring(0, name.length() - 1)); + prefix = this.getPrefixFromExpression(expression); } catch (final IllegalArgumentException e) { System.err.printf("Parsing error on line %d:%n", lineCounter); throw e; @@ -168,6 +173,7 @@ public final class UnitsDatabase { * @throws NullPointerException * if name or unit is null * @since 2019-01-14 + * @since v0.1.0 */ public void addPrefix(final String name, final UnitPrefix prefix) { this.prefixes.put(Objects.requireNonNull(name, "name must not be null."), @@ -184,6 +190,7 @@ public final class UnitsDatabase { * @throws NullPointerException * if unit is null * @since 2019-01-10 + * @since v0.1.0 */ public void addUnit(final String name, final Unit unit) { this.units.put(name, Objects.requireNonNull(unit, "unit must not be null.")); @@ -196,6 +203,7 @@ public final class UnitsDatabase { * name to test * @return if database contains name * @since 2019-01-13 + * @since v0.1.0 */ public boolean containsPrefixlessUnitName(final String name) { return this.units.containsKey(name); @@ -208,6 +216,7 @@ public final class UnitsDatabase { * name to test * @return if database contains name * @since 2019-01-13 + * @since v0.1.0 */ public boolean containsPrefixName(final String name) { return this.prefixes.containsKey(name); @@ -220,6 +229,7 @@ public final class UnitsDatabase { * name to test * @return if database contains name * @since 2019-01-13 + * @since v0.1.0 */ public boolean containsUnitName(final String name) { // check for prefixes @@ -238,13 +248,14 @@ public final class UnitsDatabase { * prefix's name * @return prefix * @since 2019-01-10 + * @since v0.1.0 */ public UnitPrefix getPrefix(final String name) { return this.prefixes.get(name); } /** - * Gets a unit prefix from a prefix expression and a name + * Gets a unit prefix from a prefix expression * <p> * Currently, prefix expressions are much simpler than unit expressions: They are either a number or the name of * another prefix @@ -252,18 +263,16 @@ public final class UnitsDatabase { * * @param expression * expression to input - * @param name - * name of prefix if a new prefix is created * @return prefix * @throws IllegalArgumentException * if expression cannot be parsed * @throws NullPointerException * if any argument is null * @since 2019-01-14 + * @since v0.1.0 */ - public UnitPrefix getPrefixFromExpression(final String expression, final String name) { + public UnitPrefix getPrefixFromExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); - Objects.requireNonNull(name, "name must not be null."); try { return new DefaultUnitPrefix(Double.parseDouble(expression)); @@ -301,6 +310,7 @@ public final class UnitsDatabase { * unit's name * @return unit * @since 2019-01-10 + * @since v0.1.0 */ public Unit getPrefixlessUnit(final String name) { return this.units.get(name); @@ -313,6 +323,7 @@ public final class UnitsDatabase { * unit's name * @return unit * @since 2019-01-10 + * @since v0.1.0 */ public Unit getUnit(final String name) { if (name.contains("^")) { @@ -377,21 +388,19 @@ public final class UnitsDatabase { * <li>The name of a unit, which multiplies or divides the result based on preceding operators</li> * <li>The operators '*' and '/', which multiply and divide (note that just putting two units or values next to each * other is equivalent to multiplication)</li> + * <li>The operator '^' which exponentiates. Exponents must be integers.</li> * <li>A number which is multiplied or divided</li> * </ul> * This method only works with linear units. - * <p> - * If the expression contains just the name of a unit, returns that unit without changing name or symbol. This - * exists for the creation of aliases. - * </p> * - * @param line - * line to parse + * @param expression + * expression to parse * @throws IllegalArgumentException * if the expression cannot be parsed * @throws NullPointerException * if any argument is null * @since 2019-01-07 + * @since v0.1.0 */ public Unit getUnitFromExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); @@ -558,6 +567,7 @@ public final class UnitsDatabase { /** * @return an immutable set of all of the unit names in this database, ignoring prefixes * @since 2019-01-14 + * @since v0.1.0 */ public Set<String> prefixlessUnitNameSet() { return Collections.unmodifiableSet(this.units.keySet()); @@ -566,6 +576,7 @@ public final class UnitsDatabase { /** * @return an immutable set of all of the prefix names in this database * @since 2019-01-14 + * @since v0.1.0 */ public Set<String> prefixNameSet() { return Collections.unmodifiableSet(this.prefixes.keySet()); |