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
author: David A. Hall |