001: // ============================================================================
002: // $Id: ReplaceAll.java,v 1.7 2006/01/08 00:52:25 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.ConditionalUnary;
037: import net.sf.jga.fn.adaptor.ConstantUnary;
038: import net.sf.jga.util.TransformIterator;
039: import net.sf.jga.fn.adaptor.Identity;
040:
041: /**
042: * Replaces all instances in an iteration that pass the given test with the
043: * given value.
044: * <p>
045: * To Serialize a ReplaceAll, the generic parameter T must be serializable.
046: * <p>
047: * Copyright © 2003-2005 David A. Hall
048: *
049: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
050: */
051:
052: public class ReplaceAll<T> extends
053: UnaryFunctor<Iterator<? extends T>, TransformIterator<T, T>> {
054: static final long serialVersionUID = -6156225521195687370L;
055:
056: // The functor used determine which elements to replace
057: private UnaryFunctor<T, T> _fn;
058:
059: // the value that replaces some items
060: private T _value;
061:
062: /**
063: * Builds an ReplaceAll functor that will apply the given test to
064: * elements in an iteration, and replace those elements that pass with the
065: * given value.
066: * @throws IllegalArgumentException if the test is null
067: */
068: public ReplaceAll(UnaryFunctor<T, Boolean> test, T value) {
069: if (test == null)
070: throw new IllegalArgumentException();
071:
072: _value = value;
073: _fn = new ConditionalUnary<T, T>(test, new ConstantUnary<T, T>(
074: value), new Identity<T>());
075: }
076:
077: /**
078: * Returns the functor used to process elements in an iteration.
079: */
080: public UnaryFunctor<T, T> getFunction() {
081: return _fn;
082: }
083:
084: /**
085: * Returns the replacement value
086: */
087:
088: public T getValue() {
089: return _value;
090: }
091:
092: /**
093: * Apply the functor to each element in the iteration and return an iterator
094: * over the results
095: *
096: * @return an iterator over the results of the transformation
097: */
098: public TransformIterator<T, T> fn(Iterator<? extends T> iterator) {
099: return new TransformIterator<T, T>(iterator, _fn);
100: }
101:
102: /**
103: * Calls the Visitor's <code>visit(ReplaceAll)</code> method, if it
104: * implements the nested Visitor interface.
105: */
106: public void accept(net.sf.jga.fn.Visitor v) {
107: if (v instanceof ReplaceAll.Visitor)
108: ((ReplaceAll.Visitor) v).visit(this );
109: else
110: v.visit(this );
111: }
112:
113: // Object overrides
114:
115: public String toString() {
116: return "ReplaceAll";
117: }
118:
119: // AcyclicVisitor
120:
121: /**
122: * Interface for classes that may interpret an <b>ReplaceAll</b> functor.
123: */
124: public interface Visitor extends net.sf.jga.fn.Visitor {
125: public void visit(ReplaceAll host);
126: }
127: }
|