001: // ============================================================================
002: // $Id: TransformAdjacent.java,v 1.6 2006/05/08 02:22:04 davidahall Exp $
003: // Copyright (c) 2004-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 net.sf.jga.fn.BinaryFunctor;
035: import net.sf.jga.fn.UnaryFunctor;
036: import net.sf.jga.util.TransformAdjacentIterator;
037: import java.util.Iterator;
038:
039: /**
040: * Applies a UnaryFunctor to every element in an iteration, and iterates
041: * over the results.
042: * <p>
043: * Copyright © 2004-2005 David A. Hall
044: *
045: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
046: * @deprecated
047: */
048:
049: public class TransformAdjacent<T, R>
050: extends
051: UnaryFunctor<Iterator<? extends T>, TransformAdjacentIterator<T, R>> {
052: static final long serialVersionUID = -3208147943658111386L;
053:
054: private BinaryFunctor<T, T, R> _fn;
055:
056: /**
057: * Builds an TransformAdjacent functor that will apply the given functor to
058: * elements in an iteration.
059: * @throws IllegalArgumentException if the functor is null
060: */
061: public TransformAdjacent(BinaryFunctor<T, T, 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 an iteration.
070: */
071: public BinaryFunctor<T, T, R> getFunction() {
072: return _fn;
073: }
074:
075: /**
076: * Apply the functor to each element in the iteration and return an iterator
077: * over the results
078: *
079: * @return an iterator over the results of the transformation
080: */
081: public TransformAdjacentIterator<T, R> fn(
082: Iterator<? extends T> iterator) {
083: return new TransformAdjacentIterator<T, R>(iterator, _fn);
084: }
085:
086: /**
087: * Calls the Visitor's <code>visit(TransformAdjacent)</code> method, if it
088: * implements the nested Visitor interface.
089: */
090: public void accept(net.sf.jga.fn.Visitor v) {
091: if (v instanceof TransformAdjacent.Visitor)
092: ((TransformAdjacent.Visitor) v).visit(this );
093: else
094: v.visit(this );
095: }
096:
097: // Object overrides
098:
099: public String toString() {
100: return "TransformAdjacent[" + _fn + "]";
101: }
102:
103: // AcyclicVisitor
104:
105: /**
106: * Interface for classes that may interpret an <b>TransformAdjacent</b> functor.
107: */
108: public interface Visitor extends net.sf.jga.fn.Visitor {
109: public void visit(TransformAdjacent host);
110: }
111: }
|