|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Arithmetic<T extends java.lang.Number>
Defines arithmetic operations for classes derived from Number.
An implementation of Arithmetic for classes (such as BigDecimal and BigInteger) that provide the appropriate operations can simply map these methods to the methods provided by the Number. For the reference types, the implementation will need to dereference the arguments, perform the specified operation on the resulting primitives, and box up the result in a new reference type.
This interface may be used with user-defined Number implementations. For
Example, assuming that a Fraction
class has been defined,
support for Fraction Arithmetic could be provided by
public class FractionMath implements Arithmetic<Fraction> { public Fraction plus (Fraction x, Fraction y) { // implementation omitted } ... }
To use Fractions with the various arithmetic Functors, it is necessary to
register the Arithmetic implementation with the ArithmeticFactory.
ArithmeticFactory.register(Fraction.class, new FractionMath());
Copyright © 2003-2005 David A. Hall
Method Summary | |
---|---|
T |
divides(T x,
T y)
For numeric arguments x and y, returns x / y |
T |
minus(T x,
T y)
For numeric arguments x and y, returns x - y |
T |
multiplies(T x,
T y)
For numeric arguments x and y, returns x * y |
T |
negate(T x)
for numeric argument x, returns -x |
T |
one()
Returns the value 1 of the appropriate type |
T |
plus(T x,
T y)
For numeric arguments x and y, returns x + y |
T |
valueOf(java.lang.Number value)
Returns the given value in the appropriate type |
T |
zero()
Returns the value 0 of the appropriate type |
Method Detail |
---|
T valueOf(java.lang.Number value) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if the value cannot be convertedT zero()
T one()
T plus(T x, T y)
T minus(T x, T y)
T multiplies(T x, T y)
T divides(T x, T y)
T negate(T x)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |