KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > algorithm > ReplaceAll


1 // ============================================================================
2
// $Id: ReplaceAll.java,v 1.7 2006/01/08 00:52:25 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32
package net.sf.jga.fn.algorithm;
33
34 import java.util.Iterator JavaDoc;
35 import net.sf.jga.fn.UnaryFunctor;
36 import net.sf.jga.fn.adaptor.ConditionalUnary;
37 import net.sf.jga.fn.adaptor.ConstantUnary;
38 import net.sf.jga.util.TransformIterator;
39 import net.sf.jga.fn.adaptor.Identity;
40
41 /**
42  * Replaces all instances in an iteration that pass the given test with the
43  * given value.
44  * <p>
45  * To Serialize a ReplaceAll, the generic parameter T must be serializable.
46  * <p>
47  * Copyright &copy; 2003-2005 David A. Hall
48  *
49  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
50  */

51
52 public class ReplaceAll<T>
53     extends UnaryFunctor<Iterator JavaDoc<? extends T>, TransformIterator<T,T>>
54 {
55     static final long serialVersionUID = -6156225521195687370L;
56
57     // The functor used determine which elements to replace
58
private UnaryFunctor<T,T> _fn;
59
60     // the value that replaces some items
61
private T _value;
62
63     /**
64      * Builds an ReplaceAll functor that will apply the given test to
65      * elements in an iteration, and replace those elements that pass with the
66      * given value.
67      * @throws IllegalArgumentException if the test is null
68      */

69     public ReplaceAll(UnaryFunctor<T,Boolean JavaDoc> test, T value) {
70         if (test == null)
71             throw new IllegalArgumentException JavaDoc();
72
73         _value = value;
74         _fn = new ConditionalUnary<T,T>(test, new ConstantUnary<T,T>(value), new Identity<T>());
75     }
76
77     /**
78      * Returns the functor used to process elements in an iteration.
79      */

80     public UnaryFunctor<T,T> getFunction() {
81         return _fn;
82     }
83
84     /**
85      * Returns the replacement value
86      */

87
88     public T getValue() {
89         return _value;
90     }
91
92     /**
93      * Apply the functor to each element in the iteration and return an iterator
94      * over the results
95      *
96      * @return an iterator over the results of the transformation
97      */

98     public TransformIterator<T,T> fn(Iterator JavaDoc<? extends T> iterator) {
99         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 JavaDoc 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 }
128
Popular Tags