From 943496888d18b031be19ba8e7348ec188dc8eb6b Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 22 Mar 2019 17:00:58 -0400 Subject: Made BaseUnit a subclass of LinearUnit and made an expression parser --- src/org/unitConverter/math/ExpressionParser.java | 627 +++++++++++++++++++++++ 1 file changed, 627 insertions(+) create mode 100644 src/org/unitConverter/math/ExpressionParser.java (limited to 'src/org/unitConverter/math/ExpressionParser.java') diff --git a/src/org/unitConverter/math/ExpressionParser.java b/src/org/unitConverter/math/ExpressionParser.java new file mode 100644 index 0000000..e06a58b --- /dev/null +++ b/src/org/unitConverter/math/ExpressionParser.java @@ -0,0 +1,627 @@ +/** + * Copyright (C) 2019 Adrien Hopkins + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.unitConverter.math; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Deque; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.UnaryOperator; + +/** + * An object that can parse expressions with unary or binary operators. + * + * @author Adrien Hopkins + * @param + * type of object that exists in parsed expressions + * @since 2019-03-14 + */ +// TODO: possibly make this class non-final? +public final class ExpressionParser { + /** + * A builder that can create {@code ExpressionParser} instances. + * + * @author Adrien Hopkins + * @param + * type of object that exists in parsed expressions + * @since 2019-03-17 + */ + public static final class Builder { + /** + * A function that obtains a parseable object from a string. For example, an integer {@code ExpressionParser} + * would use {@code Integer::parseInt}. + * + * @since 2019-03-14 + */ + private final Function objectObtainer; + + /** + * A map mapping operator strings to operator functions, for unary operators. + * + * @since 2019-03-14 + */ + private final Map> unaryOperators; + + /** + * A map mapping operator strings to operator functions, for binary operators. + * + * @since 2019-03-14 + */ + private final Map> binaryOperators; + + /** + * Creates the {@code Builder}. + * + * @param objectObtainer + * a function that can turn strings into objects of the type handled by the parser. + * @throws NullPointerException + * if {@code objectObtainer} is null + * @since 2019-03-17 + */ + public Builder(final Function objectObtainer) { + this.objectObtainer = Objects.requireNonNull(objectObtainer, "objectObtainer must not be null."); + this.unaryOperators = new HashMap<>(); + this.binaryOperators = new HashMap<>(); + } + + /** + * Adds a binary operator to the builder. + * + * @param text + * text used to reference the operator, like '+' + * @param operator + * operator to add + * @param priority + * operator's priority, which determines which operators are applied first + * @return this builder + * @throws NullPointerException + * if {@code text} or {@code operator} is null + * @since 2019-03-17 + */ + public Builder addBinaryOperator(final String text, final BinaryOperator operator, final int priority) { + Objects.requireNonNull(text, "text must not be null."); + Objects.requireNonNull(operator, "operator must not be null."); + + // Unfortunately, I cannot use a lambda because the PriorityBinaryOperator requires arguments. + final PriorityBinaryOperator priorityOperator = new PriorityBinaryOperator(priority) { + @Override + public T apply(final T t, final T u) { + return operator.apply(t, u); + } + + }; + this.binaryOperators.put(text, priorityOperator); + return this; + } + + /** + * Adds a unary operator to the builder. + * + * @param text + * text used to reference the operator, like '-' + * @param operator + * operator to add + * @param priority + * operator's priority, which determines which operators are applied first + * @return this builder + * @throws NullPointerException + * if {@code text} or {@code operator} is null + * @since 2019-03-17 + */ + public Builder addUnaryOperator(final String text, final UnaryOperator operator, final int priority) { + Objects.requireNonNull(text, "text must not be null."); + Objects.requireNonNull(operator, "operator must not be null."); + + // Unfortunately, I cannot use a lambda because the PriorityUnaryOperator requires arguments. + final PriorityUnaryOperator priorityOperator = new PriorityUnaryOperator(priority) { + @Override + public T apply(final T t) { + return operator.apply(t); + } + }; + this.unaryOperators.put(text, priorityOperator); + return this; + } + + /** + * @return an {@code ExpressionParser} instance with the properties given to this builder + * @since 2019-03-17 + */ + public ExpressionParser build() { + return new ExpressionParser<>(this.objectObtainer, this.unaryOperators, this.binaryOperators); + } + } + + /** + * A binary operator with a priority field that determines which operators apply first. + * + * @author Adrien Hopkins + * @param + * type of operand and result + * @since 2019-03-17 + */ + private static abstract class PriorityBinaryOperator + implements BinaryOperator, Comparable> { + /** + * The operator's priority. Higher-priority operators are applied before lower-priority operators + */ + private final int priority; + + /** + * Creates the {@code PriorityBinaryOperator}. + * + * @param priority + * operator's priority + * @since 2019-03-17 + */ + public PriorityBinaryOperator(final int priority) { + this.priority = priority; + } + + /** + * Compares this object to another by priority. + * + *

