net.sf.jga.fn.arithmetic

Interface Arithmetic<T>

public interface Arithmetic<T extends 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 David A. Hall

Author: David A. Hall

Method Summary
Tdivides(T x, T y)
For numeric arguments x and y, returns x / y
Tminus(T x, T y)
For numeric arguments x and y, returns x - y
Tmultiplies(T x, T y)
For numeric arguments x and y, returns x * y
Tnegate(T x)
for numeric argument x, returns -x
Tplus(T x, T y)
For numeric arguments x and y, returns x + y

Method Detail

divides

public T divides(T x, T y)
For numeric arguments x and y, returns x / y

Returns: the quotient of the two arguments

minus

public T minus(T x, T y)
For numeric arguments x and y, returns x - y

Returns: the difference of the two arguments

multiplies

public T multiplies(T x, T y)
For numeric arguments x and y, returns x * y

Returns: the product of the two arguments

negate

public T negate(T x)
for numeric argument x, returns -x

Returns: the negative of its argument

plus

public T plus(T x, T y)
For numeric arguments x and y, returns x + y

Returns: the sum of the two arguments