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 java.lang.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 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 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.

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

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

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 java.math.BigDecimal BIGD_DOUBLE_MAX_VALUE

  Double.MAX_VALUE as BigDecimal.

### BIGD_DOUBLE_MIN_VALUE

    protected static final java.math.BigDecimal BIGD_DOUBLE_MIN_VALUE

  Double.MIN_VALUE as BigDecimal.

### BIGI_LONG_MAX_VALUE

    protected static final java.math.BigInteger BIGI_LONG_MAX_VALUE

  Long.MAX_VALUE as BigInteger.

### BIGI_LONG_MIN_VALUE

    protected static final java.math.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 java.lang.Object controlNullNullOperands()

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

  Returns:
    Integer(0) if lenient
  Throws:
    `java.lang.NullPointerException` - if strict

### controlNullOperand

    protected void controlNullOperand()

  Throw a NPE if arithmetic is strict.

  Throws:
    `java.lang.NullPointerException` - if strict

### isFloatingPointType

    protected boolean isFloatingPointType(java.lang.Object left,
                                          java.lang.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(java.lang.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(java.lang.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(java.lang.Object o)

  Is Object a whole number.

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

### narrowBigInteger

    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.

  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 java.lang.Object narrowArrayType(java.lang.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(java.lang.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 java.lang.Object add(java.lang.Object left,
                                java.lang.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 java.lang.Object divide(java.lang.Object left,
                                   java.lang.Object right)

  Divide the left value by the right.

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

### mod

    public java.lang.Object mod(java.lang.Object left,
                                java.lang.Object right)

  left value mod right.

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

### multiply

    public java.lang.Object multiply(java.lang.Object left,
                                     java.lang.Object right)

  Multiply the left value by the right.

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

### subtract

    public java.lang.Object subtract(java.lang.Object left,
                                     java.lang.Object right)

  Subtract the right value from the left.

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

### matches

    public boolean matches(java.lang.Object left,
                           java.lang.Object right)

  Test if left regexp matches right.

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

### equals

    public boolean equals(java.lang.Object left,
                          java.lang.Object right)

  Test if left and right are equal.

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

### lessThan

    public boolean lessThan(java.lang.Object left,
                            java.lang.Object right)

  Test if left < right.

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

### greaterThan

    public boolean greaterThan(java.lang.Object left,
                               java.lang.Object right)

  Test if left > right.

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

### lessThanOrEqual

    public boolean lessThanOrEqual(java.lang.Object left,
                                   java.lang.Object right)

  Test if left <= right.

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

### greaterThanOrEqual

    public boolean greaterThanOrEqual(java.lang.Object left,
                                      java.lang.Object right)

  Test if left >= right.

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

### toBoolean

    public boolean toBoolean(java.lang.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(java.lang.Object val)

  Coerce to a int.

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

### toLong

    public long toLong(java.lang.Object val)

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

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

### toBigInteger

    public java.math.BigInteger toBigInteger(java.lang.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:
    `java.lang.NullPointerException` - if val is null and mode is strict.

### toBigDecimal

    public java.math.BigDecimal toBigDecimal(java.lang.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:
    `java.lang.NullPointerException` - if val is null and mode is strict.

### toDouble

    public double toDouble(java.lang.Object val)

  Coerce to a double.

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

### toString

    public java.lang.String toString(java.lang.Object val)

  Coerce to a string.

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

### narrow

    public java.lang.Number narrow(java.lang.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.