001: // ============================================================================
002: // $Id: ArithmeticFactory.java,v 1.8 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.math.BigDecimal;
036: import java.math.BigInteger;
037: import java.util.HashMap;
038:
039: /**
040: * Builds and distributes implementations of the Arithmetic and
041: * IntegerArithmetic interfaces that are available for supported Number
042: * classes.
043: * <p>
044: * Copyright © 2003-2005 David A. Hall
045: *
046: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
047: */
048:
049: public class ArithmeticFactory {
050: static private HashMap<Class<? extends Number>, Arithmetic<? extends Number>> _arithmeticMap = new HashMap<Class<? extends Number>, Arithmetic<? extends Number>>();
051:
052: static private HashMap<Class<? extends Number>, IntegerArithmetic<? extends Number>> _integralMap = new HashMap<Class<? extends Number>, IntegerArithmetic<? extends Number>>();
053:
054: /**
055: * Registers the Arithmetic implementation for the given class
056: */
057: static public <T extends Number> void register(Class<T> c,
058: Arithmetic<T> math) {
059: _arithmeticMap.put(c, math);
060: }
061:
062: /**
063: * Registers the IntegerArithmetic implementation for the given class
064: */
065: static public <T extends Number> void register(Class<T> c,
066: IntegerArithmetic<T> math) {
067: _arithmeticMap.put(c, math);
068: _integralMap.put(c, math);
069: }
070:
071: /**
072: * Returns the Arithmetic implementation registered for the given class
073: * @return the Arithmetic implementation registered for the given class
074: */
075: static public <T extends Number> Arithmetic<T> getArithmetic(
076: Class<T> c) {
077: // @SuppressWarnings
078: // the map is private, and the register methods ensure that the specific
079: // runtime type of each implementation is associated with the correct
080: // class.
081: return (Arithmetic<T>) _arithmeticMap.get(c);
082: }
083:
084: /**
085: * Returns the IntegerArithmetic implementation registered for the given
086: * class
087: * @return the IntegerArithmetic implementation registered for the given
088: * class
089: */
090: static public <T extends Number> IntegerArithmetic<T> getIntegralArithmetic(
091: Class<T> c) {
092: // @SuppressWarnings
093: // the map is private, and the register methods ensure that the specific
094: // runtime type of each implementation is associated with the correct
095: // class.
096: return (IntegerArithmetic<T>) _integralMap.get(c);
097: }
098:
099: static {
100: register(Byte.class, new ByteMath());
101: register(Short.class, new ShortMath());
102: register(Integer.class, new IntegerMath());
103: register(Long.class, new LongMath());
104: register(Float.class, new FloatMath());
105: register(Double.class, new DoubleMath());
106: register(BigDecimal.class, new DecimalMath());
107: register(BigInteger.class, new BigIntMath());
108: }
109: }
|