Top Description Fields Constructors Methods
org.apache.commons.jexl2

public Class JexlArithmetic

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.commons.jexl2.JexlThreadedArithmetic
Imports
java.lang.reflect.Array, .Field, java.math.BigDecimal, .BigInteger, .MathContext

Perform arithmetic.

All arithmetic operators (+, - , *, /, %) follow the same rules regarding their arguments.

  1. If both are null, result is 0
  2. If either is a BigDecimal, coerce both to BigDecimal and and perform operation
  3. If either is a floating point number, coerce both to Double and perform operation
  4. If both are BigInteger, treat as BigInteger and perform operation
  5. Else treat as BigInteger, perform operation and attempt to narrow result:
    1. if both arguments can be narrowed to Integer, narrow result to Integer
    2. if both arguments can be narrowed to Long, narrow result to Long
    3. Else return result as BigInteger

Note that the only exception throw by JexlArithmetic is ArithmeticException.
Since
2.0

Field Summary

Modifier and TypeField and Description
protected static final BigDecimal
BIGD_DOUBLE_MAX_VALUE

Double.MAX_VALUE as BigDecimal.

protected static final BigDecimal
BIGD_DOUBLE_MIN_VALUE

Double.MIN_VALUE as BigDecimal.

protected static final int
BIGD_SCALE

Default BigDecimal scale.

protected static final BigInteger
BIGI_LONG_MAX_VALUE

Long.MAX_VALUE as BigInteger.

protected static final BigInteger
BIGI_LONG_MIN_VALUE

Long.MIN_VALUE as BigInteger.

protected final MathContext
mathContext

The big decimal math context.

protected final int
mathScale

The big decimal scale.

private volatile boolean
strict

Whether this JexlArithmetic instance behaves in strict or lenient mode.

Constructor Summary

AccessConstructor and Description
public
JexlArithmetic(boolean
whether this arithmetic is lenient or strict
lenient
)

Creates a JexlArithmetic.

public
JexlArithmetic(boolean
whether this arithmetic is lenient or strict
lenient
,
MathContext
the math context instance to use for +,-,/,*,% operations on big decimals.
bigdContext
,
int
the scale used for big decimals.
bigdScale
)

Creates a JexlArithmetic.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

left + right.
add
(Object
first value
left
,
Object
second value
right
)

Add two values together.

public Object

Returns:

left & right
bitwiseAnd
(Object
the left operand
left
,
Object
the right operator
right
)

Performs a bitwise and.

public Object

Returns:

~val
bitwiseComplement
(Object
the operand
val
)

Performs a bitwise complement.

public Object

Returns:

left | right
bitwiseOr
(Object
the left operand
left
,
Object
the right operator
right
)

Performs a bitwise or.

public Object

Returns:

left right
bitwiseXor
(Object
the left operand
left
,
Object
the right operator
right
)

Performs a bitwise xor.

protected int

Returns:

-1 if left < right; +1 if left > > right; 0 if left == right
compare
(Object
the left operand
left
,
Object
the right operator
right
,
String
the operator
operator
)

Performs a comparison.

protected Object

Returns:

Integer(0) if lenient
controlNullNullOperands
()

The result of +,/,-,*,% when both operands are null.

protected void
controlNullOperand()

Throw a NPE if arithmetic is strict.

public Object

Returns:

left / right
divide
(Object
first value
left
,
Object
second value
right
)

Divide the left value by the right.

public boolean

Returns:

test result.
equals
(Object
first value
left
,
Object
second value
right
)

Test if left and right are equal.

public MathContext

Returns:

the math context
getMathContext
()

The MathContext instance used for +,-,/,*,% operations on big decimals.

public int

Returns:

the scale
getMathScale
()

The BigDecimal scale used for comparison and coercion operations.

public boolean

Returns:

test result.
greaterThan
(Object
first value
left
,
Object
second value
right
)

Test if left > right.

public boolean

Returns:

test result.
greaterThanOrEqual
(Object
first value
left
,
Object
second value
right
)

Test if left >= right.

protected boolean

Returns:

true if it is a Float or a Double.
isFloatingPoint
(final Object
Object to be analyzed.
o
)

