Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.commons.jexl2
Class JexlArithmetic

    java.lang.Object
      org.apache.commons.jexl2.JexlArithmetic

    public class JexlArithmetic

extends Object

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 floating point number, coerce both to Double and perform operation
3. If both are BigInteger, treat as BigInteger and perform operation
4. If either is a BigDecimal, coerce both to BigDecimal and 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

Since:
  2.0

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 BigInteger` | `BIGI_LONG_MAX_VALUE` Long.MAX_VALUE as BigInteger.
`protected static BigInteger` | `BIGI_LONG_MIN_VALUE` Long.MIN_VALUE as BigInteger.

Constructor Summary
`JexlArithmetic(boolean lenient)` Creates a JexlArithmetic.

Method Summary
`Object` | `add(Object left, Object right)` Add two values together.
`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.
`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` | `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` | `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.
`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.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### BIGD_DOUBLE_MAX_VALUE

    protected static final BigDecimal BIGD_DOUBLE_MAX_VALUE

  Double.MAX_VALUE as BigDecimal.

### BIGD_DOUBLE_MIN_VALUE

    protected static final BigDecimal BIGD_DOUBLE_MIN_VALUE

  Double.MIN_VALUE as BigDecimal.

### BIGI_LONG_MAX_VALUE

    protected static final BigInteger BIGI_LONG_MAX_VALUE

  Long.MAX_VALUE as BigInteger.

### BIGI_LONG_MIN_VALUE

    protected static final BigInteger BIGI_LONG_MIN_VALUE

  Long.MIN_VALUE as BigInteger.

Constructor Detail

### JexlArithmetic

    public JexlArithmetic(boolean lenient)

  Creates a JexlArithmetic.

  Parameters:
    `lenient` - whether this arithmetic is lenient or strict

Method Detail

### isLenient

    public boolean isLenient()

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

  Returns:
    true if lenient, false if strict

### controlNullNullOperands

    protected Object controlNullNullOperands()

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

  Returns:
    null if strict, else Long(0)

### controlNullOperand

    protected void controlNullOperand()

  Throw a NPE if arithmetic is strict.

### isFloatingPointType

    protected boolean isFloatingPointType(Object left,
                                          Object right)

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

  Parameters:
    `left` - one object to test
    `right` - the other
  Returns:
    the result of the test.

### isFloatingPointNumber

    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` - the object to be tested
  Returns:
    true if it is, false otherwise.

### isFloatingPoint

    protected boolean isFloatingPoint(Object o)

  Is Object a floating point number.

  Parameters:
    `o` - Object to be analyzed.
  Returns:
    true if it is a Float or a Double.

### isNumberable

    protected boolean isNumberable(Object o)

  Is Object a whole number.

  Parameters:
    `o` - Object to be analyzed.
  Returns:
    true if Integer, Long, Byte, Short or Character.

### narrowBigInteger

    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` - the left hand side operand that lead to the bigi result
    `rhs` - the right hand side operand that lead to the bigi result
    `bigi` - the BigInteger to narrow
  Returns:
    an Integer or Long if narrowing is possible, the original BigInteger otherwise

### narrowArrayType

    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` - an untyped array
  Returns:
    the original array if the attempt to strictly type the array fails, a typed array otherwise

### narrowArguments

    protected boolean narrowArguments(Object[] args)

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

  Parameters:
    `args` - the argument array
  Returns:
    true if some arguments were narrowed and args array is modified, false if no narrowing occured and args array has not been modified

### add

    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` - first value
    `right` - second value
  Returns:
    left + right.

### divide

    public Object divide(Object left,
                         Object right)

  Divide the left value by the right.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    left / right
  Throws:
    `ArithmeticException` - if right == 0

### mod

    public Object mod(Object left,
                      Object right)

  left value mod right.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    left mod right
  Throws:
    `ArithmeticException` - if right == 0.0

### multiply

    public Object multiply(Object left,
                           Object right)

  Multiply the left value by the right.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    left * right.

### subtract

    public Object subtract(Object left,
                           Object right)

  Subtract the right value from the left.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    left - right.

### matches

    public boolean matches(Object left,
                           Object right)

  Test if left regexp matches right.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    test result.

### equals

    public boolean equals(Object left,
                          Object right)

  Test if left and right are equal.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    test result.

### lessThan

    public boolean lessThan(Object left,
                            Object right)

  Test if left < right.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    test result.

### greaterThan

    public boolean greaterThan(Object left,
                               Object right)

  Test if left > right.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    test result.

### lessThanOrEqual

    public boolean lessThanOrEqual(Object left,
                                   Object right)

  Test if left <= right.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    test result.

### greaterThanOrEqual

    public boolean greaterThanOrEqual(Object left,
                                      Object right)

  Test if left >= right.

  Parameters:
    `left` - first value
    `right` - second value
  Returns:
    test result.

### toBoolean

    public boolean toBoolean(Object val)

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

  Parameters:
    `val` - Object to be coerced.
  Returns:
    The boolean coerced value, or false if none possible.

### toInteger

    public int toInteger(Object val)

  Coerce to a int.

  Parameters:
    `val` - Object to be coerced.
  Returns:
    The int coerced value.

### toLong

    public long toLong(Object val)

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

  Parameters:
    `val` - Object to be coerced.
  Returns:
    The long coerced value.

### toBigInteger

    public BigInteger toBigInteger(Object val)

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

  Parameters:
    `val` - the object to be coerced.
  Returns:
    a BigDecimal.
  Throws:
    `NullPointerException` - if val is null and mode is strict.

### toBigDecimal

    public BigDecimal toBigDecimal(Object val)

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

  Parameters:
    `val` - the object to be coerced.
  Returns:
    a BigDecimal.
  Throws:
    `NullPointerException` - if val is null and mode is strict.

### toDouble

    public double toDouble(Object val)

  Coerce to a double.

  Parameters:
    `val` - Object to be coerced.
  Returns:
    The double coerced value.
  Throws:
    `NullPointerException` - if val is null and mode is strict.

### toString

    public String toString(Object val)

  Coerce to a string.

  Parameters:
    `val` - Object to be coerced.
  Returns:
    The String coerced value.
  Throws:
    `NullPointerException` - if val is null and mode is strict.

### narrow

    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` - the original number.
  Returns:
    a value of the smallest type the original number will fit into.

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.