+ * {@inheritDoc} + */ + @Override + public int compareTo(final PriorityBinaryOperator o) { + if (this.priority < o.priority) + return -1; + else if (this.priority > o.priority) + return 1; + else + return 0; + } + + /** + * @return priority + * @since 2019-03-22 + */ + public final int getPriority() { + return this.priority; + } + } + + /** + * A unary operator with a priority field that determines which operators apply first. + * + * @author Adrien Hopkins + * @param + * type of operand and result + * @since 2019-03-17 + */ + private static abstract class PriorityUnaryOperator + implements UnaryOperator, Comparable> { + /** + * The operator's priority. Higher-priority operators are applied before lower-priority operators + */ + private final int priority; + + /** + * Creates the {@code PriorityUnaryOperator}. + * + * @param priority + * operator's priority + * @since 2019-03-17 + */ + public PriorityUnaryOperator(final int priority) { + this.priority = priority; + } + + /** + * Compares this object to another by priority. + * + *

+ * {@inheritDoc} + */ + @Override + public int compareTo(final PriorityUnaryOperator o) { + if (this.priority < o.priority) + return -1; + else if (this.priority > o.priority) + return 1; + else + return 0; + } + + /** + * @return priority + * @since 2019-03-22 + */ + public final int getPriority() { + return this.priority; + } + } + + /** + * The types of tokens that are available. + * + * @author Adrien Hopkins + * @since 2019-03-14 + */ + private static enum TokenType { + OBJECT, UNARY_OPERATOR, BINARY_OPERATOR; + } + + /** + * The opening bracket. + * + * @since 2019-03-22 + */ + public static final char OPENING_BRACKET = '('; + + /** + * The closing bracket. + * + * @since 2019-03-22 + */ + public static final char CLOSING_BRACKET = ')'; + + /** + * Finds the other bracket in a pair of brackets, given the position of one. + * + * @param string + * string that contains brackets + * @param bracketPosition + * position of first bracket + * @return position of matching bracket + * @throws NullPointerException + * if string is null + * @since 2019-03-22 + */ + private static int findBracketPair(final String string, final int bracketPosition) { + Objects.requireNonNull(string, "string must not be null."); + + final char openingBracket = string.charAt(bracketPosition); + + // figure out what closing bracket to look for + final char closingBracket; + switch (openingBracket) { + case '(': + closingBracket = ')'; + break; + case '[': + closingBracket = ']'; + break; + case '{': + closingBracket = '}'; + break; + default: + throw new IllegalArgumentException(String.format("Invalid bracket '%s'", openingBracket)); + } + + // level of brackets. every opening bracket increments this; every closing bracket decrements it + int bracketLevel = 0; + + // iterate over the string to find the closing bracket + for (int currentPosition = bracketPosition; currentPosition < string.length(); currentPosition++) { + final char currentCharacter = string.charAt(currentPosition); + + if (currentCharacter == openingBracket) { + bracketLevel++; + } else if (currentCharacter == closingBracket) { + bracketLevel--; + if (bracketLevel == 0) + return currentPosition; + } + } + + throw new IllegalArgumentException("No matching bracket found."); + } + + public static void main(final String[] args) { + final ExpressionParser numberParser = new ExpressionParser.Builder<>(Integer::parseInt) + .addBinaryOperator("+", (o1, o2) -> o1 + o2, 0).addBinaryOperator("*", (o1, o2) -> o1 * o2, 1) + .addBinaryOperator("^", (o1, o2) -> (int) Math.pow(o1, o2), 2).build(); + System.out.println(numberParser.convertExpressionToReversePolish("(1 + 2) ^ 5 * 3")); + System.out.println(numberParser.parseExpression("(1 + 2) ^ 5 * 3")); // 729 + } + + /** + * Swaps two elements in a list. Modifies the list passed in instead of returning a modified list. + * + * @param list + * list to swap elements + * @param firstIndex + * index of first element to swap + * @param otherIndex + * index of other element to swap + * @throws NullPointerException + * if list is null + * @since 2019-03-20 + */ + private static void swap(final List list, final int firstIndex, final int otherIndex) { + Objects.requireNonNull(list, "list must not be null."); + final E temp = list.get(firstIndex); + list.set(firstIndex, list.get(otherIndex)); + list.set(otherIndex, temp); + } + + /** + * A function that obtains a parseable object from a string. For example, an integer {@code ExpressionParser} would + * use {@code Integer::parseInt}. + * + * @since 2019-03-14 + */ + private final Function objectObtainer; + + /** + * A map mapping operator strings to operator functions, for unary operators. + * + * @since 2019-03-14 + */ + private final Map> unaryOperators; + + /** + * A map mapping operator strings to operator functions, for binary operators. + * + * @since 2019-03-14 + */ + private final Map> binaryOperators; + + /** + * Creates the {@code ExpressionParser}. + * + * @param objectObtainer + * function to get objects from strings + * @param unaryOperators + * operators available to the parser + * @since 2019-03-14 + */ + private ExpressionParser(final Function objectObtainer, + final Map> unaryOperators, + final Map> binaryOperators) { + this.objectObtainer = objectObtainer; + this.unaryOperators = unaryOperators; + this.binaryOperators = binaryOperators; + } + + /** + * Converts a given mathematical expression to reverse Polish notation (operators after operands). + *

+ * For example,
+ * {@code 2 * (3 + 4)}
+ * becomes
+ * {@code 2 3 4 + *}. + * + * @param expression + * expression + * @return expression in RPN + * @since 2019-03-17 + */ + private String convertExpressionToReversePolish(final String expression) { + Objects.requireNonNull(expression, "expression must not be null."); + + final List components = new ArrayList<>(); + + // the part of the expression remaining to parse + String partialExpression = expression; + + // find and deal with brackets + while (partialExpression.indexOf(OPENING_BRACKET) != -1) { + final int openingBracketPosition = partialExpression.indexOf(OPENING_BRACKET); + final int closingBracketPosition = findBracketPair(partialExpression, openingBracketPosition); + components.addAll(Arrays.asList(partialExpression.substring(0, openingBracketPosition).split(" "))); + components.add(this.convertExpressionToReversePolish( + partialExpression.substring(openingBracketPosition + 1, closingBracketPosition))); + partialExpression = partialExpression.substring(closingBracketPosition + 1); + } + + // add everything else + components.addAll(Arrays.asList(partialExpression.split(" "))); + + // remove empty entries + while (components.contains("")) { + components.remove(""); + } + + // turn the expression into reverse Polish + while (true) { + final int highestPriorityOperatorPosition = this.findHighestPriorityOperatorPosition(components); + if (highestPriorityOperatorPosition == -1) { + break; + } + + switch (this.getTokenType(components.get(highestPriorityOperatorPosition))) { + case UNARY_OPERATOR: + final String unaryOperator = components.remove(highestPriorityOperatorPosition); + final String operand = components.remove(highestPriorityOperatorPosition); + components.add(highestPriorityOperatorPosition, operand + " " + unaryOperator); + break; + case BINARY_OPERATOR: + final String binaryOperator = components.remove(highestPriorityOperatorPosition); + final String operand1 = components.remove(highestPriorityOperatorPosition - 1); + final String operand2 = components.remove(highestPriorityOperatorPosition - 1); + components.add(highestPriorityOperatorPosition - 1, + operand2 + " " + operand1 + " " + binaryOperator); + break; + default: + throw new AssertionError("Expected operator, found non-operator."); + } + } + + // join all of the components together, then ensure there is only one space in a row + String expressionRPN = String.join(" ", components).replaceAll(" +", " "); + + while (expressionRPN.charAt(0) == ' ') { + expressionRPN = expressionRPN.substring(1); + } + while (expressionRPN.charAt(expressionRPN.length() - 1) == ' ') { + expressionRPN = expressionRPN.substring(0, expressionRPN.length() - 1); + } + return expressionRPN; + + // TODO method stub org.unitConverter.expressionParser.ExpressionParser.convertExpressionToPolish(expression) + } + + /** + * Finds the position of the highest-priority operator in a list + * + * @param components + * components to test + * @param blacklist + * positions of operators that should be ignored + * @return position of highest priority, or -1 if the list contains no operators + * @throws NullPointerException + * if components is null + * @since 2019-03-22 + */ + private int findHighestPriorityOperatorPosition(final List components) { + Objects.requireNonNull(components, "components must not be null."); + // find highest priority + int maxPriority = Integer.MIN_VALUE; + int maxPriorityPosition = -1; + + // go over components one by one + // if it is an operator, test its priority to see if it's max + // if it is, update maxPriority and maxPriorityPosition + for (int i = 0; i < components.size(); i++) { + + switch (this.getTokenType(components.get(i))) { + case UNARY_OPERATOR: + final PriorityUnaryOperator unaryOperator = this.unaryOperators.get(components.get(i)); + final int unaryPriority = unaryOperator.getPriority(); + + if (unaryPriority > maxPriority) { + maxPriority = unaryPriority; + maxPriorityPosition = i; + } + break; + case BINARY_OPERATOR: + final PriorityBinaryOperator binaryOperator = this.binaryOperators.get(components.get(i)); + final int binaryPriority = binaryOperator.getPriority(); + + if (binaryPriority > maxPriority) { + maxPriority = binaryPriority; + maxPriorityPosition = i; + } + break; + default: + break; + } + } + + // max priority position found + return maxPriorityPosition; + } + + /** + * Determines whether an inputted string is an object or an operator + * + * @param token + * string to input + * @return type of token it is + * @throws NullPointerException + * if {@code expression} is null + * @since 2019-03-14 + */ + private TokenType getTokenType(final String token) { + Objects.requireNonNull(token, "token must not be null."); + + if (this.unaryOperators.containsKey(token)) + return TokenType.UNARY_OPERATOR; + else if (this.binaryOperators.containsKey(token)) + return TokenType.BINARY_OPERATOR; + else + return TokenType.OBJECT; + } + + /** + * Parses an expression. + * + * @param expression + * expression to parse + * @return result + * @throws NullPointerException + * if {@code expression} is null + * @since 2019-03-14 + */ + public T parseExpression(final String expression) { + return this.parseReversePolishExpression(this.convertExpressionToReversePolish(expression)); + } + + /** + * Parses an expression expressed in reverse Polish notation. + * + * @param expression + * expression to parse + * @return result + * @throws NullPointerException + * if {@code expression} is null + * @since 2019-03-14 + */ + private T parseReversePolishExpression(final String expression) { + Objects.requireNonNull(expression, "expression must not be null."); + + final Deque stack = new ArrayDeque<>(); + + // iterate over every item in the expression, then + for (final String item : expression.split(" ")) { + // choose a path based on what kind of thing was just read + switch (this.getTokenType(item)) { + + case BINARY_OPERATOR: + if (stack.size() < 2) + throw new IllegalStateException(String.format( + "Attempted to call binary operator %s with only %d arguments.", item, stack.size())); + + // get two arguments and operator, then apply! + final T o1 = stack.pop(); + final T o2 = stack.pop(); + final BinaryOperator binaryOperator = this.binaryOperators.get(item); + + stack.push(binaryOperator.apply(o1, o2)); + break; + + case OBJECT: + // just add it to the stack + stack.push(this.objectObtainer.apply(item)); + break; + + case UNARY_OPERATOR: + if (stack.size() < 1) + throw new IllegalStateException(String.format( + "Attempted to call unary operator %s with only %d arguments.", item, stack.size())); + + // get one argument and operator, then apply! + final T o = stack.pop(); + final UnaryOperator unaryOperator = this.unaryOperators.get(item); + + stack.push(unaryOperator.apply(o)); + break; + default: + throw new AssertionError( + String.format("Internal error: Invalid token type %s.", this.getTokenType(item))); + + } + } + + // return answer, or throw an exception if I can't + if (stack.size() > 1) + throw new IllegalStateException("Computation ended up with more than one answer."); + else if (stack.size() == 0) + throw new IllegalStateException("Computation ended up without an answer."); + return stack.pop(); + } +} -- cgit v1.2.3 From bfe1f266922bffd3c0c8d8906535be7621217e7a Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 22 Mar 2019 19:35:30 -0400 Subject: Unit Expressions are now parsed with the expression parser. Addition and subtraction are now possible. --- CHANGELOG.org | 1 + src/org/unitConverter/UnitsDatabase.java | 326 +++++++++------------ .../converterGUI/UnitConverterGUI.java | 7 +- src/org/unitConverter/math/ExpressionParser.java | 108 ++++--- unitsfile.txt | 2 +- 5 files changed, 213 insertions(+), 231 deletions(-) (limited to 'src/org/unitConverter/math/ExpressionParser.java') diff --git a/CHANGELOG.org b/CHANGELOG.org index 5baf980..95dc57a 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -10,6 +10,7 @@ All notable changes in this project will be shown in this file. - GUI for a selection-based unit converter - 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! ** 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 3af1c8d..481ce93 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -21,13 +21,17 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import org.unitConverter.dimension.UnitDimension; +import org.unitConverter.math.DecimalComparison; +import org.unitConverter.math.ExpressionParser; import org.unitConverter.unit.AbstractUnit; import org.unitConverter.unit.BaseUnit; import org.unitConverter.unit.DefaultUnitPrefix; @@ -67,6 +71,32 @@ public final class UnitsDatabase { */ private final Map dimensions; + /** + * A parser that can parse unit expressions. + * + * @since 2019-03-22 + */ + private final ExpressionParser 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) -> { + // 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(); + /** * Creates the {@code UnitsDatabase}. * @@ -298,6 +328,37 @@ public final class UnitsDatabase { return this.dimensions.get(name); } + /** + * Gets a unit. If it is linear, cast it to a LinearUnit and return it. Otherwise, throw an + * {@code IllegalArgumentException}. + * + * @param name + * unit's name + * @return unit + * @since 2019-03-22 + */ + private LinearUnit getLinearUnit(final String name) { + // see if I am using a function-unit like tempC(100) + if (name.contains("(") && name.contains(")")) { + // break it into function name and value + final List parts = Arrays.asList(name.split("\\(")); + 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(parts.get(1).substring(0, parts.get(1).length() - 1)); + return unit.getBase().times(unit.convertToBase(value)); + } else { + // get a linear unit + final Unit unit = this.getUnit(name); + if (unit instanceof LinearUnit) + return (LinearUnit) unit; + else + throw new IllegalArgumentException(String.format("%s is not a linear unit.", name)); + } + } + /** * Gets a unit prefix from the database from its name * @@ -383,55 +444,60 @@ public final class UnitsDatabase { * @since v0.1.0 */ public Unit getUnit(final String name) { - 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."); - } + try { + 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("\\^"); - final int exponent; - try { - exponent = Integer.parseInt(baseAndExponent[baseAndExponent.length - 1]); - } catch (final NumberFormatException e2) { - throw new IllegalArgumentException("Exponent must be an integer."); - } + 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 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()); + 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()); + } } } } + return this.units.get(name); } - return this.units.get(name); } } @@ -453,165 +519,39 @@ public final class UnitsDatabase { * @throws IllegalArgumentException * if the expression cannot be parsed * @throws NullPointerException - * if any argument is null + * if expression is null * @since 2019-01-07 * @since v0.1.0 */ public Unit getUnitFromExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); - // parse the expression - // start with an "empty" unit then apply operations on it - LinearUnit unit = SI.SI.getBaseUnit(UnitDimension.EMPTY); - boolean dividing = false; - - // if I'm just creating an alias, just create one instead of going through the parsing process - if (!expression.contains(" ") && !expression.contains("*") && !expression.contains("/") - && !expression.contains("(") && !expression.contains(")") && !expression.contains("^")) { - try { - return SI.SI.getBaseUnit(UnitDimension.EMPTY).times(Double.parseDouble(expression)); - } catch (final NumberFormatException e) { - if (!this.containsUnitName(expression)) - throw new IllegalArgumentException("Unrecognized unit name \"" + expression + "\"."); - return this.getUnit(expression); - } - } - - // \\* means "asterisk", * is reserved - for (final String part : expression.replaceAll("\\*", " \\* ").replaceAll("/", " / ").replaceAll(" \\^", "\\^") - .replaceAll("\\^ ", "\\^").split(" ")) { - if ("".equals(part)) { - continue; - } - // "unit1 unit2" is the same as "unit1 * unit2", so multiplication signs do nothing - if ("*".equals(part)) { - continue; - } - // When I reach a division sign, don't parse a unit, instead tell myself I'm going to divide the next - // thing - if ("/".equals(part) || "per".equals(part)) { - dividing = true; - continue; - } - - try { - final double partAsNumber = Double.parseDouble(part); // if this works, it's a number - // this code should not throw any exceptions, so I'm going to throw AssertionErrors if it does - try { - if (dividing) { - unit = unit.dividedBy(partAsNumber); - dividing = false; - } else { - unit = unit.times(partAsNumber); - } - } catch (final Exception e) { - throw new AssertionError(e); - } - } catch (final NumberFormatException e) { - // it's a unit, try that - - if (part.contains("(") && part.endsWith(")")) { - // the unitsfile is looking for a nonlinear unit - final String[] unitAndValue = part.split("\\("); - - // this will work because I've checked that it contains a ( - final String unitName = unitAndValue[0]; - final String valueStringWithBracket = unitAndValue[unitAndValue.length - 1]; - final String valueString = valueStringWithBracket.substring(0, valueStringWithBracket.length() - 1); - final double value; - - // try to get the value - else throw an error - try { - value = Double.parseDouble(valueString); - } catch (final NumberFormatException e2) { - throw new IllegalArgumentException("Unparseable value " + valueString); - } - - // get this unit in a linear form - if (!this.containsPrefixlessUnitName(unitName)) - throw new IllegalArgumentException("Unrecognized unit name \"" + part + "\"."); - final Unit partUnit = this.getPrefixlessUnit(unitName); - final LinearUnit multiplier = partUnit.getBase().times(partUnit.convertToBase(value)); - - // finally, add it to the expression - if (dividing) { - unit = unit.dividedBy(multiplier); - dividing = false; - } else { - unit = unit.times(multiplier); - } - } else { - // check for exponientation - if (part.contains("^")) { - final String[] valueAndExponent = part.split("\\^"); - // this will always work because of the contains check - final String valueString = valueAndExponent[0]; - final String exponentString = valueAndExponent[valueAndExponent.length - 1]; - - LinearUnit value; - - // first, try to get the value - try { - final double valueAsNumber = Double.parseDouble(valueString); - - value = SI.SI.getBaseUnit(UnitDimension.EMPTY).times(valueAsNumber); - } catch (final NumberFormatException e2) { - - // look for a unit - if (!this.containsUnitName(valueString)) - throw new IllegalArgumentException("Unrecognized unit name \"" + valueString + "\"."); - final Unit valueUnit = this.getUnit(valueString); - - // try to turn the value into a linear unit - if (valueUnit instanceof LinearUnit) { - value = (LinearUnit) valueUnit; - } else - throw new IllegalArgumentException("Only linear and base units can be exponientated."); - } - - // now, try to get the exponent - final int exponent; - try { - exponent = Integer.parseInt(exponentString); - } catch (final NumberFormatException e2) { - throw new IllegalArgumentException("Exponents must be integers."); - } - - final LinearUnit exponientated = value.toExponent(exponent); - - if (dividing) { - unit = unit.dividedBy(exponientated); - dividing = false; - } else { - unit = unit.times(exponientated); - } - } else { - // no exponent - look for a unit - // the unitsfile is looking for a linear unit - if (!this.containsUnitName(part)) - throw new IllegalArgumentException("Unrecognized unit name \"" + part + "\"."); - final Unit other = this.getUnit(part); - if (other instanceof LinearUnit) { - if (dividing) { - unit = unit.dividedBy((LinearUnit) other); - dividing = false; - } else { - unit = unit.times((LinearUnit) other); - } - } else - throw new IllegalArgumentException( - "Only linear or base units can be multiplied and divided. If you want to use a non-linear unit, try the format UNITNAME(VALUE)."); - } - } + // 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("-", " - "); + modifiedExpression = modifiedExpression.replaceAll("\\*", " \\* "); + modifiedExpression = modifiedExpression.replaceAll("/", " / "); + modifiedExpression = modifiedExpression.replaceAll("\\^", " \\^ "); + + // fix broken spaces + modifiedExpression = modifiedExpression.replaceAll(" +", " "); + + // the previous operation breaks negative numbers, fix them! + // (i.e. -2 becomes - 2) + for (int i = 2; i < modifiedExpression.length(); i++) { + if (modifiedExpression.charAt(i) == '-' + && Arrays.asList('+', '-', '*', '/', '^').contains(modifiedExpression.charAt(i - 2))) { + // found a broken negative number + modifiedExpression = modifiedExpression.substring(0, i + 1) + modifiedExpression.substring(i + 2); } } - // replace conversion-factor-1 linear units with base units - // this improves the autogenerated names, allowing them to use derived SI names - if (unit != null && unit.getConversionFactor() == 1) - return unit.getSystem().getBaseUnit(unit.getDimension()); - else - return unit; + return this.unitExpressionParser.parseExpression(modifiedExpression); } /** diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index a70e971..867211c 100755 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -175,7 +175,12 @@ final class UnitConverterGUI { // try to parse to final Unit to; try { - to = this.units.getUnitFromExpression(toUnitString); + // if it's a unit, convert to that + if (this.units.containsUnitName(toUnitString)) { + to = this.units.getUnit(toUnitString); + } else { + to = this.units.getUnitFromExpression(toUnitString); + } } catch (final IllegalArgumentException e) { this.view.showErrorDialog("Parse Error", "Could not recognize text in To entry: " + e.getMessage()); return; diff --git a/src/org/unitConverter/math/ExpressionParser.java b/src/org/unitConverter/math/ExpressionParser.java index e06a58b..f34a0c2 100644 --- a/src/org/unitConverter/math/ExpressionParser.java +++ b/src/org/unitConverter/math/ExpressionParser.java @@ -55,6 +55,13 @@ public final class ExpressionParser { */ private final Function objectObtainer; + /** + * The function of the space as an operator (like 3 x y) + * + * @since 2019-03-22 + */ + private String spaceFunction = null; + /** * A map mapping operator strings to operator functions, for unary operators. * @@ -114,6 +121,24 @@ public final class ExpressionParser { return this; } + /** + * Adds a function for spaces. You must use the text of an existing binary operator. + * + * @param operator + * text of operator to use + * @return this builder + * @since 2019-03-22 + */ + public Builder addSpaceFunction(final String operator) { + Objects.requireNonNull(operator, "operator must not be null."); + + if (!this.binaryOperators.containsKey(operator)) + throw new IllegalArgumentException(String.format("Could not find binary operator '%s'", operator)); + + this.spaceFunction = operator; + return this; + } + /** * Adds a unary operator to the builder. * @@ -148,7 +173,8 @@ public final class ExpressionParser { * @since 2019-03-17 */ public ExpressionParser build() { - return new ExpressionParser<>(this.objectObtainer, this.unaryOperators, this.binaryOperators); + return new ExpressionParser<>(this.objectObtainer, this.unaryOperators, this.binaryOperators, + this.spaceFunction); } } @@ -330,34 +356,6 @@ public final class ExpressionParser { throw new IllegalArgumentException("No matching bracket found."); } - public static void main(final String[] args) { - final ExpressionParser numberParser = new ExpressionParser.Builder<>(Integer::parseInt) - .addBinaryOperator("+", (o1, o2) -> o1 + o2, 0).addBinaryOperator("*", (o1, o2) -> o1 * o2, 1) - .addBinaryOperator("^", (o1, o2) -> (int) Math.pow(o1, o2), 2).build(); - System.out.println(numberParser.convertExpressionToReversePolish("(1 + 2) ^ 5 * 3")); - System.out.println(numberParser.parseExpression("(1 + 2) ^ 5 * 3")); // 729 - } - - /** - * Swaps two elements in a list. Modifies the list passed in instead of returning a modified list. - * - * @param list - * list to swap elements - * @param firstIndex - * index of first element to swap - * @param otherIndex - * index of other element to swap - * @throws NullPointerException - * if list is null - * @since 2019-03-20 - */ - private static void swap(final List list, final int firstIndex, final int otherIndex) { - Objects.requireNonNull(list, "list must not be null."); - final E temp = list.get(firstIndex); - list.set(firstIndex, list.get(otherIndex)); - list.set(otherIndex, temp); - } - /** * A function that obtains a parseable object from a string. For example, an integer {@code ExpressionParser} would * use {@code Integer::parseInt}. @@ -380,21 +378,33 @@ public final class ExpressionParser { */ private final Map> binaryOperators; + /** + * The operator for space, or null if spaces have no function. + * + * @since 2019-03-22 + */ + private final String spaceOperator; + /** * Creates the {@code ExpressionParser}. * * @param objectObtainer * function to get objects from strings * @param unaryOperators - * operators available to the parser + * unary operators available to the parser + * @param binaryOperators + * binary operators available to the parser + * @param spaceOperator + * operator used by spaces * @since 2019-03-14 */ private ExpressionParser(final Function objectObtainer, final Map> unaryOperators, - final Map> binaryOperators) { + final Map> binaryOperators, final String spaceOperator) { this.objectObtainer = objectObtainer; this.unaryOperators = unaryOperators; this.binaryOperators = binaryOperators; + this.spaceOperator = spaceOperator; } /** @@ -422,10 +432,26 @@ public final class ExpressionParser { while (partialExpression.indexOf(OPENING_BRACKET) != -1) { final int openingBracketPosition = partialExpression.indexOf(OPENING_BRACKET); final int closingBracketPosition = findBracketPair(partialExpression, openingBracketPosition); - components.addAll(Arrays.asList(partialExpression.substring(0, openingBracketPosition).split(" "))); - components.add(this.convertExpressionToReversePolish( - partialExpression.substring(openingBracketPosition + 1, closingBracketPosition))); - partialExpression = partialExpression.substring(closingBracketPosition + 1); + + // check for function + if (openingBracketPosition > 0 && partialExpression.charAt(openingBracketPosition - 1) != ' ') { + // function like sin(2) or tempF(32) + // find the position of the last space + int spacePosition = openingBracketPosition; + while (spacePosition >= 0 && partialExpression.charAt(spacePosition) != ' ') { + spacePosition--; + } + // then split the function into pre-function and function, using the space position + components.addAll(Arrays.asList(partialExpression.substring(0, spacePosition + 1).split(" "))); + components.add(partialExpression.substring(spacePosition + 1, closingBracketPosition + 1)); + partialExpression = partialExpression.substring(closingBracketPosition + 1); + } else { + // normal brackets like (1 + 2) * (3 / 5) + components.addAll(Arrays.asList(partialExpression.substring(0, openingBracketPosition).split(" "))); + components.add(this.convertExpressionToReversePolish( + partialExpression.substring(openingBracketPosition + 1, closingBracketPosition))); + partialExpression = partialExpression.substring(closingBracketPosition + 1); + } } // add everything else @@ -436,6 +462,16 @@ public final class ExpressionParser { components.remove(""); } + // deal with space multiplication (x y) + if (this.spaceOperator != null) { + for (int i = 0; i < components.size() - 1; i++) { + if (this.getTokenType(components.get(i)) == TokenType.OBJECT + && this.getTokenType(components.get(i + 1)) == TokenType.OBJECT) { + components.add(++i, this.spaceOperator); + } + } + } + // turn the expression into reverse Polish while (true) { final int highestPriorityOperatorPosition = this.findHighestPriorityOperatorPosition(components); @@ -472,7 +508,7 @@ public final class ExpressionParser { } return expressionRPN; - // TODO method stub org.unitConverter.expressionParser.ExpressionParser.convertExpressionToPolish(expression) + // TODO document org.unitConverter.expressionParser.ExpressionParser.convertExpressionToPolish(expression) } /** diff --git a/unitsfile.txt b/unitsfile.txt index 78e51f7..2455c8a 100755 --- a/unitsfile.txt +++ b/unitsfile.txt @@ -136,7 +136,7 @@ steradian m^2 / m^2 sr steradian degree 360 / tau * radian deg degree -° degree +° degree # Nonlinear units, which are not supported by the file reader and must be defined manually # Use tempC(100) for 100 degrees Celsius -- cgit v1.2.3 From 91ee53876aeeb52e980dd1fa976fae06d890ba19 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Wed, 10 Apr 2019 19:31:59 -0400 Subject: Removed AbstractUnit's unit counting functionnality. The startup unit count is now performed by the UnitDatabase. --- src/org/unitConverter/UnitsDatabase.java | 15 ++++-- .../converterGUI/UnitConverterGUI.java | 18 ++++++-- src/org/unitConverter/math/ExpressionParser.java | 3 ++ src/org/unitConverter/unit/AbstractUnit.java | 54 ---------------------- src/org/unitConverter/unit/BaseUnit.java | 8 ++++ 5 files changed, 35 insertions(+), 63 deletions(-) (limited to 'src/org/unitConverter/math/ExpressionParser.java') diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java index 481ce93..290a425 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; @@ -32,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.AbstractUnit; import org.unitConverter.unit.BaseUnit; import org.unitConverter.unit.DefaultUnitPrefix; import org.unitConverter.unit.LinearUnit; @@ -186,10 +186,7 @@ public final class UnitsDatabase { System.err.printf("Parsing error on line %d:%n", lineCounter); throw e; } - AbstractUnit.incrementUnitCounter(); - if (unit instanceof BaseUnit) { - AbstractUnit.incrementBaseUnitCounter(); - } + this.addUnit(name, unit); } } @@ -563,6 +560,14 @@ public final class UnitsDatabase { return Collections.unmodifiableSet(this.units.keySet()); } + /** + * @return an immutable set of all of the units in this database, ignoring prefixes. + * @since 2019-04-10 + */ + public Set prefixlessUnitSet() { + return Collections.unmodifiableSet(new HashSet<>(this.units.values())); + } + /** * @return an immutable set of all of the prefix names in this database * @since 2019-01-14 diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 867211c..fd40ff4 100755 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -47,7 +47,7 @@ import javax.swing.ListSelectionModel; import org.unitConverter.UnitsDatabase; import org.unitConverter.dimension.StandardDimensions; import org.unitConverter.dimension.UnitDimension; -import org.unitConverter.unit.AbstractUnit; +import org.unitConverter.unit.BaseUnit; import org.unitConverter.unit.NonlinearUnits; import org.unitConverter.unit.SI; import org.unitConverter.unit.Unit; @@ -143,8 +143,13 @@ final class UnitConverterGUI { this.prefixNamesFiltered = new DelegateListModel<>(new ArrayList<>(this.units.prefixNameSet())); this.prefixNamesFiltered.sort(this.prefixNameComparator); // sorts it using my comparator - System.out.printf("Successfully loaded %d units (%d base units)", AbstractUnit.getUnitCount(), - AbstractUnit.getBaseUnitCount()); + // a Predicate that returns true iff the argument is a full base unit + final Predicate isFullBase = unit -> unit instanceof BaseUnit && ((BaseUnit) unit).isFullBase(); + + // print out unit counts + System.out.printf("Successfully loaded %d units with %d unit names (%d base units).%n", + this.units.prefixlessUnitSet().size(), this.units.prefixlessUnitNameSet().size(), + this.units.prefixlessUnitSet().stream().filter(isFullBase).count()); } /** @@ -162,6 +167,11 @@ final class UnitConverterGUI { final String fromUnitString = this.view.getFromText(); final String toUnitString = this.view.getToText(); + if (fromUnitString.isEmpty()) { + this.view.showErrorDialog("Parse Error", "Please enter a unit expression in the From: box."); + return; + } + // try to parse from final Unit from; try { @@ -175,8 +185,8 @@ final class UnitConverterGUI { // try to parse to final Unit to; try { - // if it's a unit, convert to that if (this.units.containsUnitName(toUnitString)) { + // if it's a unit, convert to that to = this.units.getUnit(toUnitString); } else { to = this.units.getUnitFromExpression(toUnitString); diff --git a/src/org/unitConverter/math/ExpressionParser.java b/src/org/unitConverter/math/ExpressionParser.java index f34a0c2..b56fa71 100644 --- a/src/org/unitConverter/math/ExpressionParser.java +++ b/src/org/unitConverter/math/ExpressionParser.java @@ -479,6 +479,9 @@ public final class ExpressionParser { break; } + // swap components based on what kind of operator there is + // 1 + 2 becomes 2 1 + + // - 1 becomes 1 - switch (this.getTokenType(components.get(highestPriorityOperatorPosition))) { case UNARY_OPERATOR: final String unaryOperator = components.remove(highestPriorityOperatorPosition); diff --git a/src/org/unitConverter/unit/AbstractUnit.java b/src/org/unitConverter/unit/AbstractUnit.java index 6088960..a0d6f7e 100644 --- a/src/org/unitConverter/unit/AbstractUnit.java +++ b/src/org/unitConverter/unit/AbstractUnit.java @@ -28,60 +28,6 @@ import org.unitConverter.dimension.UnitDimension; * @since v0.1.0 */ public abstract class AbstractUnit implements Unit { - /** - * The number of units created, including base units. - * - * @since 2019-01-02 - * @since v0.1.0 - */ - private static long unitCount = 0; - - /** - * The number of base units created. - * - * @since 2019-01-02 - * @since v0.1.0 - */ - private static long baseUnitCount = 0; - - /** - * @return number of base units created - * @since 2019-01-02 - * @since v0.1.0 - */ - public static final long getBaseUnitCount() { - return baseUnitCount; - } - - /** - * @return number of units created - * @since 2019-01-02 - * @since v0.1.0 - */ - public static final long getUnitCount() { - return unitCount; - } - - /** - * Increments the number of base units. - * - * @since 2019-01-15 - * @since v0.1.0 - */ - public static final void incrementBaseUnitCounter() { - baseUnitCount++; - } - - /** - * Increments the number of units. - * - * @since 2019-01-15 - * @since v0.1.0 - */ - public static final void incrementUnitCounter() { - unitCount++; - } - /** * The dimension, or what the unit measures. * diff --git a/src/org/unitConverter/unit/BaseUnit.java b/src/org/unitConverter/unit/BaseUnit.java index 2def48e..643272f 100755 --- a/src/org/unitConverter/unit/BaseUnit.java +++ b/src/org/unitConverter/unit/BaseUnit.java @@ -84,6 +84,14 @@ public final class BaseUnit extends LinearUnit { return new BaseUnit(this.getDimension().dividedBy(divisor.getDimension()), this.getSystem()); } + /** + * @return true if the unit is a "full base" unit like the metre or second. + * @since 2019-04-10 + */ + public final boolean isFullBase() { + return this.isFullBase; + } + /** * Returns the product of this unit and another. *

-- cgit v1.2.3 From 63dd50e5d7a5daa0bcbdd00608543d4572c870ea Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 13 Apr 2019 19:48:25 -0400 Subject: Edited the UnitsDatabase API; it now favours prefixless units. --- CHANGELOG.org | 2 + src/org/unitConverter/UnitsDatabase.java | 753 ++++++++++++++++++--- .../converterGUI/UnitConverterGUI.java | 18 +- src/org/unitConverter/math/ExpressionParser.java | 2 - 4 files changed, 689 insertions(+), 86 deletions(-) (limited to 'src/org/unitConverter/math/ExpressionParser.java') diff --git a/CHANGELOG.org b/CHANGELOG.org index e7748ba..db9766b 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -3,6 +3,8 @@ All notable changes in this project will be shown in this file. ** Unreleased *** Changed + - When searching for units, units with no prefixes are searched for before prefixed units + - Smaller prefixes are searched for before larger prefixes - Moved project to Maven - Downgraded JUnit to 4.11 - BaseUnit is now a subclass of LinearUnit diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java index a7e6047..9749e9c 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -21,14 +21,20 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; +import java.util.AbstractSet; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; import org.unitConverter.dimension.UnitDimension; import org.unitConverter.math.DecimalComparison; @@ -40,13 +46,652 @@ import org.unitConverter.unit.Unit; import org.unitConverter.unit.UnitPrefix; /** - * A database of units and prefixes, and their names. + * A database of units, prefixes and dimensions, and their names. * * @author Adrien Hopkins * @since 2019-01-07 * @since v0.1.0 */ public final class UnitsDatabase { + /** + * A map for units that allows the use of prefixes. + *

+ * As this map implementation is intended to be used as a sort of "augmented view" of a unit and prefix map, it is + * unmodifiable but instead reflects the changes to the maps passed into it. Do not edit this map, instead edit the + * maps that were passed in during construction. + *

+ *

+ * The rules for applying prefixes onto units are the following: + *

    + *
  • Prefixes can only be applied to linear units.
  • + *
  • Before attempting to search for prefixes in a unit name, this map will first search for a unit name. So, if + * there are two units, "B" and "AB", and a prefix "A", this map will favour the unit "AB" over the unit "B" with + * the prefix "A", even though they have the same string.
  • + *
  • Shorter prefixes are preferred to longer prefixes. So, if you have units "BC" and "C", and prefixes "AB" and + * "A", inputting "ABC" will return the unit "BC" with the prefix "A", not "C" with the prefix "AB".
  • + *
+ *

+ * + * @author Adrien Hopkins + * @since 2019-04-13 + */ + private static final class PrefixedUnitMap implements Map { + /** + * The class used for entry sets. + * + * @author Adrien Hopkins + * @since 2019-04-13 + */ + private static final class PrefixedUnitEntrySet extends AbstractSet> { + // the map that created this set + private final PrefixedUnitMap map; + + /** + * Creates the {@code PrefixedUnitNameSet}. + * + * @param map + * @since 2019-04-13 + */ + public PrefixedUnitEntrySet(final PrefixedUnitMap map) { + this.map = map; + } + + @Override + public boolean add(final Map.Entry e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(final Collection> c) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean contains(final Object o) { + // get the entry + final Entry entry; + + try { + // This is OK because I'm in a try-catch block. + @SuppressWarnings("unchecked") + final Entry tempEntry = (Entry) o; + entry = tempEntry; + } catch (final ClassCastException e) { + 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) + if (!this.contains(o)) + return false; + return true; + } + + @Override + public boolean isEmpty() { + return this.map.isEmpty(); + } + + @Override + public Iterator> iterator() { + return new Iterator>() { + // position in the unit list + int unitNamePosition = -1; + // the indices of the prefixes attached to the current unit + List prefixCoordinates = new ArrayList<>(); + + List unitNames = new ArrayList<>(PrefixedUnitEntrySet.this.map.units.keySet()); + List prefixNames = new ArrayList<>(PrefixedUnitEntrySet.this.map.prefixes.keySet()); + + @Override + public boolean hasNext() { + if (this.unitNames.isEmpty()) + return false; + else { + if (this.prefixNames.isEmpty()) + return this.unitNamePosition >= this.unitNames.size() - 1; + else + return true; + } + } + + @Override + public Entry next() { + // increment unit name position + this.unitNamePosition++; + + // if I have prefixes, ensure I'm not using a nonlinear unit + // since all of the unprefixed stuff is done, just remove nonlinear units + if (!this.prefixCoordinates.isEmpty()) { + while (!(PrefixedUnitEntrySet.this.map + .get(this.unitNames.get(this.unitNamePosition)) instanceof LinearUnit)) { + this.unitNames.remove(this.unitNamePosition); + } + } + + // carry over + if (!this.prefixNames.isEmpty() && this.unitNamePosition >= this.unitNames.size() - 1) { + // handle prefix position + this.unitNamePosition = 0; + int i = this.prefixCoordinates.size() - 1; + this.prefixCoordinates.set(i, this.prefixCoordinates.get(i) + 1); + + while (this.prefixCoordinates.get(i) >= this.prefixNames.size() - 1) { + this.prefixCoordinates.set(i, 0); + i--; + if (i < 0) { + this.prefixCoordinates.add(0, 0); + } + } + } + + final StringBuilder unitNameBuilder = new StringBuilder(); + for (final int i : this.prefixCoordinates) { + unitNameBuilder.append(this.prefixNames.get(i)); + } + unitNameBuilder.append(this.unitNames.get(this.unitNamePosition)); + + final String unitName = unitNameBuilder.toString(); + return new Entry() { + @Override + public String getKey() { + return unitName; + } + + @Override + public Unit getValue() { + return PrefixedUnitEntrySet.this.map.get(unitName); + } + + @Override + public Unit setValue(final Unit value) { + throw new UnsupportedOperationException(); + } + }; + } + }; + } + + @Override + public boolean remove(final Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(final Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeIf(final Predicate> filter) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(final Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public int size() { + if (this.map.units.isEmpty()) + return 0; + else { + if (this.map.prefixes.isEmpty()) + return this.map.units.size(); + else + // infinite set + return Integer.MAX_VALUE; + } + } + + @Override + public Object[] toArray() { + if (this.map.units.isEmpty()) + // finite, it will work + return super.toArray(); + else { + if (this.map.prefixes.isEmpty()) + // finite, it will work + return super.toArray(); + else + // infinite set + throw new UnsupportedOperationException("Cannot make an infinite set into an array."); + } + } + + @Override + public T[] toArray(final T[] a) { + if (this.map.units.isEmpty()) + // finite, it will work + return super.toArray(a); + else { + if (this.map.prefixes.isEmpty()) + // finite, it will work + return super.toArray(a); + else + // infinite set + throw new UnsupportedOperationException("Cannot make an infinite set into an array."); + } + } + + } + + /** + * The class used for unit name sets. + * + * @author Adrien Hopkins + * @since 2019-04-13 + */ + private static final class PrefixedUnitNameSet extends AbstractSet { + // the map that created this set + private final PrefixedUnitMap map; + + /** + * Creates the {@code PrefixedUnitNameSet}. + * + * @param map + * @since 2019-04-13 + */ + public PrefixedUnitNameSet(final PrefixedUnitMap map) { + this.map = map; + } + + @Override + public boolean add(final String e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(final Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean contains(final Object o) { + return this.map.containsKey(o); + } + + @Override + public boolean containsAll(final Collection c) { + for (final Object o : c) + if (!this.contains(o)) + return false; + return true; + } + + @Override + public boolean isEmpty() { + return this.map.isEmpty(); + } + + @Override + public Iterator iterator() { + return new Iterator() { + // position in the unit list + int unitNamePosition = -1; + // the indices of the prefixes attached to the current unit + List prefixCoordinates = new ArrayList<>(); + + List unitNames = new ArrayList<>(PrefixedUnitNameSet.this.map.units.keySet()); + List prefixNames = new ArrayList<>(PrefixedUnitNameSet.this.map.prefixes.keySet()); + + @Override + public boolean hasNext() { + if (this.unitNames.isEmpty()) + return false; + else { + if (this.prefixNames.isEmpty()) + return this.unitNamePosition >= this.unitNames.size() - 1; + else + return true; + } + } + + @Override + public String next() { + // increment unit name position + this.unitNamePosition++; + + // if I have prefixes, ensure I'm not using a nonlinear unit + // since all of the unprefixed stuff is done, just remove nonlinear units + if (!this.prefixCoordinates.isEmpty()) { + while (!(PrefixedUnitNameSet.this.map + .get(this.unitNames.get(this.unitNamePosition)) instanceof LinearUnit)) { + this.unitNames.remove(this.unitNamePosition); + } + } + + // carry over + if (!this.prefixNames.isEmpty() && this.unitNamePosition >= this.unitNames.size() - 1) { + // handle prefix position + this.unitNamePosition = 0; + int i = this.prefixCoordinates.size() - 1; + this.prefixCoordinates.set(i, this.prefixCoordinates.get(i) + 1); + + while (this.prefixCoordinates.get(i) >= this.prefixNames.size() - 1) { + this.prefixCoordinates.set(i, 0); + i--; + if (i < 0) { + this.prefixCoordinates.add(0, 0); + } + } + } + + final StringBuilder unitName = new StringBuilder(); + for (final int i : this.prefixCoordinates) { + unitName.append(this.prefixNames.get(i)); + } + unitName.append(this.unitNames.get(this.unitNamePosition)); + return unitName.toString(); + } + }; + } + + @Override + public boolean remove(final Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(final Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeIf(final Predicate filter) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(final Collection c) { + throw new UnsupportedOperationException(); + } + + @Override + public int size() { + if (this.map.units.isEmpty()) + return 0; + else { + if (this.map.prefixes.isEmpty()) + return this.map.units.size(); + else + // infinite set + return Integer.MAX_VALUE; + } + } + + @Override + public Object[] toArray() { + if (this.map.units.isEmpty()) + // finite, it will work + return super.toArray(); + else { + if (this.map.prefixes.isEmpty()) + // finite, it will work + return super.toArray(); + else + // infinite set + throw new UnsupportedOperationException("Cannot make an infinite set into an array."); + } + } + + @Override + public T[] toArray(final T[] a) { + if (this.map.units.isEmpty()) + // finite, it will work + return super.toArray(a); + else { + if (this.map.prefixes.isEmpty()) + // finite, it will work + return super.toArray(a); + else + // infinite set + throw new UnsupportedOperationException("Cannot make an infinite set into an array."); + } + } + + } + + /** + * The units stored in this collection, without prefixes. + * + * @since 2019-04-13 + */ + private final Map units; + + /** + * The available prefixes for use. + * + * @since 2019-04-13 + */ + private final Map prefixes; + + // caches + private Collection values = null; + private Set keySet = null; + private Set> entrySet = null; + + /** + * Creates the {@code PrefixedUnitMap}. + * + * @param units + * @param prefixes + * @since 2019-04-13 + */ + public PrefixedUnitMap(final Map units, final Map prefixes) { + // I am making unmodifiable maps to ensure I don't accidentally make changes. + this.units = Collections.unmodifiableMap(units); + this.prefixes = Collections.unmodifiableMap(prefixes); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + + @Override + public Unit compute(final String key, + final BiFunction remappingFunction) { + throw new UnsupportedOperationException(); + } + + @Override + public Unit computeIfAbsent(final String key, final Function mappingFunction) { + throw new UnsupportedOperationException(); + } + + @Override + public Unit computeIfPresent(final String key, + final BiFunction remappingFunction) { + throw new UnsupportedOperationException(); + } + + @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 shortest prefix that is attached to a valid unit + String shortestPrefix = null; + int shortestLength = Integer.MAX_VALUE; + + for (final String prefixName : this.prefixes.keySet()) { + // a prefix name is valid if: + // - it is prefixed (i.e. the unit name starts with it) + // - it is shorter than the existing largest prefix (since I am looking for the smallest valid prefix) + // - the part after the prefix is a valid unit name + // - the unit described that name is a linear unit (since only linear units can have prefixes) + if (unitName.startsWith(prefixName) && prefixName.length() < shortestLength) { + final String rest = unitName.substring(prefixName.length()); + if (this.containsKey(rest) && this.get(rest) instanceof LinearUnit) { + shortestPrefix = prefixName; + shortestLength = prefixName.length(); + } + } + } + + return shortestPrefix != null; + } + + @Override + public boolean containsValue(final Object value) { + return this.units.containsValue(value); + } + + @Override + public Set> entrySet() { + if (this.entrySet == null) { + this.entrySet = new PrefixedUnitEntrySet(this); + } + 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 shortest prefix that is attached to a valid unit + String shortestPrefix = null; + int shortestLength = Integer.MAX_VALUE; + + for (final String prefixName : this.prefixes.keySet()) { + // a prefix name is valid if: + // - it is prefixed (i.e. the unit name starts with it) + // - it is shorter than the existing largest prefix (since I am looking for the smallest valid prefix) + // - the part after the prefix is a valid unit name + // - the unit described that name is a linear unit (since only linear units can have prefixes) + if (unitName.startsWith(prefixName) && prefixName.length() < shortestLength) { + final String rest = unitName.substring(prefixName.length()); + if (this.containsKey(rest) && this.get(rest) instanceof LinearUnit) { + shortestPrefix = prefixName; + shortestLength = prefixName.length(); + } + } + } + + // if none found, returns null + if (shortestPrefix == null) + return null; + else { + // get necessary data + final String rest = unitName.substring(shortestLength); + // this cast will not fail because I verified that it would work before selecting this prefix + final LinearUnit unit = (LinearUnit) this.get(rest); + final UnitPrefix prefix = this.prefixes.get(shortestPrefix); + + return unit.withPrefix(prefix); + } + } + + @Override + public boolean isEmpty() { + return this.units.isEmpty(); + } + + @Override + public Set keySet() { + if (this.keySet == null) { + this.keySet = new PrefixedUnitNameSet(this); + } + return this.keySet; + } + + @Override + public Unit merge(final String key, final Unit value, + final BiFunction remappingFunction) { + throw new UnsupportedOperationException(); + } + + @Override + public Unit put(final String key, final Unit value) { + throw new UnsupportedOperationException(); + } + + @Override + public void putAll(final Map m) { + throw new UnsupportedOperationException(); + } + + @Override + public Unit putIfAbsent(final String key, final Unit value) { + throw new UnsupportedOperationException(); + } + + @Override + public Unit remove(final Object key) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(final Object key, final Object value) { + throw new UnsupportedOperationException(); + } + + @Override + public Unit replace(final String key, final Unit value) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean replace(final String key, final Unit oldValue, final Unit newValue) { + throw new UnsupportedOperationException(); + } + + @Override + public void replaceAll(final BiFunction function) { + throw new UnsupportedOperationException(); + } + + @Override + public int size() { + if (this.units.isEmpty()) + return 0; + else { + if (this.prefixes.isEmpty()) + return this.units.size(); + else + // infinite set + return Integer.MAX_VALUE; + } + } + + @Override + public Collection values() { + if (this.values == null) { + this.values = Collections.unmodifiableCollection(this.units.values()); + } + return this.values; + } + } + /** * The exponent operator * @@ -57,7 +702,7 @@ public final class UnitsDatabase { * @return result * @since 2019-04-10 */ - private static final LinearUnit exponent(final LinearUnit base, final LinearUnit exponentUnit) { + private static final LinearUnit exponentiateUnits(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, @@ -74,12 +719,12 @@ public final class UnitsDatabase { } /** - * The units in this system. + * The units in this system, excluding prefixes. * * @since 2019-01-07 * @since v0.1.0 */ - private final Map units; + private final Map prefixlessUnits; /** * The unit prefixes in this system. @@ -96,6 +741,13 @@ public final class UnitsDatabase { */ private final Map dimensions; + /** + * A map mapping strings to units (including prefixes) + * + * @since 2019-04-13 + */ + private final Map units; + /** * A parser that can parse unit expressions. * @@ -106,7 +758,7 @@ public final class UnitsDatabase { .addBinaryOperator("-", (o1, o2) -> o1.minus(o2), 0) .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 1).addSpaceFunction("*") .addBinaryOperator("/", (o1, o2) -> o1.dividedBy(o2), 1) - .addBinaryOperator("^", UnitsDatabase::exponent, 2).build(); + .addBinaryOperator("^", UnitsDatabase::exponentiateUnits, 2).build(); /** * A parser that can parse unit prefix expressions @@ -134,9 +786,10 @@ public final class UnitsDatabase { * @since v0.1.0 */ public UnitsDatabase() { - this.units = new HashMap<>(); + this.prefixlessUnits = new HashMap<>(); this.prefixes = new HashMap<>(); this.dimensions = new HashMap<>(); + this.units = new PrefixedUnitMap(this.prefixlessUnits, this.prefixes); } /** @@ -236,7 +889,7 @@ public final class UnitsDatabase { * @since v0.1.0 */ public void addUnit(final String name, final Unit unit) { - this.units.put(Objects.requireNonNull(name, "name must not be null."), + this.prefixlessUnits.put(Objects.requireNonNull(name, "name must not be null."), Objects.requireNonNull(unit, "unit must not be null.")); } @@ -313,19 +966,6 @@ public final class UnitsDatabase { return this.dimensions.containsKey(name); } - /** - * Tests if the database has a unit with this name, ignoring prefixes - * - * @param name - * 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); - } - /** * Tests if the database has a unit prefix with this name. * @@ -349,21 +989,15 @@ public final class UnitsDatabase { * @since v0.1.0 */ public boolean containsUnitName(final String name) { - // check for prefixes - for (final String prefixName : this.prefixNameSet()) { - if (name.startsWith(prefixName)) - if (this.containsUnitName(name.substring(prefixName.length()))) - return true; - } return this.units.containsKey(name); } /** - * @return an immutable set of all of the dimension names in this database. - * @since 2019-03-14 + * @return a map mapping dimension names to dimensions + * @since 2019-04-13 */ - public Set dimensionNameSet() { - return Collections.unmodifiableSet(this.dimensions.keySet()); + public Map dimensionMap() { + return Collections.unmodifiableMap(this.dimensions); } /** @@ -519,19 +1153,6 @@ public final class UnitsDatabase { return this.prefixExpressionParser.parseExpression(modifiedExpression); } - /** - * Gets a unit from the database from its name, ignoring prefixes. - * - * @param name - * unit's name - * @return unit - * @since 2019-01-10 - * @since v0.1.0 - */ - public Unit getPrefixlessUnit(final String name) { - return this.units.get(name); - } - /** * Gets a unit from the database from its name, looking for prefixes. * @@ -546,24 +1167,6 @@ public final class UnitsDatabase { final double value = Double.parseDouble(name); return SI.SI.getBaseUnit(UnitDimension.EMPTY).times(value); } catch (final NumberFormatException e) { - 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); } @@ -703,28 +1306,26 @@ 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 + * @return a map mapping prefix names to prefixes + * @since 2019-04-13 */ - public Set prefixlessUnitNameSet() { - return Collections.unmodifiableSet(this.units.keySet()); + public Map prefixMap() { + return Collections.unmodifiableMap(this.prefixes); } /** - * @return an immutable set of all of the units in this database, ignoring prefixes. - * @since 2019-04-10 + * @return a map mapping unit names to units, including prefixed names + * @since 2019-04-13 */ - public Set prefixlessUnitSet() { - return Collections.unmodifiableSet(new HashSet<>(this.units.values())); + public Map unitMap() { + return this.units; // PrefixedUnitMap is immutable so I don't need to make an unmodifiable map. } /** - * @return an immutable set of all of the prefix names in this database - * @since 2019-01-14 - * @since v0.1.0 + * @return a map mapping unit names to units, ignoring prefixes + * @since 2019-04-13 */ - public Set prefixNameSet() { - return Collections.unmodifiableSet(this.prefixes.keySet()); + public Map unitMapPrefixless() { + return Collections.unmodifiableMap(this.prefixlessUnits); } } diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 9314510..49a40d6 100755 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -24,6 +24,7 @@ import java.math.MathContext; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Comparator; +import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.function.Predicate; @@ -143,19 +144,19 @@ final class UnitConverterGUI { return o1.compareTo(o2); }; - this.unitNames = new ArrayList<>(this.units.prefixlessUnitNameSet()); + this.unitNames = new ArrayList<>(this.units.unitMapPrefixless().keySet()); this.unitNames.sort(null); // sorts it using Comparable - this.unitNamesFiltered = new DelegateListModel<>(new ArrayList<>(this.units.prefixlessUnitNameSet())); + this.unitNamesFiltered = new DelegateListModel<>(new ArrayList<>(this.units.unitMapPrefixless().keySet())); this.unitNamesFiltered.sort(null); // sorts it using Comparable - this.prefixNames = new ArrayList<>(this.units.prefixNameSet()); + this.prefixNames = new ArrayList<>(this.units.prefixMap().keySet()); this.prefixNames.sort(this.prefixNameComparator); // sorts it using my comparator - this.prefixNamesFiltered = new DelegateListModel<>(new ArrayList<>(this.units.prefixNameSet())); + this.prefixNamesFiltered = new DelegateListModel<>(new ArrayList<>(this.units.prefixMap().keySet())); this.prefixNamesFiltered.sort(this.prefixNameComparator); // sorts it using my comparator - this.dimensionNames = new DelegateListModel<>(new ArrayList<>(this.units.dimensionNameSet())); + this.dimensionNames = new DelegateListModel<>(new ArrayList<>(this.units.dimensionMap().keySet())); this.dimensionNames.sort(null); // sorts it using Comparable // a Predicate that returns true iff the argument is a full base unit @@ -163,8 +164,9 @@ final class UnitConverterGUI { // print out unit counts System.out.printf("Successfully loaded %d units with %d unit names (%d base units).%n", - this.units.prefixlessUnitSet().size(), this.units.prefixlessUnitNameSet().size(), - this.units.prefixlessUnitSet().stream().filter(isFullBase).count()); + new HashSet<>(this.units.unitMapPrefixless().values()).size(), + this.units.unitMapPrefixless().size(), + new HashSet<>(this.units.unitMapPrefixless().values()).stream().filter(isFullBase).count()); } /** @@ -449,7 +451,7 @@ final class UnitConverterGUI { } public final Set unitNameSet() { - return this.units.prefixlessUnitNameSet(); + return this.units.unitMapPrefixless().keySet(); } } diff --git a/src/org/unitConverter/math/ExpressionParser.java b/src/org/unitConverter/math/ExpressionParser.java index b56fa71..d01afaa 100644 --- a/src/org/unitConverter/math/ExpressionParser.java +++ b/src/org/unitConverter/math/ExpressionParser.java @@ -510,8 +510,6 @@ public final class ExpressionParser { expressionRPN = expressionRPN.substring(0, expressionRPN.length() - 1); } return expressionRPN; - - // TODO document org.unitConverter.expressionParser.ExpressionParser.convertExpressionToPolish(expression) } /** -- cgit v1.2.3 From 73d305684d3549d17ebd95a5fdb7d366849db226 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sun, 14 Apr 2019 17:29:50 -0400 Subject: Added @since tags to all classes and methods from v0.2.0 --- src/org/unitConverter/UnitsDatabase.java | 47 +++++++++++++++++++++- .../converterGUI/FilterComparator.java | 2 + .../converterGUI/MutablePredicate.java | 10 +++++ .../unitConverter/converterGUI/SearchBoxList.java | 35 ++++++++++++++++ .../converterGUI/UnitConverterGUI.java | 13 ++++++ .../unitConverter/converterGUI/package-info.java | 1 + src/org/unitConverter/dimension/package-info.java | 1 + src/org/unitConverter/math/DecimalComparison.java | 7 ++++ src/org/unitConverter/math/ExpressionParser.java | 46 ++++++++++++++++++++- src/org/unitConverter/unit/AbstractUnit.java | 1 - src/org/unitConverter/unit/BaseUnit.java | 1 + src/org/unitConverter/unit/DefaultUnitPrefix.java | 1 + src/org/unitConverter/unit/LinearUnit.java | 3 ++ src/org/unitConverter/unit/UnitPrefix.java | 3 ++ src/org/unitConverter/unit/package-info.java | 1 + src/test/java/ExpressionParserTest.java | 1 + src/test/java/UnitTest.java | 1 + src/test/java/UnitsDatabaseTest.java | 7 ++++ src/test/java/package-info.java | 1 + 19 files changed, 179 insertions(+), 3 deletions(-) (limited to 'src/org/unitConverter/math/ExpressionParser.java') diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java index abe6546..e5d2f67 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -72,9 +72,15 @@ public final class UnitsDatabase { * "A", inputting "ABC" will return the unit "C" with the prefix "AB", not "BC" with the prefix "A". * *

+ *

+ * This map is infinite in size if there is at least one unit and at least one prefix. If it is infinite, some + * operations that only work with finite collections, like converting name/entry sets to arrays, will throw an + * {@code UnsupportedOperationException}. + *

* * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ private static final class PrefixedUnitMap implements Map { /** @@ -82,6 +88,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ private static final class PrefixedUnitEntrySet extends AbstractSet> { /** @@ -89,6 +96,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-14 + * @since v0.2.0 */ private static final class PrefixedUnitEntry implements Entry { private final String key; @@ -98,8 +106,11 @@ public final class UnitsDatabase { * Creates the {@code PrefixedUnitEntry}. * * @param key + * key * @param value + * value * @since 2019-04-14 + * @since v0.2.0 */ public PrefixedUnitEntry(final String key, final Unit value) { this.key = key; @@ -127,6 +138,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-14 + * @since v0.2.0 */ private static final class PrefixedUnitEntryIterator implements Iterator> { // position in the unit list @@ -143,6 +155,7 @@ public final class UnitsDatabase { * Creates the {@code UnitsDatabase.PrefixedUnitMap.PrefixedUnitNameSet.PrefixedUnitNameIterator}. * * @since 2019-04-14 + * @since v0.2.0 */ public PrefixedUnitEntryIterator(final PrefixedUnitEntrySet set) { this.map = set.map; @@ -153,6 +166,7 @@ public final class UnitsDatabase { /** * @return current unit name * @since 2019-04-14 + * @since v0.2.0 */ private String getCurrentUnitName() { final StringBuilder unitName = new StringBuilder(); @@ -180,6 +194,7 @@ public final class UnitsDatabase { * Changes this iterator's position to the next available one. * * @since 2019-04-14 + * @since v0.2.0 */ private void incrementPosition() { this.unitNamePosition++; @@ -239,7 +254,9 @@ public final class UnitsDatabase { * Creates the {@code PrefixedUnitNameSet}. * * @param map + * map that created this set * @since 2019-04-13 + * @since v0.2.0 */ public PrefixedUnitEntrySet(final PrefixedUnitMap map) { this.map = map; @@ -353,6 +370,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ private static final class PrefixedUnitNameSet extends AbstractSet { /** @@ -360,6 +378,7 @@ public final class UnitsDatabase { * * @author Adrien Hopkins * @since 2019-04-14 + * @since v0.2.0 */ private static final class PrefixedUnitNameIterator implements Iterator { // position in the unit list @@ -376,6 +395,7 @@ public final class UnitsDatabase { * Creates the {@code UnitsDatabase.PrefixedUnitMap.PrefixedUnitNameSet.PrefixedUnitNameIterator}. * * @since 2019-04-14 + * @since v0.2.0 */ public PrefixedUnitNameIterator(final PrefixedUnitNameSet set) { this.map = set.map; @@ -386,6 +406,7 @@ public final class UnitsDatabase { /** * @return current unit name * @since 2019-04-14 + * @since v0.2.0 */ private String getCurrentUnitName() { final StringBuilder unitName = new StringBuilder(); @@ -413,6 +434,7 @@ public final class UnitsDatabase { * Changes this iterator's position to the next available one. * * @since 2019-04-14 + * @since v0.2.0 */ private void incrementPosition() { this.unitNamePosition++; @@ -472,7 +494,9 @@ public final class UnitsDatabase { * Creates the {@code PrefixedUnitNameSet}. * * @param map + * map that created this set * @since 2019-04-13 + * @since v0.2.0 */ public PrefixedUnitNameSet(final PrefixedUnitMap map) { this.map = map; @@ -567,13 +591,13 @@ public final class UnitsDatabase { // infinite set throw new UnsupportedOperationException("Cannot make an infinite set into an array."); } - } /** * The units stored in this collection, without prefixes. * * @since 2019-04-13 + * @since v0.2.0 */ private final Map units; @@ -581,6 +605,7 @@ public final class UnitsDatabase { * The available prefixes for use. * * @since 2019-04-13 + * @since v0.2.0 */ private final Map prefixes; @@ -593,8 +618,11 @@ public final class UnitsDatabase { * Creates the {@code PrefixedUnitMap}. * * @param units + * map mapping unit names to units * @param prefixes + * map mapping prefix names to prefixes * @since 2019-04-13 + * @since v0.2.0 */ public PrefixedUnitMap(final Map units, final Map prefixes) { // I am making unmodifiable maps to ensure I don't accidentally make changes. @@ -804,6 +832,7 @@ public final class UnitsDatabase { * exponent * @return result * @since 2019-04-10 + * @since v0.2.0 */ private static final LinearUnit exponentiateUnits(final LinearUnit base, final LinearUnit exponentUnit) { // exponent function - first check if o2 is a number, @@ -841,6 +870,7 @@ public final class UnitsDatabase { * The dimensions in this system. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map dimensions; @@ -848,6 +878,7 @@ public final class UnitsDatabase { * A map mapping strings to units (including prefixes) * * @since 2019-04-13 + * @since v0.2.0 */ private final Map units; @@ -855,6 +886,7 @@ public final class UnitsDatabase { * A parser that can parse unit expressions. * * @since 2019-03-22 + * @since v0.2.0 */ private final ExpressionParser unitExpressionParser = new ExpressionParser.Builder<>( this::getLinearUnit).addBinaryOperator("+", (o1, o2) -> o1.plus(o2), 0) @@ -867,6 +899,7 @@ public final class UnitsDatabase { * A parser that can parse unit prefix expressions * * @since 2019-04-13 + * @since v0.2.0 */ private final ExpressionParser prefixExpressionParser = new ExpressionParser.Builder<>(this::getPrefix) .addBinaryOperator("*", (o1, o2) -> o1.times(o2), 0).addSpaceFunction("*") @@ -877,6 +910,7 @@ public final class UnitsDatabase { * A parser that can parse unit dimension expressions. * * @since 2019-04-13 + * @since v0.2.0 */ private final ExpressionParser unitDimensionParser = new ExpressionParser.Builder<>( this::getDimension).addBinaryOperator("*", (o1, o2) -> o1.times(o2), 0).addSpaceFunction("*") @@ -905,6 +939,7 @@ public final class UnitsDatabase { * @throws NullPointerException * if name or dimension is null * @since 2019-03-14 + * @since v0.2.0 */ public void addDimension(final String name, final UnitDimension dimension) { this.dimensions.put(Objects.requireNonNull(name, "name must not be null."), @@ -919,6 +954,7 @@ public final class UnitsDatabase { * @param lineCounter * number of line, for error messages * @since 2019-04-10 + * @since v0.2.0 */ private void addDimensionFromLine(final String line, final long lineCounter) { // ignore lines that start with a # sign - they're comments @@ -1004,6 +1040,7 @@ public final class UnitsDatabase { * @param lineCounter * number of line, for error messages * @since 2019-04-10 + * @since v0.2.0 */ private void addUnitOrPrefixFromLine(final String line, final long lineCounter) { // ignore lines that start with a # sign - they're comments @@ -1064,6 +1101,7 @@ public final class UnitsDatabase { * name to test * @return if database contains name * @since 2019-03-14 + * @since v0.2.0 */ public boolean containsDimensionName(final String name) { return this.dimensions.containsKey(name); @@ -1098,6 +1136,7 @@ public final class UnitsDatabase { /** * @return a map mapping dimension names to dimensions * @since 2019-04-13 + * @since v0.2.0 */ public Map dimensionMap() { return Collections.unmodifiableMap(this.dimensions); @@ -1114,6 +1153,7 @@ public final class UnitsDatabase { * dimension's name * @return dimension * @since 2019-03-14 + * @since v0.2.0 */ public UnitDimension getDimension(final String name) { Objects.requireNonNull(name, "name must not be null."); @@ -1152,6 +1192,7 @@ public final class UnitsDatabase { * @throws NullPointerException * if expression is null * @since 2019-04-13 + * @since v0.2.0 */ public UnitDimension getDimensionFromExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); @@ -1180,6 +1221,7 @@ public final class UnitsDatabase { * unit's name * @return unit * @since 2019-03-22 + * @since v0.2.0 */ private LinearUnit getLinearUnit(final String name) { // see if I am using a function-unit like tempC(100) @@ -1411,6 +1453,7 @@ public final class UnitsDatabase { /** * @return a map mapping prefix names to prefixes * @since 2019-04-13 + * @since v0.2.0 */ public Map prefixMap() { return Collections.unmodifiableMap(this.prefixes); @@ -1419,6 +1462,7 @@ public final class UnitsDatabase { /** * @return a map mapping unit names to units, including prefixed names * @since 2019-04-13 + * @since v0.2.0 */ public Map unitMap() { return this.units; // PrefixedUnitMap is immutable so I don't need to make an unmodifiable map. @@ -1427,6 +1471,7 @@ public final class UnitsDatabase { /** * @return a map mapping unit names to units, ignoring prefixes * @since 2019-04-13 + * @since v0.2.0 */ public Map unitMapPrefixless() { return Collections.unmodifiableMap(this.prefixlessUnits); diff --git a/src/org/unitConverter/converterGUI/FilterComparator.java b/src/org/unitConverter/converterGUI/FilterComparator.java index 2d0e7f9..7b17bfc 100755 --- a/src/org/unitConverter/converterGUI/FilterComparator.java +++ b/src/org/unitConverter/converterGUI/FilterComparator.java @@ -45,6 +45,7 @@ final class FilterComparator implements Comparator { * Whether or not the comparison is case-sensitive. * * @since 2019-04-14 + * @since v0.2.0 */ private final boolean caseSensitive; @@ -87,6 +88,7 @@ final class FilterComparator implements Comparator { * @throws NullPointerException * if filter is null * @since 2019-04-14 + * @since v0.2.0 */ public FilterComparator(final String filter, final Comparator comparator, final boolean caseSensitive) { this.filter = Objects.requireNonNull(filter, "filter must not be null."); diff --git a/src/org/unitConverter/converterGUI/MutablePredicate.java b/src/org/unitConverter/converterGUI/MutablePredicate.java index 157903c..e15b3cd 100644 --- a/src/org/unitConverter/converterGUI/MutablePredicate.java +++ b/src/org/unitConverter/converterGUI/MutablePredicate.java @@ -23,14 +23,22 @@ import java.util.function.Predicate; * * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ final class MutablePredicate implements Predicate { + /** + * The predicate stored in this {@code MutablePredicate} + * + * @since 2019-04-13 + * @since v0.2.0 + */ private Predicate predicate; /** * Creates the {@code MutablePredicate}. * * @since 2019-04-13 + * @since v0.2.0 */ public MutablePredicate(final Predicate predicate) { this.predicate = predicate; @@ -39,6 +47,7 @@ final class MutablePredicate implements Predicate { /** * @return predicate * @since 2019-04-13 + * @since v0.2.0 */ public final Predicate getPredicate() { return this.predicate; @@ -48,6 +57,7 @@ final class MutablePredicate implements Predicate { * @param predicate * new value of predicate * @since 2019-04-13 + * @since v0.2.0 */ public final void setPredicate(final Predicate predicate) { this.predicate = predicate; diff --git a/src/org/unitConverter/converterGUI/SearchBoxList.java b/src/org/unitConverter/converterGUI/SearchBoxList.java index 35cc347..1995466 100644 --- a/src/org/unitConverter/converterGUI/SearchBoxList.java +++ b/src/org/unitConverter/converterGUI/SearchBoxList.java @@ -33,20 +33,29 @@ import javax.swing.JTextField; /** * @author Adrien Hopkins * @since 2019-04-13 + * @since v0.2.0 */ final class SearchBoxList extends JPanel { /** * @since 2019-04-13 + * @since v0.2.0 */ private static final long serialVersionUID = 6226930279415983433L; /** * The text to place in an empty search box. + * + * @since 2019-04-13 + * @since v0.2.0 */ private static final String EMPTY_TEXT = "Search..."; + /** * The color to use for an empty foreground. + * + * @since 2019-04-13 + * @since v0.2.0 */ private static final Color EMPTY_FOREGROUND = new Color(192, 192, 192); @@ -66,6 +75,13 @@ final class SearchBoxList extends JPanel { private final Comparator defaultOrdering; private final boolean caseSensitive; + /** + * Creates the {@code SearchBoxList}. + * + * @param itemsToFilter + * items to put in the list + * @since 2019-04-14 + */ public SearchBoxList(final Collection itemsToFilter) { this(itemsToFilter, null, false); } @@ -73,7 +89,15 @@ final class SearchBoxList extends JPanel { /** * Creates the {@code SearchBoxList}. * + * @param itemsToFilter + * items to put in the list + * @param defaultOrdering + * default ordering of items after filtration (null=Comparable) + * @param caseSensitive + * whether or not the filtration is case-sensitive + * * @since 2019-04-13 + * @since v0.2.0 */ public SearchBoxList(final Collection itemsToFilter, final Comparator defaultOrdering, final boolean caseSensitive) { @@ -116,6 +140,7 @@ final class SearchBoxList extends JPanel { * @param filter * filter to add. * @since 2019-04-13 + * @since v0.2.0 */ public void addSearchFilter(final Predicate filter) { this.customSearchFilter = this.customSearchFilter.and(filter); @@ -125,6 +150,7 @@ final class SearchBoxList extends JPanel { * Resets the search filter. * * @since 2019-04-13 + * @since v0.2.0 */ public void clearSearchFilters() { this.customSearchFilter = o -> true; @@ -133,6 +159,7 @@ final class SearchBoxList extends JPanel { /** * @return this component's search box component * @since 2019-04-14 + * @since v0.2.0 */ public final JTextField getSearchBox() { return this.searchBox; @@ -143,6 +170,7 @@ final class SearchBoxList extends JPanel { * text to search for * @return a filter that filters out that text, based on this list's case sensitive setting * @since 2019-04-14 + * @since v0.2.0 */ private Predicate getSearchFilter(final String searchText) { if (this.caseSensitive) @@ -154,6 +182,7 @@ final class SearchBoxList extends JPanel { /** * @return this component's list component * @since 2019-04-14 + * @since v0.2.0 */ public final JList getSearchList() { return this.searchItems; @@ -162,6 +191,7 @@ final class SearchBoxList extends JPanel { /** * @return index selected in item list * @since 2019-04-14 + * @since v0.2.0 */ public int getSelectedIndex() { return this.searchItems.getSelectedIndex(); @@ -170,6 +200,7 @@ final class SearchBoxList extends JPanel { /** * @return value selected in item list * @since 2019-04-13 + * @since v0.2.0 */ public String getSelectedValue() { return this.searchItems.getSelectedValue(); @@ -179,6 +210,7 @@ final class SearchBoxList extends JPanel { * Re-applies the filters. * * @since 2019-04-13 + * @since v0.2.0 */ public void reapplyFilter() { final String searchText = this.searchBoxEmpty ? "" : this.searchBox.getText(); @@ -205,6 +237,7 @@ final class SearchBoxList extends JPanel { * @param e * focus event * @since 2019-04-13 + * @since v0.2.0 */ private void searchBoxFocusGained(final FocusEvent e) { this.searchBoxFocused = true; @@ -220,6 +253,7 @@ final class SearchBoxList extends JPanel { * @param e * focus event * @since 2019-04-13 + * @since v0.2.0 */ private void searchBoxFocusLost(final FocusEvent e) { this.searchBoxFocused = false; @@ -236,6 +270,7 @@ final class SearchBoxList extends JPanel { *

* * @since 2019-04-14 + * @since v0.2.0 */ private void searchBoxTextChanged() { if (this.searchBoxFocused) { diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 1f59e3a..e258c6f 100755 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -64,6 +64,7 @@ final class UnitConverterGUI { * @param database * database to add to * @since 2019-04-14 + * @since v0.2.0 */ private static void addDefaults(final UnitsDatabase database) { database.addUnit("metre", SI.METRE); @@ -167,6 +168,7 @@ final class UnitConverterGUI { * Converts in the dimension-based converter * * @since 2019-04-13 + * @since v0.2.0 */ public final void convertDimensionBased() { final String fromSelection = this.view.getFromSelection(); @@ -264,6 +266,7 @@ final class UnitConverterGUI { /** * @return a list of all of the unit dimensions * @since 2019-04-13 + * @since v0.2.0 */ public final List dimensionNameList() { return this.dimensionNames; @@ -272,6 +275,7 @@ final class UnitConverterGUI { /** * @return a comparator to compare prefix names * @since 2019-04-14 + * @since v0.2.0 */ public final Comparator getPrefixNameComparator() { return this.prefixNameComparator; @@ -282,6 +286,7 @@ final class UnitConverterGUI { * value to round * @return string of that value rounded to {@code significantDigits} significant digits. * @since 2019-04-14 + * @since v0.2.0 */ private final String getRoundedString(final double value) { // round value @@ -304,6 +309,7 @@ final class UnitConverterGUI { /** * @return a set of all prefix names in the database * @since 2019-04-14 + * @since v0.2.0 */ public final Set prefixNameSet() { return this.database.prefixMap().keySet(); @@ -333,6 +339,7 @@ final class UnitConverterGUI { * @param significantFigures * new value of significantFigures * @since 2019-01-15 + * @since v0.1.0 */ public final void setSignificantFigures(final int significantFigures) { this.significantFigures = significantFigures; @@ -348,6 +355,7 @@ final class UnitConverterGUI { * name of dimension to test * @return whether unit has dimenision * @since 2019-04-13 + * @since v0.2.0 */ public final boolean unitMatchesDimension(final String unitName, final String dimensionName) { final Unit unit = this.database.getUnit(unitName); @@ -378,6 +386,7 @@ final class UnitConverterGUI { /** * @return a set of all of the unit names * @since 2019-04-14 + * @since v0.2.0 */ public final Set unitNameSet() { return this.database.unitMapPrefixless().keySet(); @@ -452,6 +461,7 @@ final class UnitConverterGUI { /** * @return value in dimension-based converter * @since 2019-04-13 + * @since v0.2.0 */ public String getDimensionConverterInput() { return this.valueInput.getText(); @@ -460,6 +470,7 @@ final class UnitConverterGUI { /** * @return selection in "From" selector in dimension-based converter * @since 2019-04-13 + * @since v0.2.0 */ public String getFromSelection() { return this.fromSearch.getSelectedValue(); @@ -486,6 +497,7 @@ final class UnitConverterGUI { /** * @return selection in "To" selector in dimension-based converter * @since 2019-04-13 + * @since v0.2.0 */ public String getToSelection() { return this.toSearch.getSelectedValue(); @@ -752,6 +764,7 @@ final class UnitConverterGUI { * @param text * text to set * @since 2019-04-13 + * @since v0.2.0 */ public void setDimensionConverterOutputText(final String text) { this.dimensionBasedOutput.setText(text); diff --git a/src/org/unitConverter/converterGUI/package-info.java b/src/org/unitConverter/converterGUI/package-info.java index d899f97..1555291 100644 --- a/src/org/unitConverter/converterGUI/package-info.java +++ b/src/org/unitConverter/converterGUI/package-info.java @@ -19,5 +19,6 @@ * * @author Adrien Hopkins * @since 2019-01-25 + * @since v0.2.0 */ package org.unitConverter.converterGUI; \ No newline at end of file diff --git a/src/org/unitConverter/dimension/package-info.java b/src/org/unitConverter/dimension/package-info.java index db363df..8cb26b1 100755 --- a/src/org/unitConverter/dimension/package-info.java +++ b/src/org/unitConverter/dimension/package-info.java @@ -19,5 +19,6 @@ * * @author Adrien Hopkins * @since 2018-12-22 + * @since v0.1.0 */ package org.unitConverter.dimension; \ No newline at end of file diff --git a/src/org/unitConverter/math/DecimalComparison.java b/src/org/unitConverter/math/DecimalComparison.java index e6fb733..7cdbe5b 100644 --- a/src/org/unitConverter/math/DecimalComparison.java +++ b/src/org/unitConverter/math/DecimalComparison.java @@ -21,6 +21,7 @@ package org.unitConverter.math; * * @author Adrien Hopkins * @since 2019-03-18 + * @since v0.2.0 */ public final class DecimalComparison { /** @@ -28,6 +29,7 @@ public final class DecimalComparison { * they are considered equal. * * @since 2019-03-18 + * @since v0.2.0 */ public static final double DOUBLE_EPSILON = 1.0e-15; @@ -36,6 +38,7 @@ public final class DecimalComparison { * they are considered equal. * * @since 2019-03-18 + * @since v0.2.0 */ public static final float FLOAT_EPSILON = 1.0e-6f; @@ -48,6 +51,7 @@ public final class DecimalComparison { * second value to test * @return whether they are equal * @since 2019-03-18 + * @since v0.2.0 */ public static final boolean equals(final double a, final double b) { return DecimalComparison.equals(a, b, DOUBLE_EPSILON); @@ -64,6 +68,7 @@ public final class DecimalComparison { * allowed difference * @return whether they are equal * @since 2019-03-18 + * @since v0.2.0 */ public static final boolean equals(final double a, final double b, final double epsilon) { return Math.abs(a - b) <= epsilon * Math.max(Math.abs(a), Math.abs(b)); @@ -78,6 +83,7 @@ public final class DecimalComparison { * second value to test * @return whether they are equal * @since 2019-03-18 + * @since v0.2.0 */ public static final boolean equals(final float a, final float b) { return DecimalComparison.equals(a, b, FLOAT_EPSILON); @@ -94,6 +100,7 @@ public final class DecimalComparison { * allowed difference * @return whether they are equal * @since 2019-03-18 + * @since v0.2.0 */ public static final boolean equals(final float a, final float b, final float epsilon) { return Math.abs(a - b) <= epsilon * Math.max(Math.abs(a), Math.abs(b)); diff --git a/src/org/unitConverter/math/ExpressionParser.java b/src/org/unitConverter/math/ExpressionParser.java index d01afaa..b2261ed 100644 --- a/src/org/unitConverter/math/ExpressionParser.java +++ b/src/org/unitConverter/math/ExpressionParser.java @@ -35,8 +35,8 @@ import java.util.function.UnaryOperator; * @param * type of object that exists in parsed expressions * @since 2019-03-14 + * @since v0.2.0 */ -// TODO: possibly make this class non-final? public final class ExpressionParser { /** * A builder that can create {@code ExpressionParser} instances. @@ -45,6 +45,7 @@ public final class ExpressionParser { * @param * type of object that exists in parsed expressions * @since 2019-03-17 + * @since v0.2.0 */ public static final class Builder { /** @@ -52,6 +53,7 @@ public final class ExpressionParser { * would use {@code Integer::parseInt}. * * @since 2019-03-14 + * @since v0.2.0 */ private final Function objectObtainer; @@ -59,6 +61,7 @@ public final class ExpressionParser { * The function of the space as an operator (like 3 x y) * * @since 2019-03-22 + * @since v0.2.0 */ private String spaceFunction = null; @@ -66,6 +69,7 @@ public final class ExpressionParser { * A map mapping operator strings to operator functions, for unary operators. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map> unaryOperators; @@ -73,6 +77,7 @@ public final class ExpressionParser { * A map mapping operator strings to operator functions, for binary operators. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map> binaryOperators; @@ -84,6 +89,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code objectObtainer} is null * @since 2019-03-17 + * @since v0.2.0 */ public Builder(final Function objectObtainer) { this.objectObtainer = Objects.requireNonNull(objectObtainer, "objectObtainer must not be null."); @@ -104,6 +110,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code text} or {@code operator} is null * @since 2019-03-17 + * @since v0.2.0 */ public Builder addBinaryOperator(final String text, final BinaryOperator operator, final int priority) { Objects.requireNonNull(text, "text must not be null."); @@ -128,6 +135,7 @@ public final class ExpressionParser { * text of operator to use * @return this builder * @since 2019-03-22 + * @since v0.2.0 */ public Builder addSpaceFunction(final String operator) { Objects.requireNonNull(operator, "operator must not be null."); @@ -152,6 +160,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code text} or {@code operator} is null * @since 2019-03-17 + * @since v0.2.0 */ public Builder addUnaryOperator(final String text, final UnaryOperator operator, final int priority) { Objects.requireNonNull(text, "text must not be null."); @@ -171,6 +180,7 @@ public final class ExpressionParser { /** * @return an {@code ExpressionParser} instance with the properties given to this builder * @since 2019-03-17 + * @since v0.2.0 */ public ExpressionParser build() { return new ExpressionParser<>(this.objectObtainer, this.unaryOperators, this.binaryOperators, @@ -185,11 +195,15 @@ public final class ExpressionParser { * @param * type of operand and result * @since 2019-03-17 + * @since v0.2.0 */ private static abstract class PriorityBinaryOperator implements BinaryOperator, Comparable> { /** * The operator's priority. Higher-priority operators are applied before lower-priority operators + * + * @since 2019-03-17 + * @since v0.2.0 */ private final int priority; @@ -199,6 +213,7 @@ public final class ExpressionParser { * @param priority * operator's priority * @since 2019-03-17 + * @since v0.2.0 */ public PriorityBinaryOperator(final int priority) { this.priority = priority; @@ -209,6 +224,10 @@ public final class ExpressionParser { * *

* {@inheritDoc} + *

+ * + * @since 2019-03-17 + * @since v0.2.0 */ @Override public int compareTo(final PriorityBinaryOperator o) { @@ -223,6 +242,7 @@ public final class ExpressionParser { /** * @return priority * @since 2019-03-22 + * @since v0.2.0 */ public final int getPriority() { return this.priority; @@ -236,11 +256,15 @@ public final class ExpressionParser { * @param * type of operand and result * @since 2019-03-17 + * @since v0.2.0 */ private static abstract class PriorityUnaryOperator implements UnaryOperator, Comparable> { /** * The operator's priority. Higher-priority operators are applied before lower-priority operators + * + * @since 2019-03-17 + * @since v0.2.0 */ private final int priority; @@ -250,6 +274,7 @@ public final class ExpressionParser { * @param priority * operator's priority * @since 2019-03-17 + * @since v0.2.0 */ public PriorityUnaryOperator(final int priority) { this.priority = priority; @@ -260,6 +285,10 @@ public final class ExpressionParser { * *

* {@inheritDoc} + *

+ * + * @since 2019-03-17 + * @since v0.2.0 */ @Override public int compareTo(final PriorityUnaryOperator o) { @@ -274,6 +303,7 @@ public final class ExpressionParser { /** * @return priority * @since 2019-03-22 + * @since v0.2.0 */ public final int getPriority() { return this.priority; @@ -285,6 +315,7 @@ public final class ExpressionParser { * * @author Adrien Hopkins * @since 2019-03-14 + * @since v0.2.0 */ private static enum TokenType { OBJECT, UNARY_OPERATOR, BINARY_OPERATOR; @@ -294,6 +325,7 @@ public final class ExpressionParser { * The opening bracket. * * @since 2019-03-22 + * @since v0.2.0 */ public static final char OPENING_BRACKET = '('; @@ -301,6 +333,7 @@ public final class ExpressionParser { * The closing bracket. * * @since 2019-03-22 + * @since v0.2.0 */ public static final char CLOSING_BRACKET = ')'; @@ -315,6 +348,7 @@ public final class ExpressionParser { * @throws NullPointerException * if string is null * @since 2019-03-22 + * @since v0.2.0 */ private static int findBracketPair(final String string, final int bracketPosition) { Objects.requireNonNull(string, "string must not be null."); @@ -361,6 +395,7 @@ public final class ExpressionParser { * use {@code Integer::parseInt}. * * @since 2019-03-14 + * @since v0.2.0 */ private final Function objectObtainer; @@ -368,6 +403,7 @@ public final class ExpressionParser { * A map mapping operator strings to operator functions, for unary operators. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map> unaryOperators; @@ -375,6 +411,7 @@ public final class ExpressionParser { * A map mapping operator strings to operator functions, for binary operators. * * @since 2019-03-14 + * @since v0.2.0 */ private final Map> binaryOperators; @@ -382,6 +419,7 @@ public final class ExpressionParser { * The operator for space, or null if spaces have no function. * * @since 2019-03-22 + * @since v0.2.0 */ private final String spaceOperator; @@ -397,6 +435,7 @@ public final class ExpressionParser { * @param spaceOperator * operator used by spaces * @since 2019-03-14 + * @since v0.2.0 */ private ExpressionParser(final Function objectObtainer, final Map> unaryOperators, @@ -419,6 +458,7 @@ public final class ExpressionParser { * expression * @return expression in RPN * @since 2019-03-17 + * @since v0.2.0 */ private String convertExpressionToReversePolish(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); @@ -523,6 +563,7 @@ public final class ExpressionParser { * @throws NullPointerException * if components is null * @since 2019-03-22 + * @since v0.2.0 */ private int findHighestPriorityOperatorPosition(final List components) { Objects.requireNonNull(components, "components must not be null."); @@ -572,6 +613,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code expression} is null * @since 2019-03-14 + * @since v0.2.0 */ private TokenType getTokenType(final String token) { Objects.requireNonNull(token, "token must not be null."); @@ -593,6 +635,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code expression} is null * @since 2019-03-14 + * @since v0.2.0 */ public T parseExpression(final String expression) { return this.parseReversePolishExpression(this.convertExpressionToReversePolish(expression)); @@ -607,6 +650,7 @@ public final class ExpressionParser { * @throws NullPointerException * if {@code expression} is null * @since 2019-03-14 + * @since v0.2.0 */ private T parseReversePolishExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); diff --git a/src/org/unitConverter/unit/AbstractUnit.java b/src/org/unitConverter/unit/AbstractUnit.java index a0d6f7e..05a6c17 100644 --- a/src/org/unitConverter/unit/AbstractUnit.java +++ b/src/org/unitConverter/unit/AbstractUnit.java @@ -110,7 +110,6 @@ public abstract class AbstractUnit implements Unit { return this.system; } - // TODO document and revise units' toString methods @Override public String toString() { return String.format("%s-derived unit of dimension %s", this.getSystem(), this.getDimension()); diff --git a/src/org/unitConverter/unit/BaseUnit.java b/src/org/unitConverter/unit/BaseUnit.java index 8bac866..67309cf 100755 --- a/src/org/unitConverter/unit/BaseUnit.java +++ b/src/org/unitConverter/unit/BaseUnit.java @@ -111,6 +111,7 @@ public final class BaseUnit extends LinearUnit { /** * @return true if the unit is a "full base" unit like the metre or second. * @since 2019-04-10 + * @since v0.2.0 */ public final boolean isFullBase() { return this.isFullBase; diff --git a/src/org/unitConverter/unit/DefaultUnitPrefix.java b/src/org/unitConverter/unit/DefaultUnitPrefix.java index c0e8dcc..4a9e487 100755 --- a/src/org/unitConverter/unit/DefaultUnitPrefix.java +++ b/src/org/unitConverter/unit/DefaultUnitPrefix.java @@ -33,6 +33,7 @@ public final class DefaultUnitPrefix implements UnitPrefix { * * @param multiplier * @since 2019-01-14 + * @since v0.2.0 */ public DefaultUnitPrefix(final double multiplier) { this.multiplier = multiplier; diff --git a/src/org/unitConverter/unit/LinearUnit.java b/src/org/unitConverter/unit/LinearUnit.java index 5b2680b..1b1ac97 100644 --- a/src/org/unitConverter/unit/LinearUnit.java +++ b/src/org/unitConverter/unit/LinearUnit.java @@ -175,6 +175,7 @@ public class LinearUnit extends AbstractUnit { * @throws NullPointerException * if {@code subtrahend} is null * @since 2019-03-17 + * @since v0.2.0 */ public LinearUnit minus(final LinearUnit subtrahendend) { Objects.requireNonNull(subtrahendend, "addend must not be null."); @@ -203,6 +204,7 @@ public class LinearUnit extends AbstractUnit { * @throws NullPointerException * if {@code addend} is null * @since 2019-03-17 + * @since v0.2.0 */ public LinearUnit plus(final LinearUnit addend) { Objects.requireNonNull(addend, "addend must not be null."); @@ -284,6 +286,7 @@ public class LinearUnit extends AbstractUnit { * prefix to apply * @return unit with prefix * @since 2019-03-18 + * @since v0.2.0 */ public LinearUnit withPrefix(final UnitPrefix prefix) { return this.times(prefix.getMultiplier()); diff --git a/src/org/unitConverter/unit/UnitPrefix.java b/src/org/unitConverter/unit/UnitPrefix.java index a1609c6..9f9645d 100755 --- a/src/org/unitConverter/unit/UnitPrefix.java +++ b/src/org/unitConverter/unit/UnitPrefix.java @@ -31,6 +31,7 @@ public interface UnitPrefix { * prefix to divide by * @return quotient of prefixes * @since 2019-04-13 + * @since v0.2.0 */ default UnitPrefix dividedBy(final UnitPrefix other) { return new DefaultUnitPrefix(this.getMultiplier() / other.getMultiplier()); @@ -50,6 +51,7 @@ public interface UnitPrefix { * prefix to multiply by * @return product of prefixes * @since 2019-04-13 + * @since v0.2.0 */ default UnitPrefix times(final UnitPrefix other) { return new DefaultUnitPrefix(this.getMultiplier() * other.getMultiplier()); @@ -62,6 +64,7 @@ public interface UnitPrefix { * exponent to raise to * @return result of exponentiation. * @since 2019-04-13 + * @since v0.2.0 */ default UnitPrefix toExponent(final double exponent) { return new DefaultUnitPrefix(Math.pow(getMultiplier(), exponent)); diff --git a/src/org/unitConverter/unit/package-info.java b/src/org/unitConverter/unit/package-info.java index c4493ae..dd5a939 100644 --- a/src/org/unitConverter/unit/package-info.java +++ b/src/org/unitConverter/unit/package-info.java @@ -19,5 +19,6 @@ * * @author Adrien Hopkins * @since 2019-01-25 + * @since v0.1.0 */ package org.unitConverter.unit; \ No newline at end of file diff --git a/src/test/java/ExpressionParserTest.java b/src/test/java/ExpressionParserTest.java index 62fa964..40c91ac 100644 --- a/src/test/java/ExpressionParserTest.java +++ b/src/test/java/ExpressionParserTest.java @@ -26,6 +26,7 @@ import org.unitConverter.math.ExpressionParser; * * @author Adrien Hopkins * @since 2019-03-22 + * @since v0.2.0 */ public class ExpressionParserTest { private static final ExpressionParser numberParser = new ExpressionParser.Builder<>(Integer::parseInt) diff --git a/src/test/java/UnitTest.java b/src/test/java/UnitTest.java index 952b6f2..00fcf3c 100755 --- a/src/test/java/UnitTest.java +++ b/src/test/java/UnitTest.java @@ -35,6 +35,7 @@ import org.unitConverter.unit.Unit; * * @author Adrien Hopkins * @since 2018-12-22 + * @since v0.1.0 */ public class UnitTest { /** A random number generator */ diff --git a/src/test/java/UnitsDatabaseTest.java b/src/test/java/UnitsDatabaseTest.java index 8429561..9222740 100644 --- a/src/test/java/UnitsDatabaseTest.java +++ b/src/test/java/UnitsDatabaseTest.java @@ -38,6 +38,7 @@ import org.unitConverter.unit.UnitPrefix; * * @author Adrien Hopkins * @since 2019-04-14 + * @since v0.2.0 */ public class UnitsDatabaseTest { // some linear units and one nonlinear @@ -72,6 +73,7 @@ public class UnitsDatabaseTest { * Test that prefixes correctly apply to units. * * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testPrefixes() { @@ -101,6 +103,7 @@ public class UnitsDatabaseTest { *

* * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testPrefixlessUnitMap() { @@ -123,6 +126,7 @@ public class UnitsDatabaseTest { * Tests that the database correctly stores and retrieves units, ignoring prefixes. * * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testPrefixlessUnits() { @@ -143,6 +147,7 @@ public class UnitsDatabaseTest { * Test that unit expressions return the correct value. * * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testUnitExpressions() { @@ -176,6 +181,7 @@ public class UnitsDatabaseTest { * Tests both the unit name iterator and the name-unit entry iterator * * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testUnitIterator() { @@ -221,6 +227,7 @@ public class UnitsDatabaseTest { *

* * @since 2019-04-14 + * @since v0.2.0 */ @Test public void testUnitPrefixCombinations() { diff --git a/src/test/java/package-info.java b/src/test/java/package-info.java index 87b4a06..3da7fcb 100644 --- a/src/test/java/package-info.java +++ b/src/test/java/package-info.java @@ -19,5 +19,6 @@ * * @author Adrien Hopkins * @since 2019-03-16 + * @since v0.2.0 */ package test.java; \ No newline at end of file -- cgit v1.2.3