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.
|
Object |
bitwiseAnd(Object lhs,
Object rhs)
Deprecated.
|
Object |
bitwiseOr(Object lhs,
Object rhs)
Deprecated.
|
Object |
bitwiseXor(Object lhs,
Object rhs)
Deprecated.
|
protected int |
compare(Object left,
Object right,
String operator)
Performs a comparison.
|
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()
The result of +,/,-,*,% when both operands are null.
|
protected void |
controlNullOperand()
Throw a NPE if arithmetic is strict.
|
Iterable<?> |
createRange(Object from,
Object to)
Creates a literal range.
|
Object |
divide(Object left,
Object right)
Divide the left value by the right.
|
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 emptyness 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. a float, double
or string with ( "." | "E" | "e").
|
protected boolean |
isNumberable(Object o)
Is Object a whole number.
|
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.
|
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 if
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.
|
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(JexlEngine.Options options)
Apply options to this arithmetic which eventually may create another instance.
|
Object |
or(Object left,
Object right)
Performs a bitwise or.
|
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.
|
Integer |
size(Object object)
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.
|
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
public JexlArithmetic(boolean astrict)
astrict
- whether this arithmetic is strict or lenientpublic JexlArithmetic(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.public JexlArithmetic options(JexlEngine.Options options)
options
- the JexlEngine.Options
to usepublic 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()
protected BigDecimal roundBigDecimal(BigDecimal number)
number
- the big decimal to roundprotected Object controlNullNullOperands()
ArithmeticException
- if strictprotected void controlNullOperand()
ArithmeticException
- 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 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 orginal 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 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 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 isEmpty(Object object)
object
- the object to check the emptyness ofpublic Integer size(Object object)
size
of various types: Collection, Array, Map, String.object
- the object to get the size ofpublic 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 operandprotected int compare(Object left, Object right, String 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 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–2015 The Apache Software Foundation. All rights reserved.