|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.jexl3.JexlArithmetic
public class JexlArithmetic
Perform arithmetic, implements JexlOperator methods.
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
Nested Class Summary | |
---|---|
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. |
Field Summary | |
---|---|
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 Summary | |
---|---|
JexlArithmetic(boolean astrict)
Creates a JexlArithmetic. |
|
JexlArithmetic(boolean astrict,
MathContext bigdContext,
int bigdScale)
Creates a JexlArithmetic. |
Method Summary | |
---|---|
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. |
protected JexlArithmetic |
createWithOptions(boolean astrict,
MathContext bigdContext,
int bigdScale)
Creates a JexlArithmetic instance. |
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. |
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(JexlContext context)
Apply options to this arithmetic which eventually may create another instance. |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
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.
Constructor Detail |
---|
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.Method Detail |
---|
public JexlArithmetic options(JexlEngine.Options options)
options
- the JexlEngine.Options
to use
createWithOptions(boolean, java.math.MathContext, int)
public JexlArithmetic options(JexlContext context)
context
- the context that may extend JexlEngine.Options
to use
createWithOptions(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 array
public JexlArithmetic.SetBuilder setBuilder(int size)
size
- the number of elements in the set
public JexlArithmetic.MapBuilder mapBuilder(int size)
size
- the number of elements in the map
public 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 round
protected Object controlNullNullOperands()
ArithmeticException
- if strictprotected void controlNullOperand()
ArithmeticException
- if strictprotected boolean isFloatingPointNumber(Object val)
val
- the object to be tested
protected 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 class
public Number narrowNumber(Number original, Class<?> narrow)
original
- the original numbernarrow
- the attempted target class
protected 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 narrow
protected 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 narrow
public boolean narrowArguments(Object[] args)
args
- the argument array
public 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 argument
public Object divide(Object left, Object right)
left
- left argumentright
- right argument
ArithmeticException
- if right == 0public Object mod(Object left, Object right)
left
- left argumentright
- right argument
ArithmeticException
- if right == 0.0public Object multiply(Object left, Object right)
left
- left argumentright
- right argument
public Object subtract(Object left, Object right)
left
- left argumentright
- right argument
public Object negate(Object val)
val
- the value to negate
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 value
public Boolean endsWith(Object left, Object right)
left
- left argumentright
- right argument
public Boolean startsWith(Object left, Object right)
left
- left argumentright
- right argument
public Boolean isEmpty(Object object)
object
- the object to check the emptyness of
public Integer size(Object object)
size
of various types: Collection, Array, Map, String.
object
- the object to get the size of
public Object and(Object left, Object right)
left
- the left operandright
- the right operator
public Object or(Object left, Object right)
left
- the left operandright
- the right operator
public Object xor(Object left, Object right)
left
- the left operandright
- the right operator
public Object complement(Object val)
val
- the operand
public Object not(Object val)
val
- the operand
protected int compare(Object left, Object right, String operator)
left
- the left operandright
- the right operatoroperator
- the operator
ArithmeticException
- if either left or right is nullpublic boolean equals(Object left, Object right)
left
- left argumentright
- right argument
public boolean lessThan(Object left, Object right)
left
- left argumentright
- right argument
public boolean greaterThan(Object left, Object right)
left
- left argumentright
- right argument
public boolean lessThanOrEqual(Object left, Object right)
left
- left argumentright
- right argument
public boolean greaterThanOrEqual(Object left, Object right)
left
- left argumentright
- right argument
public boolean toBoolean(Object val)
Double.NaN, null, "false" and empty string coerce to false.
val
- value to coerce
public int toInteger(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 long toLong(Object val)
Double.NaN, null and empty string coerce to zero.
Boolean false is 0, true is 1.
val
- value to coerce
ArithmeticException
- 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 side
and(java.lang.Object, java.lang.Object)
@Deprecated public final Object bitwiseOr(Object lhs, Object rhs)
lhs
- left hand siderhs
- right hand side
or(java.lang.Object, java.lang.Object)
@Deprecated public final Object bitwiseXor(Object lhs, Object rhs)
lhs
- left hand siderhs
- right hand side
xor(java.lang.Object, java.lang.Object)
@Deprecated public final Object logicalNot(Object arg)
arg
- argument
not(java.lang.Object)
@Deprecated public final Object matches(Object lhs, Object rhs)
lhs
- left hand siderhs
- right hand side
contains(java.lang.Object, java.lang.Object)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |