Skip to content

Commit

Permalink
#357 adds a copy api (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenylai authored Jan 23, 2024
1 parent ad4ad69 commit 1adf77f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
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");
}
}

0 comments on commit 1adf77f

Please sign in to comment.