Document increment and decrement operators in SpEL
Closes gh-32136
This commit is contained in:
parent
ab98210e6d
commit
9b0162da49
|
|
@ -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]
|
||||
|
|
@ -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 --
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -306,43 +306,76 @@ class SpelDocumentationTests extends AbstractExpressionTests {
|
|||
|
||||
@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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue