public class JexlArithmetic extends Object
All arithmetic operators (+, - , *, /, %) follow the same rules regarding their arguments.
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.
|
protected MathContext |
mathContext
The big decimal math context.
|
protected int |
mathScale
The big decimal scale.
|
Constructor and Description |
---|
JexlArithmetic(boolean lenient)
Creates a JexlArithmetic.
|
JexlArithmetic(boolean lenient,
MathContext bigdContext,
int bigdScale)
Creates a JexlArithmetic.
|
Modifier and Type | Method and Description |
---|---|
Object |
add(Object left,
Object right)
Add two values together.
|
Object |
bitwiseAnd(Object left,
Object right)
Performs a bitwise and.
|
Object |
bitwiseComplement(Object val)
Performs a bitwise complement.
|
Object |
bitwiseOr(Object left,
Object right)
Performs a bitwise or.
|
Object |
bitwiseXor(Object left,
Object right)
Performs a bitwise xor.
|
protected int |
compare(Object left,
Object right,
String operator)
Performs a comparison.
|
protected Object |
controlNullNullOperands()
The result of +,/,-,*,% when both operands are null.
|
protected void |
controlNullOperand()
Throw a NPE if arithmetic is strict.
|
Object |
divide(Object left,
Object right)
Divide the left value by the 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 coercion operations.
|
boolean |
greaterThan(Object left,
Object right)
Test if left > right.
|
boolean |
greaterThanOrEqual(Object left,
Object right)
Test if left >= right.
|
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 |
isFloatingPointType(Object left,
Object right)
Test if either left or right are either a Float or Double.
|
boolean |
isLenient()
Checks whether this JexlArithmetic instance triggers errors during evaluation
when null is used as an operand.
|
protected boolean |
isNumberable(Object o)
Is Object a whole number.
|
boolean |
lessThan(Object left,
Object right)
Test if left < right.
|
boolean |
lessThanOrEqual(Object left,
Object right)
Test if left <= right.
|
boolean |
matches(Object left,
Object right)
Test if left regexp matches right.
|
Object |
mod(Object left,
Object right)
left value mod 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.
|
protected boolean |
narrowArguments(Object[] args)
Replace all numbers in an arguments array with the smallest type that will fit.
|
protected Object |
narrowArrayType(Object[] untyped)
Given an array of objects, attempt to type it more strictly.
|
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.
|
protected 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).
|
BigDecimal |
roundBigDecimal(BigDecimal number)
Ensure a big decimal is rounded by this arithmetic scale and rounding mode.
|
Object |
subtract(Object left,
Object right)
Subtract the right value from the left.
|
BigDecimal |
toBigDecimal(Object val)
Get a BigDecimal from the object passed.
|
BigInteger |
toBigInteger(Object val)
Get a BigInteger from the object passed.
|
boolean |
toBoolean(Object val)
Coerce to a boolean (not a java.lang.Boolean).
|
double |
toDouble(Object val)
Coerce to a double.
|
int |
toInteger(Object val)
Coerce to a int.
|
long |
toLong(Object val)
Coerce to a long (not a java.lang.Long).
|
String |
toString(Object val)
Coerce to a string.
|
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
protected final MathContext mathContext
protected final int mathScale
public JexlArithmetic(boolean lenient)
lenient
- whether this arithmetic is lenient or strictpublic JexlArithmetic(boolean lenient, MathContext bigdContext, int bigdScale)
lenient
- 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 boolean isLenient()
public MathContext getMathContext()
public int getMathScale()
public BigDecimal roundBigDecimal(BigDecimal number)
number
- the big decimal to roundprotected Object controlNullNullOperands()
ArithmeticException
- if strictprotected void controlNullOperand()
ArithmeticException
- if strictprotected boolean isFloatingPointType(Object left, Object right)
left
- one object to testright
- the otherprotected 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.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 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 narrowprotected Object narrowArrayType(Object[] untyped)
untyped
- an untyped arrayprotected 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
- first valueright
- second valuepublic Object divide(Object left, Object right)
left
- first valueright
- second valueArithmeticException
- if right == 0public Object mod(Object left, Object right)
left
- first valueright
- second valueArithmeticException
- if right == 0.0public Object multiply(Object left, Object right)
left
- first valueright
- second valuepublic Object subtract(Object left, Object right)
left
- first valueright
- second valuepublic Object negate(Object val)
val
- the value to negatepublic boolean matches(Object left, Object right)
left
- first valueright
- second valuepublic Object bitwiseAnd(Object left, Object right)
left
- the left operandright
- the right operatorpublic Object bitwiseOr(Object left, Object right)
left
- the left operandright
- the right operatorpublic Object bitwiseXor(Object left, Object right)
left
- the left operandright
- the right operatorpublic Object bitwiseComplement(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
- first valueright
- second valuepublic boolean lessThan(Object left, Object right)
left
- first valueright
- second valuepublic boolean greaterThan(Object left, Object right)
left
- first valueright
- second valuepublic boolean lessThanOrEqual(Object left, Object right)
left
- first valueright
- second valuepublic boolean greaterThanOrEqual(Object left, Object right)
left
- first valueright
- second valuepublic boolean toBoolean(Object val)
val
- Object to be coerced.public int toInteger(Object val)
val
- Object to be coerced.public long toLong(Object val)
val
- Object to be coerced.public BigInteger toBigInteger(Object val)
val
- the object to be coerced.NullPointerException
- if val is null and mode is strict.public BigDecimal toBigDecimal(Object val)
val
- the object to be coerced.NullPointerException
- if val is null and mode is strict.public double toDouble(Object val)
val
- Object to be coerced.NullPointerException
- if val is null and mode is strict.public String toString(Object val)
val
- Object to be coerced.NullPointerException
- if val is null and mode is strict.public Number narrow(Number original)
original
- the original number.protected boolean narrowAccept(Class<?> narrow, Class<?> source)
narrow
- the target narrow classsource
- the orginal source classprotected Number narrowNumber(Number original, Class<?> narrow)
original
- the original numbernarrow
- the attempted target classCopyright © 2016 Internet2. All rights reserved.