public abstract class NumberMath
extends Object
Stateless objects used to perform math on the various Number subclasses. Instances are required so that polymorphic calls work properly, but each subclass creates a singleton instance to minimize garbage. All methods must be thread-safe. The design goals of this class are as follows:
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static Number |
abs(Number number)Computes the absolute value of a number using its specific type's math operations. |
|
protected abstract Number |
absImpl(Number number)Computes the absolute value of a number. |
|
public static Number |
add(Number left, Number right)Adds two numbers using type promotion rules. |
|
public abstract Number |
addImpl(Number left, Number right)Adds two numbers. |
|
public static Number |
and(Number left, Number right)Bitwise AND operation on two integral numbers. |
|
protected Number |
andImpl(Number left, Number right)Bitwise AND of two numbers. |
|
public static Number |
bitwiseNegate(Number left)Bitwise negation (complement) of an integral number. |
|
protected Number |
bitwiseNegateImpl(Number left)Bitwise negation of a number. |
|
public static int |
compareTo(Number left, Number right)Compares two numbers using type promotion rules. |
|
public abstract int |
compareToImpl(Number left, Number right)Compares two numbers. |
|
protected UnsupportedOperationException |
createUnsupportedException(String operation, Number left)Creates an UnsupportedOperationException for an unsupported operation. |
|
public static Number |
divide(Number left, Number right)Divides two numbers using type promotion rules. |
|
public abstract Number |
divideImpl(Number left, Number right)Divides two numbers. |
|
public static NumberMath |
getMath(Number left, Number right)Determines which NumberMath instance to use for binary operations on two operands. |
|
public static Number |
intdiv(Number left, Number right)Integer division (without remainder) of two numbers. |
|
protected Number |
intdivImpl(Number left, Number right)Integer division of two numbers. |
|
public static boolean |
isBigDecimal(Number number)Tests whether a number is a BigDecimal. |
|
public static boolean |
isBigInteger(Number number)Tests whether a number is a BigInteger. |
|
public static boolean |
isByte(Number number)Tests whether a number is a Byte. |
|
public static boolean |
isFloatingPoint(Number number)Tests whether a number is a floating-point type (Float or Double). |
|
public static boolean |
isInteger(Number number)Tests whether a number is an Integer. |
|
public static boolean |
isLong(Number number)Tests whether a number is a Long. |
|
public static boolean |
isShort(Number number)Tests whether a number is a Short. |
|
public static Number |
leftShift(Number left, Number right)Left shift operation on integral numbers. |
|
protected Number |
leftShiftImpl(Number left, Number right)Left shift of a number. |
|
public static Number |
mod(Number left, Number right)Modulo operation on two numbers (for backwards compatibility). |
|
protected Number |
modImpl(Number left, Number right)Modulo of two numbers. |
|
public static Number |
multiply(Number left, Number right)Multiplies two numbers using type promotion rules. |
|
public abstract Number |
multiplyImpl(Number left, Number right)Multiplies two numbers. |
|
public static Number |
or(Number left, Number right)Bitwise OR operation on two integral numbers. |
|
protected Number |
orImpl(Number left, Number right)Bitwise OR of two numbers. |
|
public static Number |
remainder(Number left, Number right)Remainder operation on two numbers. |
|
protected Number |
remainderImpl(Number left, Number right)Remainder of two numbers. |
|
public static Number |
rightShift(Number left, Number right)Right shift operation on integral numbers. |
|
protected Number |
rightShiftImpl(Number left, Number right)Right shift of a number. |
|
public static Number |
rightShiftUnsigned(Number left, Number right)Unsigned right shift operation on integral numbers. |
|
protected Number |
rightShiftUnsignedImpl(Number left, Number right)Unsigned right shift of a number. |
|
public static Number |
subtract(Number left, Number right)Subtracts two numbers using type promotion rules. |
|
public abstract Number |
subtractImpl(Number left, Number right)Subtracts two numbers. |
|
public static BigDecimal |
toBigDecimal(Number n)Converts a number to a BigDecimal. |
|
public static BigInteger |
toBigInteger(Number n)Converts a number to a BigInteger. |
|
public static Number |
unaryMinus(Number left)Unary negation of a number. |
|
protected abstract Number |
unaryMinusImpl(Number left)Negates a number. |
|
public static Number |
unaryPlus(Number left)Unary plus (identity) operation on a number. |
|
protected abstract Number |
unaryPlusImpl(Number left)Returns the number as-is (unary plus/identity). |
|
public static Number |
xor(Number left, Number right)Bitwise XOR operation on two integral numbers. |
|
protected Number |
xorImpl(Number left, Number right)Bitwise XOR of two numbers. |
Computes the absolute value of a number using its specific type's math operations.
number - the number (must not be null)Computes the absolute value of a number.
Subclasses must implement this method according to type promotion hierarchy rules.
number - the operandAdds two numbers using type promotion rules.
left - the left operandright - the right operandAdds two numbers.
Subclasses must implement this method according to type promotion hierarchy rules.
left - the left operandright - the right operandBitwise AND operation on two integral numbers.
left - the first operandright - the second operandBitwise AND of two numbers.
Default implementation throws UnsupportedOperationException. Subclasses supporting bitwise operations must override.
left - the first operandright - the second operandBitwise negation (complement) of an integral number.
left - the operand (must be integral)Bitwise negation of a number.
Default implementation throws UnsupportedOperationException. Subclasses supporting bitwise operations must override.
left - the operandCompares two numbers using type promotion rules.
left - the first numberright - the second numberCompares two numbers.
Subclasses must implement this method according to type promotion hierarchy rules.
left - the first numberright - the second numberCreates an UnsupportedOperationException for an unsupported operation.
operation - the name of the unsupported operationleft - the operandDivides two numbers using type promotion rules.
For floating-point operands, performs binary floating-point division. For integral types, delegates to BigDecimal division for exact results.
left - the dividendright - the divisorDivides two numbers.
Subclasses must implement this method according to type promotion hierarchy rules.
left - the dividendright - the divisorDetermines which NumberMath instance to use for binary operations on two operands.
Implements the type promotion matrix:
bD bI D F L I
bD bD bD D D bD bD
bI bD bI D D bI bI
D D D D D D D
F D D D D D D
L bD bI D D L L
I bD bI D D L I
Where: bD=BigDecimal, bI=BigInteger, D=Double, F=Float, L=Long, I=Integer
Note: Byte, Character, and Short operands are pre-promoted to Integer. For division, if either operand is floating-point, the result is floating-point. Otherwise, the result is BigDecimal.
left - the left operandright - the right operandInteger division (without remainder) of two numbers.
For BigInteger and integral types, performs exact integer division. For floating-point operands, delegates to FloatingPointMath.
left - the dividendright - the divisorInteger division of two numbers.
Default implementation throws UnsupportedOperationException. Subclasses supporting integer division must override.
left - the dividendright - the divisorTests whether a number is a BigDecimal.
number - the number to testTests whether a number is a BigInteger.
number - the number to testTests whether a number is a Byte.
number - the number to testTests whether a number is a floating-point type (Float or Double).
number - the number to testTests whether a number is an Integer.
number - the number to testTests whether a number is a Long.
number - the number to testTests whether a number is a Short.
number - the number to testLeft shift operation on integral numbers.
The shift distance (right operand) must be an integral type. The value to shift (left operand) must also be integral.
left - the value to shift (must be integral)right - the shift distance (must be integral)Left shift of a number.
Default implementation throws UnsupportedOperationException. Subclasses supporting bit shifting must override.
left - the value to shiftright - the shift distanceModulo operation on two numbers (for backwards compatibility).
This method is retained for backwards compatibility
left - the dividendright - the divisorModulo of two numbers.
Default implementation throws UnsupportedOperationException. Subclasses supporting modulo must override.
left - the dividendright - the divisorMultiplies two numbers using type promotion rules.
left - the first multiplicandright - the second multiplicandMultiplies two numbers.
Subclasses must implement this method according to type promotion hierarchy rules.
left - the first multiplicandright - the second multiplicandBitwise OR operation on two integral numbers.
left - the first operandright - the second operandBitwise OR of two numbers.
Default implementation throws UnsupportedOperationException. Subclasses supporting bitwise operations must override.
left - the first operandright - the second operandRemainder operation on two numbers.
Returns the remainder after integer division.
left - the dividendright - the divisorRemainder of two numbers.
Default implementation throws UnsupportedOperationException. Subclasses supporting remainder must override.
left - the dividendright - the divisorRight shift operation on integral numbers.
The shift distance (right operand) must be an integral type. The value to shift (left operand) must also be integral.
left - the value to shift (must be integral)right - the shift distance (must be integral)Right shift of a number.
Default implementation throws UnsupportedOperationException. Subclasses supporting bit shifting must override.
left - the value to shiftright - the shift distanceUnsigned right shift operation on integral numbers.
The shift distance (right operand) must be an integral type. The value to shift (left operand) must also be integral.
left - the value to shift (must be integral)right - the shift distance (must be integral)Unsigned right shift of a number.
Default implementation throws UnsupportedOperationException. Subclasses supporting bit shifting must override.
left - the value to shiftright - the shift distanceSubtracts two numbers using type promotion rules.
left - the minuendright - the subtrahendSubtracts two numbers.
Subclasses must implement this method according to type promotion hierarchy rules.
left - the minuendright - the subtrahendConverts a number to a BigDecimal.
Handles BigDecimal, BigInteger, integral types, and floating-point types. Floating-point numbers are converted via their string representation to preserve precision.
n - the number to convert (must not be null)Converts a number to a BigInteger.
Handles BigInteger, integral types, floating-point types, and BigDecimal. Floating-point numbers are first converted to BigDecimal before extraction.
n - the number to convert (must not be null)Unary negation of a number.
left - the operandNegates a number.
Subclasses must implement this method according to type promotion hierarchy rules.
left - the operandUnary plus (identity) operation on a number.
left - the operandReturns the number as-is (unary plus/identity).
Subclasses must implement this method according to type promotion hierarchy rules.
left - the operandBitwise XOR operation on two integral numbers.
left - the first operandright - the second operandBitwise XOR of two numbers.
Default implementation throws UnsupportedOperationException. Subclasses supporting bitwise operations must override.
left - the first operandright - the second operand