public class JexlArithmetic
extends java.lang.Object
All arithmetic operators (+, - , *, /, %) follow the same rules regarding their arguments.
Modifier and Type | Field and Description |
---|---|
protected static java.math.BigDecimal |
BIGD_DOUBLE_MAX_VALUE
Double.MAX_VALUE as BigDecimal.
|
protected static java.math.BigDecimal |
BIGD_DOUBLE_MIN_VALUE
Double.MIN_VALUE as BigDecimal.
|
protected static int |
BIGD_SCALE
Default BigDecimal scale.
|
protected static java.math.BigInteger |
BIGI_LONG_MAX_VALUE
Long.MAX_VALUE as BigInteger.
|
protected static java.math.BigInteger |
BIGI_LONG_MIN_VALUE
Long.MIN_VALUE as BigInteger.
|
protected java.math.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,
java.math.MathContext bigdContext,
int bigdScale)
Creates a JexlArithmetic.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
add(java.lang.Object left,
java.lang.Object right)
Add two values together.
|
java.lang.Object |
bitwiseAnd(java.lang.Object left,
java.lang.Object right)
Performs a bitwise and.
|
java.lang.Object |
bitwiseComplement(java.lang.Object val)
Performs a bitwise complement.
|
java.lang.Object |
bitwiseOr(java.lang.Object left,
java.lang.Object right)
Performs a bitwise or.
|
java.lang.Object |
bitwiseXor(java.lang.Object left,
java.lang.Object right)
Performs a bitwise xor.
|
protected int |
compare(java.lang.Object left,
java.lang.Object right,
java.lang.String operator)
Performs a comparison.
|
protected java.lang.Object |
controlNullNullOperands()
The result of +,/,-,*,% when both operands are null.
|
protected void |
controlNullOperand()
Throw a NPE if arithmetic is strict.
|
java.lang.Object |
divide(java.lang.Object left,
java.lang.Object right)
Divide the left value by the right.
|
boolean |
equals(java.lang.Object left,
java.lang.Object right)
Test if left and right are equal.
|
java.math.MathContext |
getMathContext()
The MathContext instance used for +,-,/,*,% operations on big decimals.
|
int |
getMathScale()
The BigDecimal scale used for comparison and coercion operations.
|
boolean |
greaterThan(java.lang.Object left,
java.lang.Object right)
Test if left > right.
|
boolean |
greaterThanOrEqual(java.lang.Object left,
java.lang.Object right)
Test if left >= right.
|
protected boolean |
isFloatingPoint(java.lang.Object o)
Is Object a floating point number.
|
protected boolean |
isFloatingPointNumber(java.lang.Object val)
Test if the passed value is a floating point number, i.e.
|
protected boolean |
isFloatingPointType(java.lang.Object left,
java.lang.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(java.lang.Object o)
Is Object a whole number.
|
boolean |
lessThan(java.lang.Object left,
java.lang.Object right)
Test if left < right.
|
boolean |
lessThanOrEqual(java.lang.Object left,
java.lang.Object right)
Test if left <= right.
|
boolean |
matches(java.lang.Object left,
java.lang.Object right)
Test if left regexp matches right.
|
java.lang.Object |
mod(java.lang.Object left,
java.lang.Object right)
left value mod right.
|
java.lang.Object |
multiply(java.lang.Object left,
java.lang.Object right)
Multiply the left value by the right.
|
java.lang.Number |
narrow(java.lang.Number original)
Given a Number, return back the value using the smallest type the result
will fit into.
|
protected boolean |
narrowAccept(java.lang.Class<?> narrow,
java.lang.Class<?> source)
Whether we consider the narrow class as a potential candidate for narrowing the source.
|
protected boolean |
narrowArguments(java.lang.Object[] args)
Replace all numbers in an arguments array with the smallest type that will fit.
|
protected java.lang.Object |
narrowArrayType(java.lang.Object[] untyped)
Given an array of objects, attempt to type it more strictly.
|
protected java.lang.Number |
narrowBigDecimal(java.lang.Object lhs,
java.lang.Object rhs,
java.math.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 java.lang.Number |
narrowBigInteger(java.lang.Object lhs,
java.lang.Object rhs,
java.math.BigInteger bigi)
Given a BigInteger, narrow it to an Integer or Long if it fits and the arguments
class allow it.
|
protected java.lang.Number |
narrowNumber(java.lang.Number original,
java.lang.Class<?> narrow)
Given a Number, return back the value attempting to narrow it to a target class.
|
java.lang.Object |
negate(java.lang.Object val)
Negates a value (unary minus for numbers).
|
java.math.BigDecimal |
roundBigDecimal(java.math.BigDecimal number)
Ensure a big decimal is rounded by this arithmetic scale and rounding mode.
|
java.lang.Object |
subtract(java.lang.Object left,
java.lang.Object right)
Subtract the right value from the left.
|
java.math.BigDecimal |
toBigDecimal(java.lang.Object val)
Get a BigDecimal from the object passed.
|
java.math.BigInteger |
toBigInteger(java.lang.Object val)
Get a BigInteger from the object passed.
|
boolean |
toBoolean(java.lang.Object val)
Coerce to a boolean (not a java.lang.Boolean).
|
double |
toDouble(java.lang.Object val)
Coerce to a double.
|
int |
toInteger(java.lang.Object val)
Coerce to a int.
|
long |
toLong(java.lang.Object val)
Coerce to a long (not a java.lang.Long).
|
java.lang.String |
toString(java.lang.Object val)
Coerce to a string.
|
protected static final java.math.BigDecimal BIGD_DOUBLE_MAX_VALUE
protected static final java.math.BigDecimal BIGD_DOUBLE_MIN_VALUE
protected static final java.math.BigInteger BIGI_LONG_MAX_VALUE
protected static final java.math.BigInteger BIGI_LONG_MIN_VALUE
protected static final int BIGD_SCALE
protected final java.math.MathContext mathContext
protected final int mathScale
public JexlArithmetic(boolean lenient)
lenient
- whether this arithmetic is lenient or strictpublic JexlArithmetic(boolean lenient, java.math.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 java.math.MathContext getMathContext()
public int getMathScale()
public java.math.BigDecimal roundBigDecimal(java.math.BigDecimal number)
number
- the big decimal to roundprotected java.lang.Object controlNullNullOperands()
java.lang.ArithmeticException
- if strictprotected void controlNullOperand()
java.lang.ArithmeticException
- if strictprotected boolean isFloatingPointType(java.lang.Object left, java.lang.Object right)
left
- one object to testright
- the otherprotected boolean isFloatingPointNumber(java.lang.Object val)
val
- the object to be testedprotected boolean isFloatingPoint(java.lang.Object o)
o
- Object to be analyzed.protected boolean isNumberable(java.lang.Object o)
o
- Object to be analyzed.protected java.lang.Number narrowBigInteger(java.lang.Object lhs, java.lang.Object rhs, java.math.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 java.lang.Number narrowBigDecimal(java.lang.Object lhs, java.lang.Object rhs, java.math.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 java.lang.Object narrowArrayType(java.lang.Object[] untyped)
untyped
- an untyped arrayprotected boolean narrowArguments(java.lang.Object[] args)
args
- the argument arraypublic java.lang.Object add(java.lang.Object left, java.lang.Object right)
If any numeric add fails on coercion to the appropriate type, treat as Strings and do concatenation.
left
- first valueright
- second valuepublic java.lang.Object divide(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuejava.lang.ArithmeticException
- if right == 0public java.lang.Object mod(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuejava.lang.ArithmeticException
- if right == 0.0public java.lang.Object multiply(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuepublic java.lang.Object subtract(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuepublic java.lang.Object negate(java.lang.Object val)
val
- the value to negatepublic boolean matches(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuepublic java.lang.Object bitwiseAnd(java.lang.Object left, java.lang.Object right)
left
- the left operandright
- the right operatorpublic java.lang.Object bitwiseOr(java.lang.Object left, java.lang.Object right)
left
- the left operandright
- the right operatorpublic java.lang.Object bitwiseXor(java.lang.Object left, java.lang.Object right)
left
- the left operandright
- the right operatorpublic java.lang.Object bitwiseComplement(java.lang.Object val)
val
- the operandprotected int compare(java.lang.Object left, java.lang.Object right, java.lang.String operator)
left
- the left operandright
- the right operatoroperator
- the operatorjava.lang.ArithmeticException
- if either left or right is nullpublic boolean equals(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuepublic boolean lessThan(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuepublic boolean greaterThan(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuepublic boolean lessThanOrEqual(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuepublic boolean greaterThanOrEqual(java.lang.Object left, java.lang.Object right)
left
- first valueright
- second valuepublic boolean toBoolean(java.lang.Object val)
val
- Object to be coerced.public int toInteger(java.lang.Object val)
val
- Object to be coerced.public long toLong(java.lang.Object val)
val
- Object to be coerced.public java.math.BigInteger toBigInteger(java.lang.Object val)
val
- the object to be coerced.java.lang.NullPointerException
- if val is null and mode is strict.public java.math.BigDecimal toBigDecimal(java.lang.Object val)
val
- the object to be coerced.java.lang.NullPointerException
- if val is null and mode is strict.public double toDouble(java.lang.Object val)
val
- Object to be coerced.java.lang.NullPointerException
- if val is null and mode is strict.public java.lang.String toString(java.lang.Object val)
val
- Object to be coerced.java.lang.NullPointerException
- if val is null and mode is strict.public java.lang.Number narrow(java.lang.Number original)
original
- the original number.protected boolean narrowAccept(java.lang.Class<?> narrow, java.lang.Class<?> source)
narrow
- the target narrow classsource
- the orginal source classprotected java.lang.Number narrowNumber(java.lang.Number original, java.lang.Class<?> narrow)
original
- the original numbernarrow
- the attempted target classCopyright © 2001-2013 The Apache Software Foundation. All Rights Reserved.