001: // ============================================================================
002: // $Id: RemoveAll.java,v 1.7 2006/08/10 03:34:27 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.UnaryFunctor;
036: import net.sf.jga.fn.adaptor.Constant;
037: import net.sf.jga.fn.adaptor.Identity;
038: import net.sf.jga.fn.comparison.EqualTo;
039: import net.sf.jga.fn.comparison.Equality;
040: import net.sf.jga.fn.logical.UnaryNegate;
041: import net.sf.jga.util.FilterIterator;
042: import net.sf.jga.util.TransformIterator;
043:
044: /**
045: * Removes instances from an iteration. This functor returns an interator that
046: * skips specific values or values that meet a given condition. The underlying
047: * iterable is not modified.
048: * <p>
049: * Copyright © 2003-2005 David A. Hall
050: * @deprecated
051: */
052:
053: public class RemoveAll<T> extends
054: UnaryFunctor<Iterator<? extends T>, FilterIterator<T>> {
055: static final long serialVersionUID = 8231936204853616237L;
056:
057: private UnaryFunctor<T, Boolean> _fn;
058: private UnaryFunctor<T, Boolean> _notFn;
059:
060: /**
061: * Builds an RemoveAll functor that will remove instances of the given value
062: * from an iteration.
063: * @throws IllegalArgumentException if the test is null
064: */
065: public RemoveAll(T value) {
066: this (new EqualTo<T>().bind2nd(value));
067: }
068:
069: /**
070: * Builds an RemoveAll functor that will remove instances of the given value
071: * from an iteration, using the given Equality.
072: * @throws IllegalArgumentException if the test is null
073: */
074: public RemoveAll(Equality<T> eq, T value) {
075: this (eq.bind2nd(value));
076: }
077:
078: /**
079: * Builds an RemoveAll functor that will remove elements from an iteration
080: * that pass the given test
081: * @throws IllegalArgumentException if the test is null
082: */
083: public RemoveAll(UnaryFunctor<T, Boolean> test) {
084: if (test == null)
085: throw new IllegalArgumentException();
086:
087: _fn = test;
088: _notFn = new UnaryNegate<T>(test);
089: }
090:
091: /**
092: * Returns the functor used to process elements in an iteration.
093: */
094: public UnaryFunctor<T, Boolean> getFunction() {
095: return _fn;
096: }
097:
098: /**
099: * Apply the functor to each element in the iteration and return an iterator
100: * over the results
101: *
102: * @return an iterator over the results of the transformation
103: */
104: public FilterIterator<T> fn(Iterator<? extends T> iterator) {
105: return new FilterIterator<T>(iterator, _notFn);
106: }
107:
108: /**
109: * Calls the Visitor's <code>visit(RemoveAll)</code> method, if it
110: * implements the nested Visitor interface.
111: */
112: public void accept(net.sf.jga.fn.Visitor v) {
113: if (v instanceof RemoveAll.Visitor)
114: ((RemoveAll.Visitor) v).visit(this );
115: else
116: v.visit(this );
117: }
118:
119: // Object overrides
120:
121: public String toString() {
122: return "RemoveAll[" + _fn + "]";
123: }
124:
125: // AcyclicVisitor
126:
127: /**
128: * Interface for classes that may interpret an <b>RemoveAll</b> functor.
129: */
130: public interface Visitor extends net.sf.jga.fn.Visitor {
131: public void visit(RemoveAll host);
132: }
133: }
|