001: // ============================================================================
002: // $Id: Iterables.java,v 1.18 2006/09/02 03:10:33 davidahall Exp $
003: // Copyright (c) 2004-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:
033: package net.sf.jga.util;
034:
035: import java.util.Arrays;
036: import java.util.Collection;
037: import java.util.Comparator;
038: import java.util.Iterator;
039: import net.sf.jga.algorithms.Filter;
040: import net.sf.jga.algorithms.Merge;
041: import net.sf.jga.algorithms.Transform;
042: import net.sf.jga.algorithms.Unique;
043: import net.sf.jga.fn.BinaryFunctor;
044: import net.sf.jga.fn.UnaryFunctor;
045:
046: import static net.sf.jga.util.ArrayUtils.*;
047:
048: /**
049: * Facade for the Iterators in this package, supporting the new forloop syntax.
050: * Only iterators that perform their service during the hasNext()/next()
051: * sequence are given in this facade. Iterators that augment to the basic
052: * Iterator interface, (e.g., CachingIterator) are not included.
053: * <p>
054: * Copyright © 2004-2005 David A. Hall
055: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
056: */
057:
058: public class Iterables {
059: /**
060: * Returns iterators that point to the next instance in an iterable that
061: * meets the condition described in the functor.
062: */
063: static public <T> Iterable<Iterator<? extends T>> findAll(
064: Iterable<? extends T> i,
065: UnaryFunctor<Iterator<? extends T>, ? extends Iterator<T>> fn) {
066: return new FindAllIterator<T>(i.iterator(), fn);
067: }
068:
069: /**
070: * Returns iterators that point to the next instance in an iterable that
071: * meets the condition described in the functor.
072: */
073: static public <T> Iterable<Iterator<? extends T>> findAll(
074: T[] ts,
075: UnaryFunctor<Iterator<? extends T>, ? extends Iterator<T>> fn) {
076: return new FindAllIterator<T>(iterate(ts), fn);
077: }
078:
079: /**
080: * Returns only elements for which the predicate is true
081: * @deprecated use Filter.filter(Iterable,UnaryFunctor) instead
082: */
083: static public <T> Iterable<T> filter(Iterable<? extends T> i,
084: UnaryFunctor<T, Boolean> pred) {
085: return Filter.filter(i, pred);
086: }
087:
088: /**
089: * Returns only elements for which the predicate is true
090: * @deprecated use Filter.filter(T[], UnaryFunctor) instead
091: */
092: static public <T> Iterable<T> filter(T[] ts,
093: UnaryFunctor<T, Boolean> pred) {
094: return Filter.filter(ts, pred);
095: }
096:
097: /**
098: * Returns all elements of both iterables, always choosing the lesser of the
099: * two current elements.
100: * @deprecated use Merge.merge(Iterable,Iterable) instead
101: */
102: static public <T extends Comparable/*EA2.2:*/<T>/**/> Iterable<T> merge(
103: Iterable<? extends T> i1, Iterable<? extends T> i2) {
104: return Merge.merge(i1, i2);
105: }
106:
107: /**
108: * Returns all elements of both arrays, always choosing the lesser of the
109: * two current elements.
110: * @deprecated use Merge.merge(T[],T[]) instead
111: */
112: static public <T extends Comparable/*EA2.2:*/<T>/**/> Iterable<T> merge(
113: T[] ts1, T[] ts2) {
114: return Merge.merge(ts1, ts2);
115: }
116:
117: /**
118: * Returns all elements of both iterables, always choosing the lesser of the
119: * two current elements.
120: * @deprecated use Merge.merge(Iterable,Iterable,Comparator) instead
121: */
122: static public <T> Iterable<T> merge(Iterable<? extends T> i1,
123: Iterable<? extends T> i2, Comparator<T> comp) {
124: return Merge.merge(i1, i2, comp);
125: }
126:
127: /**
128: * Returns all elements of both arrays, always choosing the lesser of the
129: * two current elements.
130: * @deprecated use Merge.merge(T[],T[],Comparator) instead
131: */
132: static public <T> Iterable<T> merge(T[] ts1, T[] ts2,
133: Comparator<T> comp) {
134: return Merge.merge(ts1, ts2, comp);
135: }
136:
137: /**
138: * Returns all elements of both iterables, using the given predicate to
139: * choose which element to return. If the predicate is true, choose the
140: * current element of the first iterable, otherwise choose the current
141: * element of the second iterable. When one is exhausted, returns elements
142: * remaining in the other.
143: * @deprecated use Merge.merge(Iterable,Iterable,BinaryFunctor) instead
144: */
145: static public <T> Iterable<T> merge(Iterable<? extends T> i1,
146: Iterable<? extends T> i2, BinaryFunctor<T, T, Boolean> fn) {
147: return Merge.merge(i1, i2, fn);
148: }
149:
150: /**
151: * Returns all elements of both arrays, using the given predicate to
152: * choose which element to return. If the predicate is true, choose the
153: * current element of the first iterable, otherwise choose the current
154: * element of the second iterable. When one is exhausted, returns elements
155: * remaining in the other.
156: * @deprecated use Merge.merge(T[],T[],BinaryFunctor) instead
157: */
158: static public <T> Iterable<T> merge(T[] ts1, T[] ts2,
159: BinaryFunctor<T, T, Boolean> fn) {
160: return Merge.merge(ts1, ts2, fn);
161: }
162:
163: /**
164: * Returns the results of applying the given functor to each element in turn.
165: * @deprecated use Transform.transform(Iterable,UnaryFunctor) instead
166: */
167: static public <T, R> Iterable<R> transform(Iterable<? extends T> i,
168: UnaryFunctor<T, R> fn) {
169: return Transform.transform(i, fn);
170: }
171:
172: /**
173: * Returns the results of applying the given functor to each element in turn.
174: * @deprecated use Transform.transform(T[],UnaryFunctor) instead
175: */
176: static public <T, R> Iterable<R> transform(T[] ts,
177: UnaryFunctor<T, R> fn) {
178: return Transform.transform(ts, fn);
179: }
180:
181: /**
182: * Returns the results of applying the given functor to corresponding elements.
183: * @deprecated use Transform.transform(Iterable,Iterable,BinaryFunctor) instead
184: */
185: static public <T1, T2, R> Iterable<R> transform(
186: Iterable<? extends T1> i1, Iterable<? extends T2> i2,
187: BinaryFunctor<T1, T2, R> fn) {
188: return Transform.transform(i1, i2, fn);
189: }
190:
191: /**
192: * Returns the results of applying the given functor to corresponding elements.
193: * @deprecated use Transform.transform(T1[],T2[],BinaryFunctor) instead
194: */
195: static public <T1, T2, R> Iterable<R> transform(T1[] ts1, T2[] ts2,
196: BinaryFunctor<T1, T2, R> fn) {
197: return Transform.transform(ts1, ts2, fn);
198: }
199:
200: /**
201: * Returns the results of applying the given functor to succesive pairs of elements.
202: * @deprecated use Transform.transform(Iterable,BinaryFunctor) instead
203: */
204: static public <T, R> Iterable<R> transform(Iterable<? extends T> i,
205: BinaryFunctor<T, T, R> fn) {
206: return Transform.transform(i, fn);
207: }
208:
209: /**
210: * Returns the results of applying the given functor to succesive pairs of elements.
211: * @deprecated use Transform.transform(T[],BinaryFunctor) instead
212: */
213: static public <T, R> Iterable<R> transform(T[] ts,
214: BinaryFunctor<T, T, R> fn) {
215: return Transform.transform(ts, fn);
216: }
217:
218: /**
219: * Returns unduplicated results: will not return the same value twice in
220: * succession. This version uses T.equals() to test for equality.
221: * @deprecated use Unique.unique(Iterable) instead
222: */
223: static public <T> Iterable<T> unique(Iterable<? extends T> i) {
224: return Unique.unique(i);
225: }
226:
227: /**
228: * Returns unduplicated results: will not return the same value twice in
229: * succession. This version uses T.equals() to test for equality.
230: * @deprecated use Unique.unique(T[]) instead
231: */
232: static public <T> Iterable<T> unique(T[] ts) {
233: return Unique.unique(ts);
234: }
235:
236: /**
237: * Returns unduplicated results: will not return the same value twice in
238: * succession, as determined by the given predicate. The predicate should
239: * return true when the adjacent items are the same.
240: * @deprecated use Unique.unique(Iterable,BinaryFunctor) instead
241: */
242: static public <T> Iterable<T> unique(Iterable<? extends T> i,
243: BinaryFunctor<T, T, Boolean> eq) {
244: return Unique.unique(i, eq);
245: }
246:
247: /**
248: * Returns unduplicated results: will not return the same value twice in
249: * succession, as determined by the given predicate. The predicate should
250: * return true when the adjacent items are the same.
251: * @deprecated use Unique.unique(T[],BinaryFunctor) instead
252: */
253: static public <T> Iterable<T> unique(T[] ts,
254: BinaryFunctor<T, T, Boolean> eq) {
255: return Unique.unique(ts, eq);
256: }
257:
258: /**
259: * Adds all of the elements of the iterable to the collection. If
260: * necessary and possible, the collection will be enlarged: if enlarging
261: * the collection is not possible, then the runtime exception thrown.
262: * Augmentation of the Collection.addAll(Collection) API method.
263: * @deprecated use CollectionUtils.addAll
264: */
265: static public <T> boolean addAll(Collection<? super T> cout,
266: Iterable<T> iter) {
267: return CollectionUtils.addAll(cout, iter.iterator());
268: }
269:
270: /**
271: * Adds all of the elements of the array to the collection. If
272: * necessary and possible, the collection will be enlarged: if enlarging
273: * the collection is not possible, then the runtime exception thrown.
274: * Augmentation of the Collection.addAll(Collection) API method.
275: * @deprecated use CollectionUtils.addAll
276: */
277: static public <T> boolean addAll(Collection<? super T> cout, T[] ts) {
278: return CollectionUtils.addAll(cout, ts);
279: }
280:
281: /**
282: * Adds all of the elements of the iterable to the collection, and returns
283: * the collection. If necessary and possible, the collection will be enlarged:
284: * if enlarging the collection is not possible, then the runtime exception thrown.
285: * Augmentation of the Collection.addAll(Collection) API method.
286: * @deprecated use CollectionUtils.append
287: */
288: static public <T> Collection<? super T> append(
289: Collection<? super T> cout, Iterable<T> iter) {
290: return CollectionUtils.append(cout, iter.iterator());
291: }
292:
293: /**
294: * Adds all of the elements of the array to the collection and returns the
295: * collection. If necessary and possible, the collection will be enlarged:
296: * if enlarging the collection is not possible, then the runtime exception
297: * thrown. Augmentation of the Collection.addAll(Collection) API method.
298: * @deprecated use CollectionUtils.append
299: */
300: static public <T> Collection<? super T> append(
301: Collection<? super T> cout, T[] ts) {
302: return CollectionUtils.append(cout, ts);
303: }
304: }
|