001: // ============================================================================
002: // $Id: TransformBinary.java,v 1.10 2006/05/08 02:22:04 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: package net.sf.jga.fn.algorithm;
033:
034: import java.util.Iterator;
035: import net.sf.jga.fn.BinaryFunctor;
036: import net.sf.jga.util.TransformBinaryIterator;
037:
038: /**
039: * Applies a BinaryFunctor to corresponding elements in a pair of iterations,
040: * and iterates over the results.
041: * <p>
042: * Copyright © 2003-2005 David A. Hall
043: *
044: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
045: * @deprecated
046: */
047:
048: public class TransformBinary<T1, T2, R>
049: extends
050: BinaryFunctor<Iterator<? extends T1>, Iterator<? extends T2>, Iterator<R>> {
051: static final long serialVersionUID = -6353149079946565288L;
052:
053: // the functor used to transform two iterators
054: private BinaryFunctor<T1, T2, R> _fn;
055:
056: /**
057: * Builds an TransformBinary functor that will use the given functor to
058: * process corresponding elements in a pair of iterations.
059: * @throws IllegalArgumentException if the functor is null
060: */
061: public TransformBinary(BinaryFunctor<T1, T2, R> fn) {
062: if (fn == null)
063: throw new IllegalArgumentException();
064:
065: _fn = fn;
066: }
067:
068: /**
069: * Returns the functor used to process elements in a pair of iterations.
070: */
071: public BinaryFunctor<T1, T2, R> getFunction() {
072: return _fn;
073: }
074:
075: /**
076: * Apply the functor to corresponding elements in the iterations and return
077: * an iterator over the results. The resulting iterator will contain the
078: * same number of elements as the shorter of the two input iterators (if
079: * one of the input iterators is empty, then the resulting iterator will
080: * also be empty).
081: *
082: * @return an iterator over the results of the transformation
083: */
084: public Iterator<R> fn(Iterator<? extends T1> i1,
085: Iterator<? extends T2> i2) {
086: return new TransformBinaryIterator<T1, T2, R>(i1, i2, _fn);
087: }
088:
089: /**
090: * Calls the Visitor's <code>visit(Transform)</code> method, if it
091: * implements the nested Visitor interface.
092: */
093: public void accept(net.sf.jga.fn.Visitor v) {
094: if (v instanceof TransformBinary.Visitor)
095: ((TransformBinary.Visitor) v).visit(this );
096: else
097: v.visit(this );
098: }
099:
100: // Object overrides
101:
102: public String toString() {
103: return "TransformBinary(" + _fn + ")";
104: }
105:
106: // AcyclicVisitor
107:
108: /**
109: * Interface for classes that may interpret an <b>Transform</b> functor.
110: */
111: public interface Visitor extends net.sf.jga.fn.Visitor {
112: public void visit(TransformBinary host);
113: }
114: }
|