001: // ============================================================================
002: // $Id: ShiftLeft.java,v 1.7 2006/04/26 03:15:56 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.text.MessageFormat;
036: import net.sf.jga.fn.BinaryFunctor;
037: import net.sf.jga.fn.EvaluationException;
038:
039: /**
040: * Returns the first argument shifted left by the number of places given in the
041: * second argument.
042: * <p>
043: * Copyright © 2003-2005 David A. Hall
044: *
045: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
046: */
047:
048: public class ShiftLeft<T extends Number> extends
049: BinaryFunctor<T, Integer, T> {
050:
051: static final long serialVersionUID = -3242117744551182384L;
052:
053: // Utility class that performs the bitwise shift as appropriate for the
054: // class given at construction
055: private IntegerArithmetic<T> _math;
056:
057: // The type of operands this instance supports
058: private Class<T> _type;
059:
060: /**
061: * Builds ShiftLeft functor for the given class. The class argument must
062: * be the same as the generic class argument (when generics are in use)
063: * or else a ClassCastException will be thrown when the functor is used.
064: *
065: * @throws IllegalArgumentException if the given class has no Arithmetic
066: * implementation registered with the ArithmeticFactory
067: */
068: public ShiftLeft(Class<T> c) {
069: _type = c;
070: getMath();
071: }
072:
073: /**
074: * Returns the type of operands this instance supports
075: */
076: public Class<T> getType() {
077: return _type;
078: }
079:
080: // Binary Functor interface
081:
082: /**
083: * Given integral arguments <b>x</b> and <b>y</b>, returns x << y
084: * @return x << y
085: */
086: public T fn(T x, Integer y) {
087: try {
088: return getMath().shiftLeft(x, y);
089: } catch (ClassCastException ex) {
090: String msg = "ClassCastException: Cannot compute shiftLeft of {0}[{1}] and {2}[{3}]";
091: String err = MessageFormat.format(msg, new Object[] {
092: x.getClass(), x, y.getClass(), y });
093: throw new EvaluationException(err, ex);
094: }
095: }
096:
097: /**
098: * Calls the Visitor's <code>visit(ShiftLeft)</code> method, if it implements
099: * the nested Visitor interface.
100: */
101: public void accept(net.sf.jga.fn.Visitor v) {
102: if (v instanceof ShiftLeft.Visitor)
103: ((ShiftLeft.Visitor) v).visit(this );
104: else
105: v.visit(this );
106: }
107:
108: /**
109: */
110: private IntegerArithmetic<T> getMath() {
111: if (_math == null) {
112: _math = ArithmeticFactory.getIntegralArithmetic(_type);
113: if (_math == null) {
114: String msg = "No implementation of Arithmetic registered for {0}";
115: throw new IllegalArgumentException(MessageFormat
116: .format(msg, new Object[] { _type }));
117: }
118: }
119:
120: return _math;
121: }
122:
123: // Object overrides
124:
125: public String toString() {
126: return "ShiftLeft";
127: }
128:
129: // AcyclicVisitor
130:
131: /**
132: * Interface for classes that may interpret a <b>ShiftLeft</b> functor.
133: */
134: public interface Visitor extends net.sf.jga.fn.Visitor {
135: public void visit(ShiftLeft host);
136: }
137: }
|