Is Object a floating point number.

protected boolean

Returns:

true if it is, false otherwise.
isFloatingPointNumber
(Object
the object to be tested
val
)

Test if the passed value is a floating point number, i.e. a float, double or string with ( "." | "E" | "e").

protected boolean

Returns:

the result of the test.
isFloatingPointType
(Object
one object to test
left
,
Object
the other
right
)

Test if either left or right are either a Float or Double.

public boolean

Returns:

true if lenient, false if strict
isLenient
()

Checks whether this JexlArithmetic instance triggers errors during evaluation when null is used as an operand.

protected boolean

Returns:

true if Integer, Long, Byte, Short or Character.
isNumberable
(final Object
Object to be analyzed.
o
)

Is Object a whole number.

public boolean

Returns:

test result.
lessThan
(Object
first value
left
,
Object
second value
right
)

Test if left < right.

public boolean

Returns:

test result.
lessThanOrEqual
(Object
first value
left
,
Object
second value
right
)

Test if left <= right.

public boolean

Returns:

test result.
matches
(Object
first value
left
,
Object
second value
right
)

Test if left regexp matches right.

public Object

Returns:

left mod right
mod
(Object
first value
left
,
Object
second value
right
)

left value mod right.

public Object

Returns:

left * right.
multiply
(Object
first value
left
,
Object
second value
right
)

Multiply the left value by the right.

public Number

Returns:

a value of the smallest type the original number will fit into.
narrow
(Number
the original number.
original
)

Given a Number, return back the value using the smallest type the result will fit into.

protected boolean

Returns:

true if attempt to narrow source to target is accepted
narrowAccept
(Class<?>
the target narrow class
narrow
,
Class<?>
the orginal source class
source
)

Whether we consider the narrow class as a potential candidate for narrowing the source.

protected boolean

Returns:

true if some arguments were narrowed and args array is modified, false if no narrowing occured and args array has not been modified
narrowArguments
(Object[]
the argument array
args
)

Replace all numbers in an arguments array with the smallest type that will fit.

protected Object

Returns:

the original array if the attempt to strictly type the array fails, a typed array otherwise
narrowArrayType
(Object[]
an untyped array
untyped
)

Given an array of objects, attempt to type it more strictly.

protected Number

Returns:

