> numericOperators,
final String spaceOperator) {
this.objectObtainer = objectObtainer;
this.unaryOperators = unaryOperators;
this.binaryOperators = binaryOperators;
this.numericOperators = numericOperators;
this.spaceOperator = spaceOperator;
}
/**
* 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
* @throws IllegalArgumentException if expression is invalid (e.g.
* "{@code 3 *}")
* @since 2019-03-17
* @since v0.2.0
*/
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);
// 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
components.addAll(Arrays.asList(partialExpression.split(" ")));
// remove empty entries
while (components.contains("")) {
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);
if (highestPriorityOperatorPosition == -1) {
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:
if (components.size() < 2)
throw new IllegalArgumentException(
"Invalid expression \"" + expression + "\"");
final String unaryOperator = components
.remove(highestPriorityOperatorPosition);
final String operand = components
.remove(highestPriorityOperatorPosition);
components.add(highestPriorityOperatorPosition,
operand + " " + unaryOperator);
break;
case BINARY_OPERATOR:
case NUMERIC_OPERATOR:
if (components.size() < 3)
throw new IllegalArgumentException(
"Invalid expression \"" + expression + "\"");
final String binaryOperator = components
.remove(highestPriorityOperatorPosition);
final String operand1 = components
.remove(highestPriorityOperatorPosition - 1);
final String operand2 = components
.remove(highestPriorityOperatorPosition - 1);
components.add(highestPriorityOperatorPosition - 1,
operand1 + " " + operand2 + " " + 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;
}
/**
* 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
* @since v0.2.0
*/
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;
case NUMERIC_OPERATOR:
final PriorityBiFunction numericOperator = this.numericOperators
.get(components.get(i));
final int numericPriority = numericOperator.getPriority();
if (numericPriority > maxPriority) {
maxPriority = numericPriority;
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
* @since v0.2.0
*/
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 if (this.numericOperators.containsKey(token))
return TokenType.NUMERIC_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
* @since v0.2.0
*/
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
* @since v0.2.0
*/
T parseReversePolishExpression(final String expression) {
Objects.requireNonNull(expression, "expression must not be null.");
final Deque stack = new ArrayDeque<>();
final Deque doubleStack = 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(o2, o1));
break;
case NUMERIC_OPERATOR:
if (stack.size() < 1 || doubleStack.size() < 1)
throw new IllegalStateException(String.format(
"Attempted to call binary operator %s with insufficient arguments.",
item));
final T ot = stack.pop();
final UncertainDouble on = doubleStack.pop();
final BiFunction op = this.numericOperators
.get(item);
stack.push(op.apply(ot, on));
break;
case OBJECT:
// just add it to the stack
// these try-catch statements are necessary
// to make the code as generalizable as possible
// also they're required for number formatting code because
// that's the only way to tell if an expression is a number or not.
try {
stack.push(this.objectObtainer.apply(item));
} catch (Exception e) {
try {
doubleStack.push(UncertainDouble.fromString(item));
} catch (IllegalArgumentException e2) {
try {
doubleStack.push(
UncertainDouble.of(Double.parseDouble(item), 0));
} catch (NumberFormatException e3) {
throw e;
}
}
}
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();
}
}