From f0f4898f796b9cc26294ba9feb22692143d00a9e Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 13 Apr 2019 15:55:49 -0400 Subject: Unit prefixes now have math methods, and use the expression parser. --- CHANGELOG.org | 3 +- src/org/unitConverter/UnitsDatabase.java | 119 ++++++++++++----------------- src/org/unitConverter/unit/UnitPrefix.java | 36 +++++++++ 3 files changed, 85 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index 46197dc..e7748ba 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -9,11 +9,12 @@ All notable changes in this project will be shown in this file. - In unit files, Comments can now start in the middle of lines - UnitsDatabase.addAllFromFile() has been renamed to loadUnitsFile() *** Added - - GUI for a selection-based unit converter + - A selection-based unit converter which allows you to select two units, input a value, and convert. - The UnitDatabase now stores dimensions. - A system to parse mathematical expressions, used to parse unit expressions. - You can now add and subtract in unit expressions! - Instructions for obtaining unit instances are provided in the relevant classes + - The UnitPrefix interface now provides default times, dividedBy and toExponent methods. ** v0.1.0 NOTE: At this stage, the API is subject to significant change. *** Added diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java index c3d3131..a7e6047 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -33,7 +33,6 @@ import java.util.Set; import org.unitConverter.dimension.UnitDimension; import org.unitConverter.math.DecimalComparison; import org.unitConverter.math.ExpressionParser; -import org.unitConverter.unit.BaseUnit; import org.unitConverter.unit.DefaultUnitPrefix; import org.unitConverter.unit.LinearUnit; import org.unitConverter.unit.SI; @@ -109,6 +108,21 @@ public final class UnitsDatabase { .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1) .addBinaryOperator("^", UnitsDatabase::exponent, 2).build(); + /** + * A parser that can parse unit prefix expressions + * + * @since 2019-04-13 + */ + private final ExpressionParser prefixExpressionParser = new ExpressionParser.Builder<>(this::getPrefix) + .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 0).addSpaceFunction("*") + .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 0) + .addBinaryOperator("^", (o1, o2) -> o1.toExponent(o2.getMultiplier()), 1).build(); + + /** + * A parser that can parse unit dimension expressions. + * + * @since 2019-04-13 + */ private final ExpressionParser unitDimensionParser = new ExpressionParser.Builder<>( this::getDimension).addBinaryOperator("*", (o1, o2) -> o1.times(o2), 0).addSpaceFunction("*") .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 0).build(); @@ -462,7 +476,11 @@ public final class UnitsDatabase { * @since v0.1.0 */ public UnitPrefix getPrefix(final String name) { - return this.prefixes.get(name); + try { + return new DefaultUnitPrefix(Double.parseDouble(name)); + } catch (final NumberFormatException e) { + return this.prefixes.get(name); + } } /** @@ -485,33 +503,20 @@ public final class UnitsDatabase { public UnitPrefix getPrefixFromExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); - try { - return new DefaultUnitPrefix(Double.parseDouble(expression)); - } catch (final NumberFormatException e) { - if (expression.contains("^")) { - final String[] baseAndExponent = expression.split("\\^"); + // attempt to get a unit as an alias first + if (this.containsUnitName(expression)) + return this.getPrefix(expression); - final double base; - try { - base = Double.parseDouble(baseAndExponent[0]); - } catch (final NumberFormatException e2) { - throw new IllegalArgumentException("Base of exponientation must be a number."); - } + // force operators to have spaces + String modifiedExpression = expression; + modifiedExpression = modifiedExpression.replaceAll("\\*", " \\* "); + modifiedExpression = modifiedExpression.replaceAll("/", " / "); + modifiedExpression = modifiedExpression.replaceAll("\\^", " \\^ "); - final int exponent; - try { - exponent = Integer.parseInt(baseAndExponent[baseAndExponent.length - 1]); - } catch (final NumberFormatException e2) { - throw new IllegalArgumentException("Exponent must be an integer."); - } + // fix broken spaces + modifiedExpression = modifiedExpression.replaceAll(" +", " "); - return new DefaultUnitPrefix(Math.pow(base, exponent)); - } else { - if (!this.containsPrefixName(expression)) - throw new IllegalArgumentException("Unrecognized prefix name \"" + expression + "\"."); - return this.getPrefix(expression); - } - } + return this.prefixExpressionParser.parseExpression(modifiedExpression); } /** @@ -541,57 +546,27 @@ public final class UnitsDatabase { final double value = Double.parseDouble(name); return SI.SI.getBaseUnit(UnitDimension.EMPTY).times(value); } catch (final NumberFormatException e) { - if (name.contains("^")) { - final String[] baseAndExponent = name.split("\\^"); - - LinearUnit base; - try { - base = SI.SI.getBaseUnit(UnitDimension.EMPTY).times(Double.parseDouble(baseAndExponent[0])); - } catch (final NumberFormatException e2) { - final Unit unit = this.getUnit(baseAndExponent[0]); - if (unit instanceof LinearUnit) { - base = (LinearUnit) unit; - } else - throw new IllegalArgumentException("Base of exponientation must be a linear or base unit."); - } - - final int exponent; - try { - exponent = Integer.parseInt(baseAndExponent[baseAndExponent.length - 1]); - } catch (final NumberFormatException e2) { - throw new IllegalArgumentException("Exponent must be an integer."); - } - - final LinearUnit exponentiated = base.toExponent(exponent); - if (exponentiated.getConversionFactor() == 1) - return exponentiated.getSystem().getBaseUnit(exponentiated.getDimension()); - else - return exponentiated; - } else { - for (final String prefixName : this.prefixNameSet()) { - // check for a prefix - if (name.startsWith(prefixName)) { - // prefix found! Make sure what comes after it is actually a unit! - final String prefixless = name.substring(prefixName.length()); - if (this.containsUnitName(prefixless)) { - // yep, it's a proper prefix! Get the unit! - final Unit unit = this.getUnit(prefixless); - final UnitPrefix prefix = this.getPrefix(prefixName); - - // Prefixes only work with linear and base units, so make sure it's one of those - if (unit instanceof LinearUnit) { - final LinearUnit linearUnit = (LinearUnit) unit; - return linearUnit.times(prefix.getMultiplier()); - } else if (unit instanceof BaseUnit) { - final BaseUnit baseUnit = (BaseUnit) unit; - return baseUnit.times(prefix.getMultiplier()); - } + for (final String prefixName : this.prefixNameSet()) { + // check for a prefix + if (name.startsWith(prefixName)) { + // prefix found! Make sure what comes after it is actually a unit! + final String prefixless = name.substring(prefixName.length()); + if (this.containsUnitName(prefixless)) { + // yep, it's a proper prefix! Get the unit! + final Unit unit = this.getUnit(prefixless); + final UnitPrefix prefix = this.getPrefix(prefixName); + + // Prefixes only work with linear and base units, so make sure it's one of those + if (unit instanceof LinearUnit) { + final LinearUnit linearUnit = (LinearUnit) unit; + return linearUnit.withPrefix(prefix); } } } - return this.units.get(name); } + return this.units.get(name); } + } /** diff --git a/src/org/unitConverter/unit/UnitPrefix.java b/src/org/unitConverter/unit/UnitPrefix.java index 289e60f..a1609c6 100755 --- a/src/org/unitConverter/unit/UnitPrefix.java +++ b/src/org/unitConverter/unit/UnitPrefix.java @@ -24,10 +24,46 @@ package org.unitConverter.unit; * @since v0.1.0 */ public interface UnitPrefix { + /** + * Divides this prefix by {@code other}. + * + * @param other + * prefix to divide by + * @return quotient of prefixes + * @since 2019-04-13 + */ + default UnitPrefix dividedBy(final UnitPrefix other) { + return new DefaultUnitPrefix(this.getMultiplier() / other.getMultiplier()); + } + /** * @return this prefix's multiplier * @since 2019-01-14 * @since v0.1.0 */ double getMultiplier(); + + /** + * Multiplies this prefix by {@code other}. + * + * @param other + * prefix to multiply by + * @return product of prefixes + * @since 2019-04-13 + */ + default UnitPrefix times(final UnitPrefix other) { + return new DefaultUnitPrefix(this.getMultiplier() * other.getMultiplier()); + } + + /** + * Raises this prefix to an exponent. + * + * @param exponent + * exponent to raise to + * @return result of exponentiation. + * @since 2019-04-13 + */ + default UnitPrefix toExponent(final double exponent) { + return new DefaultUnitPrefix(Math.pow(getMultiplier(), exponent)); + } } -- cgit v1.2.3