public class JexlArithmetic extends Object
This is the class to derive to implement new operator behaviors.
The 5 base arithmetic operators (+, - , *, /, %) follow the same evaluation rules regarding their arguments.
JexlOperator
Modifier and Type | Class and Description |
---|---|
static interface |
JexlArithmetic.ArrayBuilder
Helper interface used when creating an array literal.
|
static interface |
JexlArithmetic.MapBuilder
Helper interface used when creating a map literal.
|
static class |
JexlArithmetic.NullOperand
Marker class for null operand exceptions.
|
static interface |
JexlArithmetic.SetBuilder
Helper interface used when creating a set literal.
|
static interface |
JexlArithmetic.Uberspect
The interface that uberspects JexlArithmetic classes.
|
Modifier and Type | Field and Description |
---|---|
protected static BigDecimal |
BIGD_DOUBLE_MAX_VALUE
Double.MAX_VALUE as BigDecimal.
|
protected static BigDecimal |
BIGD_DOUBLE_MIN_VALUE
Double.MIN_VALUE as BigDecimal.
|
protected static int |
BIGD_SCALE
Default BigDecimal scale.
|
protected static BigInteger |
BIGI_LONG_MAX_VALUE
Long.MAX_VALUE as BigInteger.
|
protected static BigInteger |
BIGI_LONG_MIN_VALUE
Long.MIN_VALUE as BigInteger.
|
static Pattern |
FLOAT_PATTERN
The float regular expression pattern.
|
Constructor and Description |
---|
JexlArithmetic(boolean astrict)
Creates a JexlArithmetic.
|
JexlArithmetic(boolean astrict,
MathContext bigdContext,
int bigdScale)
Creates a JexlArithmetic.
|
Modifier and Type | Method and Description |
---|---|
Object |
add(Object left,
Object right)
Add two values together.
|
Object |
and(Object left,
Object right)
Performs a bitwise and.
|
JexlArithmetic.ArrayBuilder |
arrayBuilder(int size)
Called by the interpreter when evaluating a literal array.
|
protected Number |
asLongNumber(Object value)
Checks if value class is a number that can be represented exactly in a long.
|
Object |
bitwiseAnd(Object lhs,
Object rhs)
Deprecated.
3.0
|
Object |
bitwiseOr(Object lhs,
Object rhs)
Deprecated.
3.0
|
Object |
bitwiseXor(Object lhs,
Object rhs)
Deprecated.
3.0
|
protected Boolean |
collectionContains(Object collection,
Object value)
Checks whether a potential collection contains another.
|
protected int |
compare(Object left,
Object right,
JexlOperator operator)
Performs a comparison.
|
protected int |
compare(Object left,
Object right,
String symbol)
Deprecated.
3.3
|
Object |
complement(Object val)
Performs a bitwise complement.
|
Boolean |
contains(Object container,
Object value)
Test if left contains right (right matches/in left).
|
protected Object |
controlNullNullOperands()
Deprecated.
3.3
|
protected Object |
controlNullNullOperands(JexlOperator operator)
The result of +,/,-,*,% when both operands are null.
|
protected void |
controlNullOperand()
Deprecated.
3.3
|
protected <T> T |
controlNullOperand(boolean strictCast,
T defaultValue)
Throws an NullOperand exception if arithmetic is strict-cast.
|
Object |
controlReturn(Object returned)
The last method called before returning a result from a script execution.
|
Iterable<?> |
createRange(Object from,
Object to)
Creates a literal range.
|
protected JexlArithmetic |
createWithOptions(boolean astrict,
MathContext bigdContext,
int bigdScale)
Creates a JexlArithmetic instance.
|
Object |
decrement(Object val)
Decrements argument by 1.
|
Object |
divide(Object left,
Object right)
Divide the left value by the right.
|
Boolean |
empty(Object object)
Check for emptiness of various types: Number, Collection, Array, Map, String.
|
Boolean |
endsWith(Object left,
Object right)
Test if left ends with right.
|
boolean |
equals(Object left,
Object right)
Test if left and right are equal.
|
MathContext |
getMathContext()
The MathContext instance used for +,-,/,*,% operations on big decimals.
|
int |
getMathScale()
The BigDecimal scale used for comparison and coericion operations.
|
boolean |
greaterThan(Object left,
Object right)
Test if left > right.
|
boolean |
greaterThanOrEqual(Object left,
Object right)
Test if left >= right.
|
Object |
increment(Object val)
Increments argument by 1.
|
protected Object |
increment(Object val,
int incr)
Add value to number argument.
|
Boolean |
isEmpty(Object object)
Check for emptiness of various types: Number, Collection, Array, Map, String.
|
Boolean |
isEmpty(Object object,
Boolean def)
Check for emptiness of various types: Number, Collection, Array, Map, String.
|
protected boolean |
isFloatingPoint(Object o)
Is Object a floating point number.
|
protected boolean |
isFloatingPointNumber(Object val)
Test if the passed value is a floating point number, i.e.
|
protected static boolean |
isMultiplyExact(long x,
long y,
long r)
Checks if the product of the arguments overflows a
long . |
boolean |
isNegateStable()
Whether negate called with a given argument will always return the same result.
|
protected boolean |
isNullOperand(Object value)
Checks if an operand is considered null.
|
protected boolean |
isNumberable(Object o)
Is Object a whole number.
|
boolean |
isPositivizeStable()
Whether positivize called with a given argument will always return the same result.
|
boolean |
isStrict()
Checks whether this JexlArithmetic instance
strictly considers null as an error when used as operand unexpectedly.
|
boolean |
isStrict(JexlOperator operator)
Checks whether this arithmetic considers a given operator as strict or null-safe.
|
boolean |
lessThan(Object left,
Object right)
Test if left < right.
|
boolean |
lessThanOrEqual(Object left,
Object right)
Test if left <= right.
|
Object |
logicalNot(Object arg)
Deprecated.
3.0
|
JexlArithmetic.MapBuilder |
mapBuilder(int size)
Called by the interpreter when evaluating a literal map.
|
Object |
matches(Object lhs,
Object rhs)
Deprecated.
3.0
|
Object |
mod(Object left,
Object right)
left value modulo right.
|
Object |
multiply(Object left,
Object right)
Multiply the left value by the right.
|
Number |
narrow(Number original)
Given a Number, return the value using the smallest type the result
will fit into.
|
protected boolean |
narrowAccept(Class<?> narrow,
Class<?> source)
Whether we consider the narrow class as a potential candidate for narrowing the source.
|
boolean |
narrowArguments(Object[] args)
Replace all numbers in an arguments array with the smallest type that will fit.
|
protected Number |
narrowBigDecimal(Object lhs,
Object rhs,
BigDecimal bigd)
Given a BigDecimal, attempt to narrow it to an Integer or Long if it fits and
one of the arguments is a numberable.
|
protected Number |
narrowBigInteger(Object lhs,
Object rhs,
BigInteger bigi)
Given a BigInteger, narrow it to an Integer or Long if it fits and the arguments
class allow it.
|
protected Number |
narrowLong(Object lhs,
Object rhs,
long r)
Given a long, attempt to narrow it to an int.
|
Number |
narrowNumber(Number original,
Class<?> narrow)
Given a Number, return the value attempting to narrow it to a target class.
|
Object |
negate(Object val)
Negates a value (unary minus for numbers).
|
Object |
not(Object val)
Performs a logical not.
|
JexlArithmetic |
options(JexlContext context)
Apply options to this arithmetic which eventually may create another instance.
|
JexlArithmetic |
options(JexlEngine.Options options)
Deprecated.
3.2
|
JexlArithmetic |
options(JexlOptions options)
Apply options to this arithmetic which eventually may create another instance.
|
Object |
or(Object left,
Object right)
Performs a bitwise or.
|
Object |
positivize(Object val)
Positivize value (unary plus for numbers).
|
protected BigDecimal |
roundBigDecimal(BigDecimal number)
Ensure a big decimal is rounded by this arithmetic scale and rounding mode.
|
JexlArithmetic.SetBuilder |
setBuilder(int size)
Called by the interpreter when evaluating a literal set.
|
Object |
shiftLeft(Object left,
Object right)
Shifts a bit pattern to the right.
|
Object |
shiftRight(Object left,
Object right)
Shifts a bit pattern to the right.
|
Object |
shiftRightUnsigned(Object left,
Object right)
Shifts a bit pattern to the right unsigned.
|
Integer |
size(Object object)
Calculate the
size of various types: Collection, Array, Map, String. |
Integer |
size(Object object,
Integer def)
Calculate the
size of various types: Collection, Array, Map, String. |
Boolean |
startsWith(Object left,
Object right)
Test if left starts with right.
|
Object |
subtract(Object left,
Object right)
Subtract the right value from the left.
|
boolean |
testPredicate(Object object)
Test if a condition is true or false.
|
protected BigDecimal |
toBigDecimal(boolean strict,
Object val)
Coerce to a BigDecimal.
|
BigDecimal |
toBigDecimal(Object val)
Coerce to a BigDecimal.
|
protected BigInteger |
toBigInteger(boolean strict,
Object val)
Coerce to a BigInteger.
|
BigInteger |
toBigInteger(Object val)
Coerce to a BigInteger.
|
protected boolean |
toBoolean(boolean strict,
Object val)
Coerce to a primitive boolean.
|
boolean |
toBoolean(Object val)
Coerce to a primitive boolean.
|
protected double |
toDouble(boolean strict,
Object val)
Coerce to a primitive double.
|
double |
toDouble(Object val)
Coerce to a primitive double.
|
protected int |
toInteger(boolean strict,
Object val)
Coerce to a primitive int.
|
int |
toInteger(Object val)
Coerce to a primitive int.
|
protected long |
toLong(boolean strict,
Object val)
Coerce to a primitive long.
|
long |
toLong(Object val)
Coerce to a primitive long.
|
protected String |
toString(boolean strict,
Object val)
Coerce to a string.
|
String |
toString(Object val)
Coerce to a string.
|
Object |
xor(Object left,
Object right)
Performs a bitwise xor.
|
protected static final BigDecimal BIGD_DOUBLE_MAX_VALUE
protected static final BigDecimal BIGD_DOUBLE_MIN_VALUE
protected static final BigInteger BIGI_LONG_MAX_VALUE
protected static final BigInteger BIGI_LONG_MIN_VALUE
protected static final int BIGD_SCALE
public static final Pattern FLOAT_PATTERN
The decimal and exponent parts are optional and captured allowing to determine if the number is a real by checking whether one of these 2 capturing groups is not empty.
public JexlArithmetic(boolean astrict)
If you derive your own arithmetic, implement the other constructor that may be needed when dealing with options.
astrict
- whether this arithmetic is strict or lenientpublic JexlArithmetic(boolean astrict, MathContext bigdContext, int bigdScale)
The constructor to define in derived classes.
astrict
- whether this arithmetic is lenient or strictbigdContext
- the math context instance to use for +,-,/,*,% operations on big decimals.bigdScale
- the scale used for big decimals.public JexlArithmetic options(JexlOptions options)
options
- the JexlEngine.Options
to usecreateWithOptions(boolean, java.math.MathContext, int)
@Deprecated public JexlArithmetic options(JexlEngine.Options options)
options
- the JexlEngine.Options
to usecreateWithOptions(boolean, java.math.MathContext, int)
public JexlArithmetic options(JexlContext context)
context
- the context that may extend JexlContext.OptionsHandle
to usecreateWithOptions(boolean, java.math.MathContext, int)
protected JexlArithmetic createWithOptions(boolean astrict, MathContext bigdContext, int bigdScale)
astrict
- whether this arithmetic is lenient or strictbigdContext
- the math context instance to use for +,-,/,*,% operations on big decimals.bigdScale
- the scale used for big decimals.options(org.apache.commons.jexl3.JexlEngine.Options)
public JexlArithmetic.ArrayBuilder arrayBuilder(int size)
size
- the number of elements in the arraypublic JexlArithmetic.SetBuilder setBuilder(int size)
size
- the number of elements in the setpublic JexlArithmetic.MapBuilder mapBuilder(int size)
size
- the number of elements in the mappublic Iterable<?> createRange(Object from, Object to) throws ArithmeticException
The default implementation only accepts integers and longs.
from
- the included lower bound value (null if none)to
- the included upper bound value (null if none)ArithmeticException
- as an option if creation failsprotected boolean isNullOperand(Object value)
value
- the operandprotected <T> T controlNullOperand(boolean strictCast, T defaultValue)
This method is called by the cast methods (toBoolean(boolean, Object)
,
toInteger(boolean, Object)
, toDouble(boolean, Object)
,
toString(boolean, Object)
, toBigInteger(boolean, Object)
,
toBigDecimal(boolean, Object)
) when they encounter a null argument.
T
- the value typestrictCast
- whether strict cast is requireddefaultValue
- the default value to return, if not strictJexlArithmetic.NullOperand
- if strict-castprotected Object controlNullNullOperands(JexlOperator operator)
operator
- the actual operatorJexlArithmetic.NullOperand
- if strict-castprotected boolean toBoolean(boolean strict, Object val)
Double.NaN, null, "false" and empty string coerce to false.
val
- value to coercestrict
- true if the calling operator or casting is strict, false otherwisepublic boolean toBoolean(Object val)
Double.NaN, null, "false" and empty string coerce to false.
val
- value to coerceprotected int toInteger(boolean strict, Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
strict
- true if the calling operator or casting is strict, false otherwiseval
- value to coerceArithmeticException
- if val is null and mode is strict or if coercion is not possiblepublic int toInteger(Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
val
- value to coerceArithmeticException
- if val is null and mode is strict or if coercion is not possibleprotected long toLong(boolean strict, Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
strict
- true if the calling operator or casting is strict, false otherwiseval
- value to coerceArithmeticException
- if value is null and mode is strict or if coercion is not possiblepublic long toLong(Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
val
- value to coerceArithmeticException
- if value is null and mode is strict or if coercion is not possibleprotected BigInteger toBigInteger(boolean strict, Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
strict
- true if the calling operator or casting is strict, false otherwiseval
- the object to be coerced.ArithmeticException
- if val is null and mode is strict or if coercion is not possiblepublic BigInteger toBigInteger(Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
val
- the object to be coerced.ArithmeticException
- if val is null and mode is strict or if coercion is not possibleprotected BigDecimal toBigDecimal(boolean strict, Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
strict
- true if the calling operator or casting is strict, false otherwiseval
- the object to be coerced.ArithmeticException
- if val is null and mode is strict or if coercion is not possiblepublic BigDecimal toBigDecimal(Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
val
- the object to be coerced.ArithmeticException
- if val is null and mode is strict or if coercion is not possibleprotected double toDouble(boolean strict, Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
strict
- true if the calling operator or casting is strict, false otherwiseval
- value to coerce.ArithmeticException
- if val is null and mode is strict or if coercion is not possiblepublic double toDouble(Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
val
- value to coerce.ArithmeticException
- if val is null and mode is strict or if coercion is not possibleprotected String toString(boolean strict, Object val)
Double.NaN coerce to the empty string.
strict
- true if the calling operator or casting is strict, false otherwiseval
- value to coerce.ArithmeticException
- if val is null and mode is strict or if coercion is not possiblepublic String toString(Object val)
Double.NaN coerce to the empty string.
val
- value to coerce.ArithmeticException
- if val is null and mode is strict or if coercion is not possiblepublic boolean isStrict()
public boolean isStrict(JexlOperator operator)
When an operator is strict, it does not accept null arguments when the arithmetic is strict. If null-safe (ie not-strict), the operator does accept null arguments even if the arithmetic itself is strict.
The default implementation considers equal/not-equal operators as null-safe so one can check for null as in
if (myvar == null) {...}
. Note that this operator is used for equal and not-equal syntax. The complete
list of operators that are not strict are (==, [], []=, ., .=, empty, size, contains).
An arithmetic refining its strict behavior handling for more operators must declare which by overriding this method.
operator
- the operator to check for null-argument(s) handlingpublic MathContext getMathContext()
public int getMathScale()
protected BigDecimal roundBigDecimal(BigDecimal number)
number
- the big decimal to round@Deprecated protected Object controlNullNullOperands()
JexlArithmetic.NullOperand
- if strict@Deprecated protected void controlNullOperand()
JexlArithmetic.NullOperand
- if strictprotected boolean isFloatingPointNumber(Object val)
val
- the object to be testedprotected boolean isFloatingPoint(Object o)
o
- Object to be analyzed.protected boolean isNumberable(Object o)
o
- Object to be analyzed.public Object controlReturn(Object returned)
returned
- the returned valuepublic Number narrow(Number original)
This works hand in hand with parameter 'widening' in java method calls, e.g. a call to substring(int,int) with an int and a long will fail, but a call to substring(int,int) with an int and a short will succeed.
original
- the original number.protected boolean narrowAccept(Class<?> narrow, Class<?> source)
narrow
- the target narrow classsource
- the original source classpublic Number narrowNumber(Number original, Class<?> narrow)
original
- the original numbernarrow
- the attempted target classprotected Number narrowBigInteger(Object lhs, Object rhs, BigInteger bigi)
The rules are: if either arguments is a BigInteger, no narrowing will occur if either arguments is a Long, no narrowing to Integer will occur
lhs
- the left-hand side operand that lead to the bigi resultrhs
- the right-hand side operand that lead to the bigi resultbigi
- the BigInteger to narrowprotected Number narrowBigDecimal(Object lhs, Object rhs, BigDecimal bigd)
lhs
- the left-hand side operand that lead to the bigd resultrhs
- the right-hand side operand that lead to the bigd resultbigd
- the BigDecimal to narrowpublic boolean narrowArguments(Object[] args)
args
- the argument arrayprotected Number narrowLong(Object lhs, Object rhs, long r)
Narrowing will only occur if no operand is a Long.
lhs
- the left hand side operand that lead to the long resultrhs
- the right hand side operand that lead to the long resultr
- the long to narrowprotected Number asLongNumber(Object value)
value
- argumentpublic Object increment(Object val)
val
- the argumentpublic Object decrement(Object val)
val
- the argumentprotected Object increment(Object val, int incr)
val
- the numberincr
- the value to addpublic Object add(Object left, Object right)
If any numeric add fails on coercion to the appropriate type, treat as Strings and do concatenation.
left
- left argumentright
- right argumentpublic Object divide(Object left, Object right)
left
- left argumentright
- right argumentArithmeticException
- if right == 0public Object mod(Object left, Object right)
left
- left argumentright
- right argumentArithmeticException
- if right == 0.0protected static boolean isMultiplyExact(long x, long y, long r)
long
.
see java8 Math.multiplyExact
x
- the first valuey
- the second valuer
- the productpublic Object multiply(Object left, Object right)
left
- left argumentright
- right argumentpublic Object subtract(Object left, Object right)
left
- left argumentright
- right argumentpublic Object negate(Object val)
val
- the value to negateisNegateStable()
public boolean isNegateStable()
This is used to determine whether negate results on number literals can be cached. If the result on calling negate with the same constant argument may change between calls, which means the function is not deterministic, this method must return false.
public Object positivize(Object val)
C/C++/C#/Java perform integral promotion of the operand, ie cast to int if type can be represented as int without loss of precision.
val
- the value to positivizeisPositivizeStable()
public boolean isPositivizeStable()
This is used to determine whether positivize results on number literals can be cached. If the result on calling positivize with the same constant argument may change between calls, which means the function is not deterministic, this method must return false.
public boolean testPredicate(Object object)
object
- the object to use as conditionpublic Boolean contains(Object container, Object value)
Beware that this "contains " method arguments order is the opposite of the
"in/matches" operator arguments.
x =~ y
means y contains x
thus contains(x, y)
.
When this method returns null during evaluation, the operator code continues trying to find one through the uberspect.
container
- the containervalue
- the valueprotected Boolean collectionContains(Object collection, Object value)
Made protected to make it easier to override if needed.
collection
- the container which can be a collection or an array (even of primitive)value
- the value which can be a collection or an array (even of primitive) or a singletonpublic Boolean endsWith(Object left, Object right)
left
- left argumentright
- right argumentpublic Boolean startsWith(Object left, Object right)
left
- left argumentright
- right argumentpublic Boolean empty(Object object)
Override or overload this method to add new signatures to the size operators.
object
- the object to check the emptiness ofpublic Boolean isEmpty(Object object)
object
- the object to check the emptiness ofpublic Boolean isEmpty(Object object, Boolean def)
object
- the object to check the emptiness ofdef
- the default value if object emptyness can not be determinedpublic Integer size(Object object)
size
of various types: Collection, Array, Map, String.object
- the object to get the size ofpublic Integer size(Object object, Integer def)
size
of various types: Collection, Array, Map, String.object
- the object to get the size ofdef
- the default value if object size can not be determinedpublic Object and(Object left, Object right)
left
- the left operandright
- the right operatorpublic Object or(Object left, Object right)
left
- the left operandright
- the right operatorpublic Object xor(Object left, Object right)
left
- the left operandright
- the right operatorpublic Object complement(Object val)
val
- the operandpublic Object not(Object val)
val
- the operandpublic Object shiftLeft(Object left, Object right)
left
- left argumentright
- right argumentpublic Object shiftRight(Object left, Object right)
left
- left argumentright
- right argumentpublic Object shiftRightUnsigned(Object left, Object right)
left
- left argumentright
- right argument@Deprecated protected int compare(Object left, Object right, String symbol)
left
- left operandright
- right operandsymbol
- the operator symbolcompare(Object, Object, JexlOperator)
protected int compare(Object left, Object right, JexlOperator operator)
left
- the left operandright
- the right operatoroperator
- the operatorArithmeticException
- if either left or right is nullpublic boolean equals(Object left, Object right)
left
- left argumentright
- right argumentpublic boolean lessThan(Object left, Object right)
left
- left argumentright
- right argumentpublic boolean greaterThan(Object left, Object right)
left
- left argumentright
- right argumentpublic boolean lessThanOrEqual(Object left, Object right)
left
- left argumentright
- right argumentpublic boolean greaterThanOrEqual(Object left, Object right)
left
- left argumentright
- right argument@Deprecated public final Object bitwiseAnd(Object lhs, Object rhs)
lhs
- left hand siderhs
- right hand sideand(java.lang.Object, java.lang.Object)
@Deprecated public final Object bitwiseOr(Object lhs, Object rhs)
lhs
- left hand siderhs
- right hand sideor(java.lang.Object, java.lang.Object)
@Deprecated public final Object bitwiseXor(Object lhs, Object rhs)
lhs
- left hand siderhs
- right hand sidexor(java.lang.Object, java.lang.Object)
@Deprecated public final Object logicalNot(Object arg)
arg
- argumentnot(java.lang.Object)
@Deprecated public final Object matches(Object lhs, Object rhs)
lhs
- left hand siderhs
- right hand sidecontains(java.lang.Object, java.lang.Object)
Copyright © 2001–2022 The Apache Software Foundation. All rights reserved.