Skip to content

Commit 057633e

Browse files
ngocnhan-tran1996sbrannen
authored andcommitted
Polish SpEL operator examples in reference docs
Closes gh-36367 Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent 7299ff9 commit 057633e

3 files changed

Lines changed: 89 additions & 33 deletions

File tree

  • framework-docs
    • modules/ROOT/pages/core/expressions/language-ref
    • src/main
      • java/org/springframework/docs/core/expressions/languageref/expressionsoperatorsoverloaded
      • kotlin/org/springframework/docs/core/expressions/languageref/expressionsoperatorsoverloaded

framework-docs/modules/ROOT/pages/core/expressions/language-ref/operators.adoc

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Kotlin::
5353
val trueValue = parser.parseExpression("'black' < 'block'").getValue(Boolean::class.java)
5454
5555
// uses CustomValue:::compareTo
56-
val trueValue = parser.parseExpression("new CustomValue(1) < new CustomValue(2)").getValue(Boolean::class.java);
56+
val trueValue = parser.parseExpression("new CustomValue(1) < new CustomValue(2)").getValue(Boolean::class.java)
5757
----
5858
======
5959

@@ -167,7 +167,7 @@ Kotlin::
167167
[CAUTION]
168168
====
169169
The syntax for the `between` operator is `<input> between {<range_begin>, <range_end>}`,
170-
which is effectively a shortcut for `<input> >= <range_begin> && <input> \<= <range_end>}`.
170+
which is effectively a shortcut for `<input> >= <range_begin> && <input> \<= <range_end>`.
171171
172172
Consequently, `1 between {1, 5}` evaluates to `true`, while `1 between {5, 1}` evaluates
173173
to `false`.
@@ -312,13 +312,13 @@ Kotlin::
312312
313313
// evaluates to 'a'
314314
val ch = parser.parseExpression("'d' - 3")
315-
.getValue(Character::class.java);
315+
.getValue(Char::class.java)
316316
317317
// -- Repeat --
318318
319319
// evaluates to "abcabc"
320320
val repeated = parser.parseExpression("'abc' * 2")
321-
.getValue(String::class.java);
321+
.getValue(String::class.java)
322322
----
323323
======
324324

@@ -485,7 +485,7 @@ Kotlin::
485485
486486
// -- Operator precedence --
487487
488-
val minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Int::class.java) // -21
488+
val minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Int::class.java) // -21
489489
----
490490
======
491491

@@ -541,32 +541,7 @@ For example, if we want to overload the `ADD` operator to allow two lists to be
541541
concatenated using the `+` sign, we can implement a custom `OperatorOverloader` as
542542
follows.
543543

544-
[source,java,indent=0,subs="verbatim,quotes"]
545-
----
546-
pubic class ListConcatenation implements OperatorOverloader {
547-
548-
@Override
549-
public boolean overridesOperation(Operation operation, Object left, Object right) {
550-
return (operation == Operation.ADD &&
551-
left instanceof List && right instanceof List);
552-
}
553-
554-
@Override
555-
@SuppressWarnings("unchecked")
556-
public Object operate(Operation operation, Object left, Object right) {
557-
if (operation == Operation.ADD &&
558-
left instanceof List list1 && right instanceof List list2) {
559-
560-
List result = new ArrayList(list1);
561-
result.addAll(list2);
562-
return result;
563-
}
564-
throw new UnsupportedOperationException(
565-
"No overload for operation %s and operands [%s] and [%s]"
566-
.formatted(operation, left, right));
567-
}
568-
}
569-
----
544+
include-code::./ListConcatenation[]
570545

571546
If we register `ListConcatenation` as the `OperatorOverloader` in a
572547
`StandardEvaluationContext`, we can then evaluate expressions like `{1, 2, 3} + {4, 5}`
@@ -589,8 +564,8 @@ Kotlin::
589564
+
590565
[source,kotlin,indent=0,subs="verbatim,quotes"]
591566
----
592-
StandardEvaluationContext context = StandardEvaluationContext()
593-
context.setOperatorOverloader(ListConcatenation())
567+
val context = StandardEvaluationContext()
568+
context.operatorOverloader = ListConcatenation()
594569
595570
// evaluates to a new list: [1, 2, 3, 4, 5]
596571
parser.parseExpression("{1, 2, 3} + {2 + 2, 5}").getValue(context, List::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.expressions.languageref.expressionsoperatorsoverloaded;
18+
19+
import org.springframework.expression.Operation;
20+
import org.springframework.expression.OperatorOverloader;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
public class ListConcatenation implements OperatorOverloader {
26+
27+
@Override
28+
public boolean overridesOperation(Operation operation, Object left, Object right) {
29+
return operation == Operation.ADD && left instanceof List && right instanceof List;
30+
}
31+
32+
@Override
33+
@SuppressWarnings({"rawtypes", "unchecked"})
34+
public Object operate(Operation operation, Object left, Object right) {
35+
if (operation == Operation.ADD &&
36+
left instanceof List list1 && right instanceof List list2) {
37+
38+
List result = new ArrayList(list1);
39+
result.addAll(list2);
40+
return result;
41+
}
42+
throw new UnsupportedOperationException(
43+
"No overload for operation %s and operands [%s] and [%s]"
44+
.formatted(operation, left, right));
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.expressions.languageref.expressionsoperatorsoverloaded
18+
19+
import org.springframework.expression.Operation
20+
import org.springframework.expression.OperatorOverloader
21+
22+
class ListConcatenation: OperatorOverloader {
23+
24+
override fun overridesOperation(operation: Operation, left: Any?, right: Any?): Boolean {
25+
return operation == Operation.ADD && left is List<*> && right is List<*>
26+
}
27+
28+
override fun operate(operation: Operation, left: Any?, right: Any?): Any {
29+
if (operation == Operation.ADD && left is List<*> && right is List<*>) {
30+
return left + right
31+
}
32+
33+
throw UnsupportedOperationException("No overload for operation $operation and operands [$left] and [$right]")
34+
}
35+
}

0 commit comments

Comments
 (0)