net.sf.jga.fn.arithmetic
Interface Arithmetic<T extends java.lang.Number>

All Superinterfaces:
java.io.Serializable
All Known Subinterfaces:
IntegerArithmetic<T>

public interface Arithmetic<T extends java.lang.Number>
extends java.io.Serializable

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

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

valueOf

T valueOf(java.lang.Number value)
                                   throws java.lang.IllegalArgumentException
Returns the given value in the appropriate type

Throws:
java.lang.IllegalArgumentException - if the value cannot be converted

zero

T zero()
Returns the value 0 of the appropriate type


one

T one()
Returns the value 1 of the appropriate type


plus

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

Returns:
the sum of the two arguments

minus

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

Returns:
the difference of the two arguments

multiplies

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

Returns:
the product of the two arguments

divides

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

Returns:
the quotient of the two arguments

negate

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

Returns:
the negative of its argument


Copyright © 2002-2006 David A. Hall. All Rights Reserved.