001: // ============================================================================
002: // $Id: Arithmetic.java,v 1.7 2005/08/02 23:45:05 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.fn.arithmetic;
034:
035: import java.io.Serializable;
036:
037: /**
038: * Defines arithmetic operations for classes derived from Number.
039: * <p>
040: * An implementation of Arithmetic for classes (such as BigDecimal and
041: * BigInteger) that provide the appropriate operations can simply map these
042: * methods to the methods provided by the Number. For the reference types,
043: * the implementation will need to dereference the arguments, perform the
044: * specified operation on the resulting primitives, and box up the result in
045: * a new reference type.
046: * <p>
047: * This interface may be used with user-defined Number implementations. For
048: * Example, assuming that a <code>Fraction</code> class has been defined,
049: * support for Fraction Arithmetic could be provided by<br>
050: * <pre>
051: * public class FractionMath implements Arithmetic<Fraction> {
052: * public Fraction plus (Fraction x, Fraction y) {
053: * // implementation omitted
054: * }
055: * ...
056: * }
057: * </pre>
058: * <p>
059: * To use Fractions with the various arithmetic Functors, it is necessary to
060: * register the Arithmetic implementation with the ArithmeticFactory.<br>
061: * <pre>
062: * ArithmeticFactory.register(Fraction.class, new FractionMath());
063: * </pre>
064: * <p>
065: * Copyright © 2003-2005 David A. Hall
066: *
067: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
068: */
069:
070: public interface Arithmetic<T extends Number> extends Serializable {
071: /**
072: * Returns the given value in the appropriate type
073: * @throws IllegalArgumentException if the value cannot be converted
074: */
075:
076: public T valueOf(Number value) throws IllegalArgumentException;
077:
078: /**
079: * Returns the value 0 of the appropriate type
080: */
081:
082: public T zero();
083:
084: /**
085: * Returns the value 1 of the appropriate type
086: */
087:
088: public T one();
089:
090: /**
091: * For numeric arguments x and y, returns x + y
092: * @return the sum of the two arguments
093: */
094:
095: public T plus(T x, T y);
096:
097: /**
098: * For numeric arguments x and y, returns x - y
099: * @return the difference of the two arguments
100: */
101:
102: public T minus(T x, T y);
103:
104: /**
105: * For numeric arguments x and y, returns x * y
106: * @return the product of the two arguments
107: */
108:
109: public T multiplies(T x, T y);
110:
111: /**
112: * For numeric arguments x and y, returns x / y
113: * @return the quotient of the two arguments
114: */
115:
116: public T divides(T x, T y);
117:
118: /**
119: * for numeric argument x, returns -x
120: * @return the negative of its argument
121: */
122:
123: public T negate(T x);
124: }
|