From ec036fdad931fbbd7dec28b864150f8668e91b41 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 10 Apr 2019 21:00:49 -0400 Subject: Edited dimension database code and improved comments. getDimension() now works with exponents, Added a dimension parser, comments can now be in the middle of lines --- CHANGELOG.org | 1 + src/org/unitConverter/UnitsDatabase.java | 181 +++++++++++++++++++------------ 2 files changed, 115 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index 95dc57a..8a79c46 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -6,6 +6,7 @@ All notable changes in this project will be shown in this file. - Moved project to Maven - Downgraded JUnit to 4.11 - BaseUnit is now a subclass of LinearUnit + - Comments can now start in the middle of lines *** Added - GUI for a selection-based unit converter - The UnitDatabase now stores dimensions. diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java index 290a425..69b25d8 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -48,6 +48,32 @@ import org.unitConverter.unit.UnitPrefix; * @since v0.1.0 */ public final class UnitsDatabase { + /** + * The exponent operator + * + * @param base + * base of exponentiation + * @param exponentUnit + * exponent + * @return result + * @since 2019-04-10 + */ + private static final LinearUnit exponent(final LinearUnit base, final LinearUnit exponentUnit) { + // exponent function - first check if o2 is a number, + if (exponentUnit.getBase().equals(SI.SI.getBaseUnit(UnitDimension.EMPTY))) { + // then check if it is an integer, + final double exponent = exponentUnit.getConversionFactor(); + if (DecimalComparison.equals(exponent % 1, 0)) + // then exponentiate + return base.toExponent((int) (exponent % 1 + 0.5)); + else + // not an integer + throw new UnsupportedOperationException("Decimal exponents are currently not supported."); + } else + // not a number + throw new IllegalArgumentException("Exponents must be numbers."); + } + /** * The units in this system. * @@ -80,22 +106,12 @@ public final class UnitsDatabase { 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) -> { - // exponent function - first check if o2 is a number, - if (o2.getBase().equals(SI.SI.getBaseUnit(UnitDimension.EMPTY))) { - // then check if it is an integer, - final double exponent = o2.getConversionFactor(); - if (DecimalComparison.equals(exponent % 1, 0)) - // then exponentiate - return o1.toExponent((int) (exponent % 1 + 0.5)); - else - // not an integer - throw new UnsupportedOperationException( - "Decimal exponents are currently not supported."); - } else - // not a number - throw new IllegalArgumentException("Exponents must be numbers."); - }, 2).build(); + .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1) + .addBinaryOperator("^", UnitsDatabase::exponent, 2).build(); + + 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(); /** * Creates the {@code UnitsDatabase}. @@ -118,7 +134,7 @@ public final class UnitsDatabase { *

* Allowed exceptions: *