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 |
---|---|
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.
|
Object |
bitwiseAnd(Object lhs,
Object rhs)
Deprecated.
|
Object |
bitwiseOr(Object lhs,
Object rhs)
Deprecated.
|
Object |
bitwiseXor(Object lhs,
Object rhs)
Deprecated.
|
Object |
complement(Object val)
Performs a bitwise complement.
|
Boolean |
contains(Object container,
Object value)
Test if left contains right (right matches/in left).
|
Iterable<?> |
createRange(Object from,
Object to)
Creates a literal range.
|
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.
|
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.
|
boolean |
isNegateStable()
Whether negate called with a given argument will always return the same result.
|
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 |
lessThan(Object left,
Object right)
Test if left < right.
|
boolean |
lessThanOrEqual(Object left,
Object right)
Test if left <= right.
|
Object |
logicalNot(Object arg)
Deprecated.
|
JexlArithmetic.MapBuilder |
mapBuilder(int size)
Called by the interpreter when evaluating a literal map.
|
Object |
matches(Object lhs,
Object rhs)
Deprecated.
|
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 back the value using the smallest type the result
will fit into.
|
boolean |
narrowArguments(Object[] args)
Replace all numbers in an arguments array with the smallest type that will fit.
|
Number |
narrowNumber(Number original,
Class<?> narrow)
Given a Number, return back 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).
|
JexlArithmetic.SetBuilder |
setBuilder(int size)
Called by the interpreter when evaluating a literal set.
|
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.
|
BigDecimal |
toBigDecimal(Object val)
Coerce to a BigDecimal.
|
BigInteger |
toBigInteger(Object val)
Coerce to a BigInteger.
|
boolean |
toBoolean(Object val)
Coerce to a primitive boolean.
|
double |
toDouble(Object val)
Coerce to a primitive double.
|
int |
toInteger(Object val)
Coerce to a primitive int.
|
long |
toLong(Object val)
Coerce to a primitive long.
|
String |
toString(Object val)
Coerce to a string.
|
Object |
xor(Object left,
Object right)
Performs a bitwise xor.
|
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)
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 failspublic boolean isStrict()
public MathContext getMathContext()
public int getMathScale()
public 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.public Number narrowNumber(Number original, Class<?> narrow)
original
- the original numbernarrow
- the attempted target classpublic boolean narrowArguments(Object[] args)
args
- the argument arraypublic 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.0public 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 negatepublic 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.
isNegateStable()
public Object positivize(Object val)
C/C++/C#/Java perform integral promotion of the operand, ie cast to int if type can 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 contains(Object container, Object value)
Beware that this method arguments are the opposite of the operator arguments. 'x in y' means 'y contains x'.
container
- the containervalue
- the valuepublic 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 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 argumentpublic boolean toBoolean(Object val)
Double.NaN, null, "false" and empty string coerce to false.
val
- value to coercepublic 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 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 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 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 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 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 possible@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–2021 The Apache Software Foundation. All rights reserved.