001: // ============================================================================
002: // $Id: Unique.java,v 1.6 2006/02/10 04:44:59 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.fn.UnaryFunctor;
037: import net.sf.jga.fn.comparison.EqualTo;
038: import net.sf.jga.util.UniqueIterator;
039:
040: /**
041: * Returns an iterator based on the input iterator that will not yield the
042: * same value twice in succession.
043: * <p>
044: * Copyright © 2003-2005 David A. Hall
045: *
046: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
047: * @deprecated
048: */
049:
050: public class Unique<T> extends
051: UnaryFunctor<Iterator<? extends T>, UniqueIterator<T>> {
052:
053: static final long serialVersionUID = 603897787127100783L;
054:
055: private BinaryFunctor<T, T, Boolean> _fn;
056:
057: /**
058: * Builds an Unique functor that will use EqualTo to compare successive
059: * elements.
060: * @throws IllegalArgumentException if the test is null
061: */
062: public Unique() {
063: this (new EqualTo<T>());
064: }
065:
066: /**
067: * Builds an Unique functor that will use the given functor to compare
068: * successive elements. The functor is required to return TRUE when its
069: * arguments are the same.
070: * @throws IllegalArgumentException if the test is null
071: */
072: public Unique(BinaryFunctor<T, T, Boolean> test) {
073: if (test == null)
074: throw new IllegalArgumentException();
075:
076: _fn = test;
077: }
078:
079: /**
080: * Returns the functor used to process elements in an iteration.
081: */
082: public BinaryFunctor<T, T, Boolean> getFunction() {
083: return _fn;
084: }
085:
086: /**
087: * Apply the functor to each element in the iteration and return an iterator
088: * over the results
089: *
090: * @return an iterator over the results of the transformation
091: */
092: public UniqueIterator<T> fn(Iterator<? extends T> iterator) {
093: return new UniqueIterator<T>(iterator, _fn);
094: }
095:
096: /**
097: * Calls the Visitor's <code>visit(Unique)</code> method, if it
098: * implements the nested Visitor interface.
099: */
100: public void accept(net.sf.jga.fn.Visitor v) {
101: if (v instanceof Unique.Visitor)
102: ((Unique.Visitor) v).visit(this );
103: else
104: v.visit(this );
105: }
106:
107: // Object overrides
108:
109: public String toString() {
110: return "Unique[" + _fn + "]";
111: }
112:
113: // AcyclicVisitor
114:
115: /**
116: * Interface for classes that may interpret an <b>Unique</b> functor.
117: */
118: public interface Visitor extends net.sf.jga.fn.Visitor {
119: public void visit(Unique host);
120: }
121: }
|