Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#357 Provide a copy constructor / function #433

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ EvaluationValue result = expression
System.out.println(result.getNumberValue()); // prints 6.00
```

### Expression can be copied and evaluated with a different set of values:

```java
Expression expression = new Expression("a + b").with("a", 1).and("b", 2);
Expression copiedExpression = expression.copy().with("a", 3).and("b", 4);

EvaluationValue result = expression.evaluate();
EvaluationValue copiedResult = copiedExpression.evaluate();

System.out.println(result.getNumberValue()); // prints 3
System.out.println(copiedResult.getNumberValue()); // prints 7
```

### Values can be passed in a map

Instead of specifying the variable values one by one, they can be set by defining a map with names and values and then
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/ezylang/evalex/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public Expression(String expressionString, ExpressionConfiguration configuration
this.constants.putAll(configuration.getDefaultConstants());
}

/**
* Creates a copy with the same expression string, configuration, dataAccessor and syntax tree
* from an existing expression. The existing expression will be parsed to populate the syntax tree
*
* @param expression An existing expression.
* @throws ParseException If there were problems while parsing the existing expression.
*/
public Expression(Expression expression) throws ParseException {
this(expression.getExpressionString(), expression.getConfiguration());
this.abstractSyntaxTree = expression.getAbstractSyntaxTree();
}
/**
* Evaluates the expression by parsing it (if not done before) and the evaluating it.
*
Expand Down Expand Up @@ -311,6 +322,16 @@ public Expression withValues(Map<String, ?> values) {
return this;
}

/**
* Return a copy of the expression using the copy constructor {@link Expression(Expression)}.
*
* @return The copied Expression instance.
* @throws ParseException If there were problems while parsing the existing expression.
*/
public Expression copy() throws ParseException {
return new Expression(this);
}

/**
* Create an AST representation for an expression string. The node can then be used as a
* sub-expression. Subexpressions are not cached.
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/ezylang/evalex/ExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,16 @@ void testGetUndefinedVariables() throws ParseException {
Expression expression = new Expression("a+A+b+B+c+C+E+e+PI+x").with("x", 1);
assertThat(expression.getUndefinedVariables()).containsExactlyInAnyOrder("a", "b", "c");
}

@Test
void testCopy() throws ParseException, EvaluationException {
Expression expression = new Expression("a + b").with("a", 1).and("b", 2);
Expression copiedExpression = expression.copy().with("a", 3).and("b", 4);

EvaluationValue result = expression.evaluate();
EvaluationValue copiedResult = copiedExpression.evaluate();

assertThat(result.getStringValue()).isEqualTo("3");
assertThat(copiedResult.getStringValue()).isEqualTo("7");
}
}
Loading