an Integer or Long if narrowing is possible, the original BigInteger otherwise
narrowBigDecimal
(Object
the left hand side operand that lead to the bigd result
lhs
,
Object
the right hand side operand that lead to the bigd result
rhs
,
BigDecimal
the BigDecimal to narrow
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

Returns:

an Integer or Long if narrowing is possible, the original BigInteger otherwise
narrowBigInteger
(Object
the left hand side operand that lead to the bigi result
lhs
,
Object
the right hand side operand that lead to the bigi result
rhs
,
BigInteger
the BigInteger to narrow
bigi
)

Given a BigInteger, narrow it to an Integer or Long if it fits and the arguments class allow it.

protected Number

Returns:

the narrowed number or the source if no narrowing was possible
narrowNumber
(Number
the original number
original
,
Class<?>
the attempted target class
narrow
)

Given a Number, return back the value attempting to narrow it to a target class.

public Object

Returns:

the negated value
negate
(Object
the value to negate
val
)

Negates a value (unary minus for numbers).

public BigDecimal

Returns:

the rounded big decimal
roundBigDecimal
(final BigDecimal
the big decimal to round
number
)

Ensure a big decimal is rounded by this arithmetic scale and rounding mode.

pack-priv void
setLenient(boolean
true means no JexlException will occur, false allows them
flag
)

Deprecated as of 2.1 - may be removed in a later release
Sets whether this JexlArithmetic instance triggers errors during evaluation when null is used as an operand.
public Object

Returns:

left - right.
subtract
(Object
first value
left
,
Object
second value
right
)

Subtract the right value from the left.

public BigDecimal

Returns:

a BigDecimal.
toBigDecimal
(Object
the object to be coerced.
val
)

Get a BigDecimal from the object passed.

public BigInteger

Returns:

a BigDecimal.
toBigInteger
(Object
the object to be coerced.
val
)

Get a BigInteger from the object passed.

public boolean

Returns:

The boolean coerced value, or false if none possible.
toBoolean
(Object
Object to be coerced.
val
)

Coerce to a boolean (not a java.lang.Boolean).

public double

Returns:

The double coerced value.
toDouble
(Object
Object to be coerced.
val
)

Coerce to a double.

public int

Returns:

The int coerced value.
toInteger
(Object
Object to be coerced.
val
)

Coerce to a int.

public long

Returns:

The long coerced value.
toLong
(Object
Object to be coerced.
val
)

Coerce to a long (not a java.lang.Long).

public String

Returns:

The String coerced value.
toString
(Object
Object to be coerced.
val
)

Coerce to a string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BIGD_DOUBLE_MAX_VALUEback to summary
protected static final BigDecimal BIGD_DOUBLE_MAX_VALUE

Double.MAX_VALUE as BigDecimal.

BIGD_DOUBLE_MIN_VALUEback to summary
protected static final BigDecimal BIGD_DOUBLE_MIN_VALUE

Double.MIN_VALUE as BigDecimal.

BIGD_SCALEback to summary
protected static final int BIGD_SCALE

Default BigDecimal scale.

Since
2.1
BIGI_LONG_MAX_VALUEback to summary
protected static final BigInteger BIGI_LONG_MAX_VALUE

Long.MAX_VALUE as BigInteger.

BIGI_LONG_MIN_VALUEback to summary
protected static final BigInteger BIGI_LONG_MIN_VALUE

Long.MIN_VALUE as BigInteger.

mathContextback to summary
protected final MathContext mathContext

The big decimal math context.

Since
2.1
mathScaleback to summary
protected final int mathScale

The big decimal scale.

Since
2.1
strictback to summary
private volatile boolean strict

Whether this JexlArithmetic instance behaves in strict or lenient mode. May be made final in a later version.

Constructor Detail

JexlArithmeticback to summary
public JexlArithmetic(boolean lenient)

Creates a JexlArithmetic.

Parameters
lenient:boolean

whether this arithmetic is lenient or strict

JexlArithmeticback to summary
public JexlArithmetic(boolean lenient, MathContext bigdContext, int bigdScale)

Creates a JexlArithmetic.

Parameters
lenient:boolean

whether this arithmetic is lenient or strict

bigdContext:MathContext

the math context instance to use for +,-,/,*,% operations on big decimals.

bigdScale:int

the scale used for big decimals.

Since
2.1

Method Detail

addback to summary
public Object add(Object left, Object right)

Add two values together.

If any numeric add fails on coercion to the appropriate type, treat as Strings and do concatenation.

Parameters
left:Object

first value

right:Object

second value

Returns:Object

left + right.

bitwiseAndback to summary
public Object bitwiseAnd(Object left, Object right)

Performs a bitwise and.

Parameters
left:Object

the left operand

right:Object

the right operator

Returns:Object

left & right

Since
2.1
bitwiseComplementback to summary
public Object bitwiseComplement(Object val)

Performs a bitwise complement.

Parameters
val:Object

the operand

Returns:Object

~val

Since
2.1
bitwiseOrback to summary
public Object bitwiseOr(Object left, Object right)

Performs a bitwise or.

Parameters
left:Object

the left operand

right:Object

the right operator

Returns:Object

left | right

Since
2.1
bitwiseXorback to summary
public Object bitwiseXor(Object left, Object right)

Performs a bitwise xor.

Parameters
left:Object

the left operand

right:Object

the right operator

Returns:Object

left right

Since
2.1
compareback to summary
protected int compare(Object left, Object right, String operator)

Performs a comparison.

Parameters
left:Object

the left operand

right:Object

the right operator

operator:String

the operator

Returns:int

-1 if left < right; +1 if left > > right; 0 if left == right

Exceptions
ArithmeticException:
if either left or right is null
Since
2.1
controlNullNullOperandsback to summary
protected Object controlNullNullOperands()

The result of +,/,-,*,% when both operands are null.

Returns:Object

Integer(0) if lenient

Exceptions
ArithmeticException:
if strict
controlNullOperandback to summary
protected void controlNullOperand()

Throw a NPE if arithmetic is strict.

Exceptions
ArithmeticException:
if strict
divideback to summary
public Object divide(Object left, Object right)

Divide the left value by the right.

Parameters
left:Object

first value

right:Object

second value

Returns:Object

left / right

Exceptions
ArithmeticException:
if right == 0
equalsback to summary
public boolean equals(Object left, Object right)

Test if left and right are equal.

Parameters
left:Object

first value

right:Object

second value

Returns:boolean

test result.

getMathContextback to summary
public MathContext getMathContext()

The MathContext instance used for +,-,/,*,% operations on big decimals.

Returns:MathContext

the math context

Since
2.1
getMathScaleback to summary
public int getMathScale()

The BigDecimal scale used for comparison and coercion operations.

Returns:int

the scale

Since
2.1
greaterThanback to summary
public boolean greaterThan(Object left, Object right)

Test if left > right.

Parameters
left:Object

first value

right:Object

second value

Returns:boolean

test result.

greaterThanOrEqualback to summary
public boolean greaterThanOrEqual(Object left, Object right)

Test if left >= right.

Parameters
left:Object

first value

right:Object

second value

Returns:boolean

test result.

isFloatingPointback to summary
protected boolean isFloatingPoint(final Object o)

Is Object a floating point number.

Parameters
o:Object

Object to be analyzed.

Returns:boolean

true if it is a Float or a Double.

isFloatingPointNumberback to summary
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").

Parameters
val:Object

the object to be tested

Returns:boolean

true if it is, false otherwise.

isFloatingPointTypeback to summary
protected boolean isFloatingPointType(Object left, Object right)

Test if either left or right are either a Float or Double.

Parameters
left:Object

one object to test

right:Object

the other

Returns:boolean

the result of the test.

isLenientback to summary
public boolean isLenient()

Checks whether this JexlArithmetic instance triggers errors during evaluation when null is used as an operand.

Returns:boolean

true if lenient, false if strict

isNumberableback to summary
protected boolean isNumberable(final Object o)

Is Object a whole number.

Parameters
o:Object

Object to be analyzed.

Returns:boolean

true if Integer, Long, Byte, Short or Character.

lessThanback to summary
public boolean lessThan(Object left, Object right)

Test if left < right.

Parameters
left:Object

first value

right:Object

second value

Returns:boolean

test result.

lessThanOrEqualback to summary
public boolean lessThanOrEqual(Object left, Object right)

Test if left <= right.

Parameters
left:Object

first value

right:Object

second value

Returns:boolean

test result.

matchesback to summary
public boolean matches(Object left, Object right)

Test if left regexp matches right.

Parameters
left:Object

first value

right:Object

second value

Returns:boolean

test result.

Since
2.1
modback to summary
public Object mod(Object left, Object right)

left value mod right.

Parameters
left:Object

first value

right:Object

second value

Returns:Object

left mod right

Exceptions
ArithmeticException:
if right == 0.0
multiplyback to summary
public Object multiply(Object left, Object right)

Multiply the left value by the right.

Parameters
left:Object

first value

right:Object

second value

Returns:Object

left * right.

narrowback to summary
public Number narrow(Number original)

Given a Number, return back the value using the smallest type the result will fit into. 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.

Parameters
original:Number

the original number.

Returns:Number

a value of the smallest type the original number will fit into.

narrowAcceptback to summary
protected boolean narrowAccept(Class<?> narrow, Class<?> source)

Whether we consider the narrow class as a potential candidate for narrowing the source.

Parameters
narrow:Class<?>

the target narrow class

source:Class<?>

the orginal source class

Returns:boolean

true if attempt to narrow source to target is accepted

Since
2.1
narrowArgumentsback to summary
protected boolean narrowArguments(Object[] args)

Replace all numbers in an arguments array with the smallest type that will fit.

Parameters
args:Object[]

the argument array

Returns:boolean

true if some arguments were narrowed and args array is modified, false if no narrowing occured and args array has not been modified

narrowArrayTypeback to summary
protected Object narrowArrayType(Object[] untyped)

Given an array of objects, attempt to type it more strictly.

  • If all objects are of the same type, the array returned will be an array of that same type
  • If all objects are Numbers, the array returned will be an array of Numbers
  • If all objects are convertible to a primitive type, the array returned will be an array of the primitive type
Parameters
untyped:Object[]

an untyped array

Returns:Object

the original array if the attempt to strictly type the array fails, a typed array otherwise

narrowBigDecimalback to summary
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.

Parameters
lhs:Object

the left hand side operand that lead to the bigd result

rhs:Object

the right hand side operand that lead to the bigd result

bigd:BigDecimal

the BigDecimal to narrow

Returns:Number

an Integer or Long if narrowing is possible, the original BigInteger otherwise

Since
2.1
narrowBigIntegerback to summary
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.

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

Parameters
lhs:Object

the left hand side operand that lead to the bigi result

rhs:Object

the right hand side operand that lead to the bigi result

bigi:BigInteger

the BigInteger to narrow

Returns:Number

an Integer or Long if narrowing is possible, the original BigInteger otherwise

narrowNumberback to summary
protected Number narrowNumber(Number original, Class<?> narrow)

Given a Number, return back the value attempting to narrow it to a target class.

Parameters
original:Number

the original number

narrow:Class<?>

the attempted target class

Returns:Number

the narrowed number or the source if no narrowing was possible

Since
2.1
negateback to summary
public Object negate(Object val)

Negates a value (unary minus for numbers).

Parameters
val:Object

the value to negate

Returns:Object

the negated value

Since
2.1
roundBigDecimalback to summary
public BigDecimal roundBigDecimal(final BigDecimal number)

Ensure a big decimal is rounded by this arithmetic scale and rounding mode.

Parameters
number:BigDecimal

the big decimal to round

Returns:BigDecimal

the rounded big decimal

Since
2.1
setLenientback to summary
pack-priv void setLenient(boolean flag)

Deprecated

as of 2.1 - may be removed in a later release

Sets whether this JexlArithmetic instance triggers errors during evaluation when null is used as an operand.

This method is not thread safe; it may be called as an optional step by the JexlEngine in its initialization code before expression creation & evaluation.

Parameters
flag:boolean

true means no JexlException will occur, false allows them

Annotations
@Deprecated
See Also
JexlEngine#setLenient, JexlEngine#setSilent, JexlEngine#setDebug
subtractback to summary
public Object subtract(Object left, Object right)

Subtract the right value from the left.

Parameters
left:Object

first value

right:Object

second value

Returns:Object

left - right.

toBigDecimalback to summary
public BigDecimal toBigDecimal(Object val)

Get a BigDecimal from the object passed. Null and empty string maps to zero.

Parameters
val:Object

the object to be coerced.

Returns:BigDecimal

a BigDecimal.

Exceptions
NullPointerException:
if val is null and mode is strict.
toBigIntegerback to summary
public BigInteger toBigInteger(Object val)

Get a BigInteger from the object passed. Null and empty string maps to zero.

Parameters
val:Object

the object to be coerced.

Returns:BigInteger

a BigDecimal.

Exceptions
NullPointerException:
if val is null and mode is strict.
toBooleanback to summary
public boolean toBoolean(Object val)

Coerce to a boolean (not a java.lang.Boolean).

Parameters
val:Object

Object to be coerced.

Returns:boolean

The boolean coerced value, or false if none possible.

toDoubleback to summary
public double toDouble(Object val)

Coerce to a double.

Parameters
val:Object

Object to be coerced.

Returns:double

The double coerced value.

Exceptions
NullPointerException:
if val is null and mode is strict.
toIntegerback to summary
public int toInteger(Object val)

Coerce to a int.

Parameters
val:Object

Object to be coerced.

Returns:int

The int coerced value.

toLongback to summary
public long toLong(Object val)

Coerce to a long (not a java.lang.Long).

Parameters
val:Object

Object to be coerced.

Returns:long

The long coerced value.

toStringback to summary
public String toString(Object val)

Coerce to a string.

Parameters
val:Object

Object to be coerced.

Returns:String

The String coerced value.

Exceptions
NullPointerException:
if val is null and mode is strict.