Skip to content

Commit

Permalink
Document increment and decrement operators in SpEL
Browse files Browse the repository at this point in the history
Closes gh-32136
  • Loading branch information
sbrannen committed Jan 28, 2024
1 parent ab98210 commit 9b0162d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,19 @@ You can use the following operators on numbers, and standard operator precedence

* addition (`+`)
* subtraction (`-`)
* increment (`{pp}`)
* decrement (`--`)
* multiplication (`*`)
* division (`/`)
* modulus (`%`)
* exponential power (`^`)

[NOTE]
====
The increment and decrement operators can be used with either prefix (`{pp}A`, `--A`) or
postfix (`A{pp}`, `A--`) notation with variables or properties that can be written to.
====

The following example shows the mathematical operators in use:

[tabs]
Expand All @@ -289,7 +297,11 @@ Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
Inventor inventor = new Inventor();
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
// -- Addition --
int two = parser.parseExpression("1 + 1").getValue(int.class); // 2
// -- Subtraction --
Expand All @@ -298,6 +310,26 @@ Java::
double d = parser.parseExpression("1000.00 - 1e4").getValue(double.class); // -9000
// -- Increment --
// The counter property in Inventor has an initial value of 0.
// evaluates to 2; counter is now 1
two = parser.parseExpression("counter++ + 2").getValue(context, inventor, int.class);
// evaluates to 5; counter is now 2
int five = parser.parseExpression("3 + ++counter").getValue(context, inventor, int.class);
// -- Decrement --
// The counter property in Inventor has a value of 2.
// evaluates to 6; counter is now 1
int six = parser.parseExpression("counter-- + 4").getValue(context, inventor, int.class);
// evaluates to 5; counter is now 0
five = parser.parseExpression("5 + --counter").getValue(context, inventor, int.class);
// -- Multiplication --
six = parser.parseExpression("-2 * -3").getValue(int.class); // 6
Expand Down Expand Up @@ -329,6 +361,9 @@ Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
----
val inventor = Inventor()
val context = SimpleEvaluationContext.forReadWriteDataBinding().build()
// -- Addition --
var two = parser.parseExpression("1 + 1").getValue(Int::class.java) // 2
Expand All @@ -339,6 +374,26 @@ Kotlin::
val d = parser.parseExpression("1000.00 - 1e4").getValue(Double::class.java) // -9000
// -- Increment --
// The counter property in Inventor has an initial value of 0.
// evaluates to 2; counter is now 1
two = parser.parseExpression("counter++ + 2").getValue(context, inventor, Int::class.java)
// evaluates to 5; counter is now 2
var five = parser.parseExpression("3 + ++counter").getValue(context, inventor, Int::class.java)
// -- Decrement --
// The counter property in Inventor has a value of 2.
// evaluates to 6; counter is now 1
var six = parser.parseExpression("counter-- + 4").getValue(context, inventor, Int::class.java)
// evaluates to 5; counter is now 0
five = parser.parseExpression("5 + --counter").getValue(context, inventor, Int::class.java)
// -- Multiplication --
six = parser.parseExpression("-2 * -3").getValue(Int::class.java) // 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,43 +306,76 @@ void stringOperators() {

@Test
void mathematicalOperators() {
// Addition
Inventor inventor = new Inventor();
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

// -- Addition --

int two = parser.parseExpression("1 + 1").getValue(int.class); // 2
assertThat(two).isEqualTo(2);

// Subtraction
// -- Subtraction --

int four = parser.parseExpression("1 - -3").getValue(int.class); // 4
assertThat(four).isEqualTo(4);

double d = parser.parseExpression("1000.00 - 1e4").getValue(double.class); // -9000
assertThat(d).isCloseTo(-9000.0d, within((double) 0));

// Multiplication
int six = parser.parseExpression("-2 * -3").getValue(int.class); // 6
// -- Increment --

// The counter property in Inventor has an initial value of 0.

// evaluates to 2; counter is now 1
two = parser.parseExpression("counter++ + 2").getValue(context, inventor, int.class);
assertThat(two).isEqualTo(2);

// evaluates to 5; counter is now 2
int five = parser.parseExpression("3 + ++counter").getValue(context, inventor, int.class);
assertThat(five).isEqualTo(5);

// -- Decrement --

// The counter property in Inventor has a value of 2.

// evaluates to 6; counter is now 1
int six = parser.parseExpression("counter-- + 4").getValue(context, inventor, int.class);
assertThat(six).isEqualTo(6);

// evaluates to 5; counter is now 0
five = parser.parseExpression("5 + --counter").getValue(context, inventor, int.class);
assertThat(five).isEqualTo(5);

// -- Multiplication --

six = parser.parseExpression("-2 * -3").getValue(int.class); // 6
assertThat(six).isEqualTo(6);

double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(double.class); // 24.0
assertThat(twentyFour).isCloseTo(24.0d, within((double) 0));

// Division
// -- Division --

int minusTwo = parser.parseExpression("6 / -3").getValue(int.class); // -2
assertThat(minusTwo).isEqualTo(-2);

double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(double.class); // 1.0
assertThat(one).isCloseTo(1.0d, within((double) 0));

// Modulus
// -- Modulus --

int three = parser.parseExpression("7 % 4").getValue(int.class); // 3
assertThat(three).isEqualTo(3);

int oneInt = parser.parseExpression("8 / 5 % 2").getValue(int.class); // 1
assertThat(oneInt).isEqualTo(1);

// Exponential power
// -- Exponential power --

int maxInt = parser.parseExpression("(2^31) - 1").getValue(int.class); // Integer.MAX_VALUE
assertThat(maxInt).isEqualTo(Integer.MAX_VALUE);

// Operator precedence
// -- Operator precedence --

int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(int.class); // -21
assertThat(minusTwentyOne).isEqualTo(-21);
Expand Down

0 comments on commit 9b0162d

Please sign in to comment.