KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > algorithms > Filter


1 // ============================================================================
2
// $Id: Filter.java,v 1.7 2006/12/16 16:48:57 davidahall Exp $
3
// Copyright (c) 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

33 package net.sf.jga.algorithms;
34
35 import java.util.Collection JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import net.sf.jga.fn.UnaryFunctor;
38 import net.sf.jga.fn.comparison.Equality;
39 import net.sf.jga.fn.comparison.NotEqualTo;
40 import net.sf.jga.fn.logical.UnaryNegate;
41 import net.sf.jga.util.FindIterator;
42
43 import static net.sf.jga.util.ArrayUtils.*;
44 import static net.sf.jga.util.CollectionUtils.*;
45
46 /**
47  * Algorithms that return a subset of its input. The input may be an array, collection, or
48  * iteration. There are two general algorithms included: filter and remove. The filter
49  * methods return elements from the input that pass the selection criteria, while the remove
50  * methods removes elements from the input stream for which the selection criteria is true.
51  * Note that the actual input is not modified in any of these methods.
52  * <p>
53  * Copyright &copy; 2006 David A. Hall
54  */

55
56 public class Filter {
57     
58     // ============
59
// Arrays
60
// ============
61

62     /**
63      * Returns the elements of the array that meet the given selection criteria.
64      */

65     static public <T> Iterable JavaDoc<T> filter(T[] ts, UnaryFunctor<T,Boolean JavaDoc> pred) {
66         return new FilterIterable<T>(iterable(ts), pred);
67     }
68     
69     /**
70      * Returns the elements of the array that are not equal to the given value, using the value's
71      * {@link equals} method
72      */

73     static public <T> Iterable JavaDoc<T> remove(T[] ts, T value) {
74         return filter(ts, new NotEqualTo<T>().bind2nd(value));
75     }
76
77     /**
78      * Returns the elements of the array that are not equal to the given value, using the given
79      * functor to compare for equality
80      */

81     static public <T> Iterable JavaDoc<T> remove(T[] ts, T value, Equality<T> eq) {
82         return filter(ts, new UnaryNegate<T>(eq.bind2nd(value)));
83     }
84
85     /**
86      * Returns the elements of the array that do not meet the given selection criteria
87      */

88     static public <T> Iterable JavaDoc<T> remove(T[] ts, UnaryFunctor<T,Boolean JavaDoc> pred) {
89         return filter(ts, new UnaryNegate<T>(pred));
90     }
91
92     // ============
93
// Iterables
94
// ============
95

96     /**
97      * Returns the contents of the input that meet the given selection criteria.
98      */

99     static public <T> Iterable JavaDoc<T> filter(Iterable JavaDoc<? extends T> i, UnaryFunctor<T,Boolean JavaDoc> pred) {
100         return new FilterIterable<T>(i, pred);
101     }
102
103     /**
104      * Returns the elements of the input that are not equal to the given value, using the value's
105      * {@link equals} method
106      */

107     static public <T> Iterable JavaDoc<T> remove(Iterable JavaDoc<? extends T> i, T value) {
108         return filter(i, new NotEqualTo<T>().bind2nd(value));
109     }
110
111     /**
112      * Returns the elements of the input that are not equal to the given value, using the given
113      * functor to compare for equality
114      */

115     static public <T> Iterable JavaDoc<T> remove(Iterable JavaDoc<? extends T> i, T value, Equality<T> eq) {
116         return filter(i, new UnaryNegate<T>(eq.bind2nd(value)));
117     }
118
119     /**
120      * Returns the elements of the input that do not meet the given selection criteria
121      */

122     static public <T> Iterable JavaDoc<T> remove(Iterable JavaDoc<? extends T> i, UnaryFunctor<T,Boolean JavaDoc> pred) {
123         return filter(i, new UnaryNegate<T>(pred));
124     }
125     
126     // ============
127
// Iterators
128
// ============
129

130     /**
131      * Returns the contents of the iterator that meet the given selection criteria.
132      */

133     static public <T> Iterator JavaDoc<T> filter(Iterator JavaDoc<? extends T> iter, UnaryFunctor<T,Boolean JavaDoc> pred) {
134         return new net.sf.jga.util.FilterIterator<T>(iter, pred);
135     }
136     
137     /**
138      * Returns the elements of the iterator that are not equal to the given value, using the value's
139      * {@link equals} method
140      */

141     static public <T> Iterator JavaDoc<T> remove(Iterator JavaDoc<? extends T> iter, T value) {
142         return filter(iter, new NotEqualTo<T>().bind2nd(value));
143     }
144
145     /**
146      * Returns the elements of the iterator that are not equal to the given value, using the given
147      * functor to compare for equality
148      */

149     static public <T> Iterator JavaDoc<T> remove(Iterator JavaDoc<? extends T> iter, T value, Equality<T> eq) {
150         return filter(iter, new UnaryNegate<T>(eq.bind2nd(value)));
151     }
152
153     /**
154      * Returns the elements of the iterator that do not meet the given selection criteria
155      */

156     static public <T> Iterator JavaDoc<T> remove(Iterator JavaDoc<? extends T> iter,UnaryFunctor<T,Boolean JavaDoc> pred){
157         return filter(iter, new UnaryNegate<T>(pred));
158     }
159     
160     // ============
161
// IterableCopy
162
// ============
163

164     /**
165      * Appends the contents of the input that meet the given selection criteria to the output.
166      */

167     static public <T, TCollection extends Collection JavaDoc<? super T>> TCollection
168     filter(Iterable JavaDoc<? extends T> cin, UnaryFunctor<T,Boolean JavaDoc> pred, TCollection cout) {
169         return append(cout, filter(cin.iterator(), pred));
170     }
171     
172
173     /**
174      * Returns the elements of the input that are not equal to the given value, using the value's
175      * {@link equals} method
176      */

177     static public <T, TCollection extends Collection JavaDoc<? super T>> TCollection
178     remove(Iterable JavaDoc<? extends T> cin, T value, TCollection cout) {
179         return append(cout, remove(cin.iterator(), value));
180     }
181
182     
183     /**
184      * Returns the elements of the input that are not equal to the given value, using the given
185      * functor to compare for equality
186      */

187     static public <T, TCollection extends Collection JavaDoc<? super T>> TCollection
188     remove(Iterable JavaDoc<? extends T> cin, T value, Equality<T> eq, TCollection cout) {
189         return append(cout, remove(cin.iterator(), value, eq));
190     }
191
192     
193     /**
194      * Returns the elements of the input that do not meet the given selection criteria
195      */

196     static public <T, TCollection extends Collection JavaDoc<? super T>> TCollection
197     remove(Iterable JavaDoc<? extends T> cin, UnaryFunctor<T,Boolean JavaDoc> pred, TCollection cout) {
198         return append(cout, remove(cin.iterator(), pred));
199     }
200
201     // ==============
202
// FilterIterable
203
// ==============
204

205     /**
206      * Produces Iterators that only return elements that meet a given condition.
207      */

208
209     // Suggested by sourceforge user Jerry Vos:
210
// https://sourceforge.net/tracker/index.php?func=detail&aid=1276988&group_id=49942&atid=458041
211

212     static public class FilterIterable<T> implements Iterable JavaDoc<T> {
213     
214         private Iterable JavaDoc<? extends T> _delegate;
215
216         // predicate used to test elements of the base iterator.
217
private UnaryFunctor<T,Boolean JavaDoc> _filter;
218
219         /**
220          * Builds a FilterIterator that will return only qualifying elements of
221          * the given iterable.
222          */

223         public FilterIterable(Iterable JavaDoc<? extends T> iter,
224                               UnaryFunctor<T,Boolean JavaDoc> pred)
225         {
226             _delegate = iter;
227             _filter = pred;
228         }
229     
230         // - - - - - - - - - - -
231
// Iterable<T> interface
232
// - - - - - - - - - - -
233

234         public Iterator JavaDoc<T> iterator() {
235             return new net.sf.jga.util.FilterIterator<T>(_delegate.iterator(), _filter);
236         }
237     }
238
239     
240     /**
241      * Iterator that only returns elements that meet the given selection criteria.
242      **/

243
244     static public class FilterIterator<T> implements Iterator JavaDoc<T> {
245
246         // base iterator
247
private FindIterator<T> _base;
248
249         // predicate used to test elements of the base iterator.
250
private UnaryFunctor<T,Boolean JavaDoc> _filter;
251
252         // flag indicating that the base iterator has been advanced to the next
253
// qualifying element (or off the end if no remaining element qualifies)
254
private boolean _testedNext;
255
256         /**
257          * Builds a FilterIterator that will return only qualifying elements of
258          * the given iterator.
259          */

260         public FilterIterator(Iterator JavaDoc<? extends T> iter, UnaryFunctor<T,Boolean JavaDoc> pred) {
261             _base = new FindIterator<T>(iter);
262             _filter = pred;
263         }
264
265         // - - - - - - - - - - - - -
266
// Iterator<T> interface
267
// - - - - - - - - - - - - -
268

269         public boolean hasNext(){
270             _testedNext = true;
271             return _base.findNext(_filter);
272         }
273
274         public T next() {
275             if (!_testedNext) {
276                 hasNext();
277             }
278         
279             _testedNext = false;
280             return _base.next();
281         }
282
283         public void remove() { throw new UnsupportedOperationException JavaDoc(); }
284     }
285 }
286
Popular Tags