Clarify semantics of SpEL's between operator

See gh-32140
This commit is contained in:
Sam Brannen 2024-02-01 14:49:12 +01:00
parent d4401cc69a
commit a82108ec73
2 changed files with 11 additions and 13 deletions

View File

@ -162,15 +162,13 @@ Kotlin::
----
======
[NOTE]
[CAUTION]
====
The left operand to the `between` operator must be a single value, and the right operand
must be a 2-element list which defines the range.
The syntax for the `between` operator is `<input> between {<range_begin>, <range_end>}`,
which is effectively a shortcut for `<input> >= <range_begin> && <input> \<= <range_end>}`.
The `between` operator returns `true` if the left operand is _between_ the two elements
in the range, inclusive (using a comparison algorithm based on `java.lang.Comparable`).
In addition, the first element in the range must be less than or equal to the second
element in the range.
Consequently, `1 between {1, 5}` evaluates to `true`, while `1 between {5, 1}` evaluates
to `false`.
====
CAUTION: Be careful with primitive types, as they are immediately boxed up to their

View File

@ -29,12 +29,12 @@ import org.springframework.expression.spel.support.BooleanTypedValue;
* Represents the {@code between} operator.
*
* <p>The left operand must be a single value, and the right operand must be a
* 2-element list which defines the range.
* 2-element list which defines a range from a lower bound to an upper bound.
*
* <p>This operator returns {@code true} if the left operand is between the two
* elements in the range, inclusive (using the registered {@link TypeComparator}
* for comparison). In addition, the first element in the range must be less than
* or equal to the second element in the range.
* <p>This operator returns {@code true} if the left operand is greater than or
* equal to the lower bound and less than or equal to the upper bound. Consequently,
* {@code 1 between {1, 5}} evaluates to {@code true}, while {@code 1 between {5, 1}}
* evaluates to {@code false}.
*
* @author Andy Clement
* @author Sam Brannen
@ -50,7 +50,7 @@ public class OperatorBetween extends Operator {
/**
* Returns a boolean based on whether a value is in the range expressed. The first
* operand is any value whilst the second is a list of two values - those two values
* being the bounds allowed for the first operand (inclusive).
* being the lower and upper bounds allowed for the first operand (inclusive).
* @param state the expression state
* @return true if the left operand is in the range specified, false otherwise
* @throws EvaluationException if there is a problem evaluating the expression