Top Description Constructors Methods
org.codehaus.groovy.runtime.typehandling

public abstract Class NumberMath

extends Object
Class Inheritance
Known Direct Subclasses
org.codehaus.groovy.runtime.typehandling.BigDecimalMath, org.codehaus.groovy.runtime.typehandling.BigIntegerMath, org.codehaus.groovy.runtime.typehandling.FloatingPointMath, org.codehaus.groovy.runtime.typehandling.IntegerMath, org.codehaus.groovy.runtime.typehandling.LongMath
Imports
java.math.BigDecimal, .BigInteger

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:
  1. Support a 'least surprising' math model to scripting language users. This means that exact, or decimal math should be used for default calculations. This scheme assumes that by default, groovy literals with decimal points are instantiated as BigDecimal objects rather than binary floating points (Float, Double).
  2. Do not force the appearance of exactness on a number that is by definition not guaranteed to be exact. In particular this means that if an operand in a NumberMath operation is a binary floating point number, ensure that the result remains a binary floating point number (i.e. never automatically promote a binary floating point number to a BigDecimal). This has the effect of preserving the expectations of binary floating point users and helps performance.
  3. Provide an implementation that is as close as practical to the Java 1.5 BigDecimal math model which implements precision based floating point decimal math (ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 (section 7.4).

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static Number
abs(Number number)

protected abstract Number
absImpl(Number number)

public static Number
add(Number left, Number right)

public abstract Number
addImpl(Number left, Number right)

public static Number
and(Number left, Number right)

protected Number
andImpl(Number left, Number right)

public static Number
protected Number
public static int
compareTo(Number left, Number right)

public abstract int
protected UnsupportedOperationException
public static Number
divide(Number left, Number right)

public abstract Number
divideImpl(Number left, Number right)

public static NumberMath
getMath(Number left, Number right)

Determine which NumberMath instance to use, given the supplied operands.

pack-priv static NumberMath
getMath(Number number)

public static Number
intdiv(Number left, Number right)

protected Number
intdivImpl(Number left, Number right)

public static boolean
public static boolean
public static boolean
isByte(Number number)

public static boolean
public static boolean
isInteger(Number number)

public static boolean
isLong(Number number)

public static boolean
isShort(Number number)

public static Number
leftShift(Number left, Number right)

For this operation, consider the operands independently.

protected Number
public static Number
mod(Number left, Number right)

protected Number
modImpl(Number left, Number right)

public static Number
multiply(Number left, Number right)

public abstract Number
multiplyImpl(Number left, Number right)

public static Number
or(Number left, Number right)

protected Number
orImpl(Number left, Number right)

public static Number
rightShift(Number left, Number right)

For this operation, consider the operands independently.

protected Number
public static Number
rightShiftUnsigned(Number left, Number right)

For this operation, consider the operands independently.

protected Number
public static Number
subtract(Number left, Number right)

public abstract Number
subtractImpl(Number left, Number right)

public static BigDecimal
public static BigInteger
public static Number
protected abstract Number
public static Number
protected abstract Number
public static Number
xor(Number left, Number right)

protected Number
xorImpl(Number left, Number right)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

NumberMathback to summary
public NumberMath()

Method Detail

absback to summary
public static Number abs(Number number)
absImplback to summary
protected abstract Number absImpl(Number number)
addback to summary
public static Number add(Number left, Number right)
addImplback to summary
public abstract Number addImpl(Number left, Number right)
andback to summary
public static Number and(Number left, Number right)
andImplback to summary
protected Number andImpl(Number left, Number right)
bitwiseNegateback to summary
public static Number bitwiseNegate(Number left)
bitwiseNegateImplback to summary
protected Number bitwiseNegateImpl(Number left)
compareToback to summary
public static int compareTo(Number left, Number right)
compareToImplback to summary
public abstract int compareToImpl(Number left, Number right)
createUnsupportedExceptionback to summary
protected UnsupportedOperationException createUnsupportedException(String operation, Number left)
divideback to summary
public static Number divide(Number left, Number right)
divideImplback to summary
public abstract Number divideImpl(Number left, Number right)
getMathback to summary
public static NumberMath getMath(Number left, Number right)

Determine which NumberMath instance to use, given the supplied operands. This method implements the type promotion rules discussed in the documentation. Note that by the time this method is called, any Byte, Character or Short operands will have been promoted to Integer. For reference, here is the 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 Note that for division, if either operand isFloatingPoint, the result will be floating. Otherwise, the result is BigDecimal

getMathback to summary
pack-priv static NumberMath getMath(Number number)
intdivback to summary
public static Number intdiv(Number left, Number right)
intdivImplback to summary
protected Number intdivImpl(Number left, Number right)
isBigDecimalback to summary
public static boolean isBigDecimal(Number number)
isBigIntegerback to summary
public static boolean isBigInteger(Number number)
isByteback to summary
public static boolean isByte(Number number)
isFloatingPointback to summary
public static boolean isFloatingPoint(Number number)
isIntegerback to summary
public static boolean isInteger(Number number)
isLongback to summary
public static boolean isLong(Number number)
isShortback to summary
public static boolean isShort(Number number)
leftShiftback to summary
public static Number leftShift(Number left, Number right)

For this operation, consider the operands independently. Throw an exception if the right operand (shift distance) is not an integral type. For the left operand (shift value) also require an integral type, but do NOT promote from Integer to Long. This is consistent with Java, and makes sense for the shift operators.

leftShiftImplback to summary
protected Number leftShiftImpl(Number left, Number right)
modback to summary
public static Number mod(Number left, Number right)
modImplback to summary
protected Number modImpl(Number left, Number right)
multiplyback to summary
public static Number multiply(Number left, Number right)
multiplyImplback to summary
public abstract Number multiplyImpl(Number left, Number right)
orback to summary
public static Number or(Number left, Number right)
orImplback to summary
protected Number orImpl(Number left, Number right)
rightShiftback to summary
public static Number rightShift(Number left, Number right)

For this operation, consider the operands independently. Throw an exception if the right operand (shift distance) is not an integral type. For the left operand (shift value) also require an integral type, but do NOT promote from Integer to Long. This is consistent with Java, and makes sense for the shift operators.

rightShiftImplback to summary
protected Number rightShiftImpl(Number left, Number right)
rightShiftUnsignedback to summary
public static Number rightShiftUnsigned(Number left, Number right)

For this operation, consider the operands independently. Throw an exception if the right operand (shift distance) is not an integral type. For the left operand (shift value) also require an integral type, but do NOT promote from Integer to Long. This is consistent with Java, and makes sense for the shift operators.

rightShiftUnsignedImplback to summary
protected Number rightShiftUnsignedImpl(Number left, Number right)
subtractback to summary
public static Number subtract(Number left, Number right)
subtractImplback to summary
public abstract Number subtractImpl(Number left, Number right)
toBigDecimalback to summary
public static BigDecimal toBigDecimal(Number n)
toBigIntegerback to summary
public static BigInteger toBigInteger(Number n)
unaryMinusback to summary
public static Number unaryMinus(Number left)
unaryMinusImplback to summary
protected abstract Number unaryMinusImpl(Number left)
unaryPlusback to summary
public static Number unaryPlus(Number left)
unaryPlusImplback to summary
protected abstract Number unaryPlusImpl(Number left)
xorback to summary
public static Number xor(Number left, Number right)
xorImplback to summary
protected Number xorImpl(Number left, Number right)