net.sf.jga.util
Class Iterables

java.lang.Object
  extended by net.sf.jga.util.Iterables

public class Iterables
extends java.lang.Object

Facade for the Iterators in this package, supporting the new forloop syntax. Only iterators that perform their service during the hasNext()/next() sequence are given in this facade. Iterators that augment to the basic Iterator interface, (e.g., CachingIterator) are not included.

Copyright © 2004 David A. Hall

Author:
David A. Hall

Constructor Summary
Iterables()
           
 
Method Summary
static
<T> boolean
addAll(java.util.Collection<? super T> c, java.lang.Iterable<T> iter)
          Adds all of the elements of the iterable to the collection.
static
<T> boolean
addAll(java.util.Collection<? super T> c, T[] ts)
          Adds all of the elements of the array to the collection.
static
<T> java.lang.Iterable<T>
filter(java.lang.Iterable<? extends T> i, UnaryFunctor<T,java.lang.Boolean> pred)
          Returns only elements for which the predicate is true
static
<T> java.lang.Iterable<T>
filter(T[] ts, UnaryFunctor<T,java.lang.Boolean> pred)
          Returns only elements for which the predicate is true
static
<T> java.lang.Iterable<java.util.Iterator<? extends T>>
findAll(java.lang.Iterable<? extends T> i, UnaryFunctor<java.util.Iterator<? extends T>,? extends java.util.Iterator<T>> fn)
          Returns iterators that point to the next instance in an iterable that meets the condition described in the functor.
static
<T> java.lang.Iterable<java.util.Iterator<? extends T>>
findAll(T[] ts, UnaryFunctor<java.util.Iterator<? extends T>,? extends java.util.Iterator<T>> fn)
          Returns iterators that point to the next instance in an iterable that meets the condition described in the functor.
static
<T extends java.lang.Comparable<T>>
java.lang.Iterable<T>
merge(java.lang.Iterable<? extends T> i1, java.lang.Iterable<? extends T> i2)
          Returns all elements of both iterables, always choosing the lesser of the two current elements.
static
<T> java.lang.Iterable<T>
merge(java.lang.Iterable<? extends T> i1, java.lang.Iterable<? extends T> i2, BinaryFunctor<T,T,java.lang.Boolean> fn)
          Returns all elements of both iterables, using the given predicate to choose which element to return.
static
<T> java.lang.Iterable<T>
merge(java.lang.Iterable<? extends T> i1, java.lang.Iterable<? extends T> i2, java.util.Comparator<T> comp)
          Returns all elements of both iterables, always choosing the lesser of the two current elements.
static
<T extends java.lang.Comparable<T>>
java.lang.Iterable<T>
merge(T[] ts1, T[] ts2)
          Returns all elements of both arrays, always choosing the lesser of the two current elements.
static
<T> java.lang.Iterable<T>
merge(T[] ts1, T[] ts2, BinaryFunctor<T,T,java.lang.Boolean> fn)
          Returns all elements of both arrays, using the given predicate to choose which element to return.
static
<T> java.lang.Iterable<T>
merge(T[] ts1, T[] ts2, java.util.Comparator<T> comp)
          Returns all elements of both arrays, always choosing the lesser of the two current elements.
static
<T,R> java.lang.Iterable<R>
transform(java.lang.Iterable<? extends T> i, BinaryFunctor<T,T,R> fn)
          Returns the results of applying the given functor to succesive pairs of elements.
static
<T,R> java.lang.Iterable<R>
transform(java.lang.Iterable<? extends T> i, UnaryFunctor<T,R> fn)
          Returns the results of applying the given functor to each element in turn.
static
<T1,T2,R> java.lang.Iterable<R>
transform(java.lang.Iterable<? extends T1> i1, java.lang.Iterable<? extends T2> i2, BinaryFunctor<T1,T2,R> fn)
          Returns the results of applying the given functor to corresponding elements.
static
<T,R> java.lang.Iterable<R>
transform(T[] ts, BinaryFunctor<T,T,R> fn)
          Returns the results of applying the given functor to succesive pairs of elements.
static
<T,R> java.lang.Iterable<R>
transform(T[] ts, UnaryFunctor<T,R> fn)
          Returns the results of applying the given functor to each element in turn.
static
<T1,T2,R> java.lang.Iterable<R>
transform(T1[] ts1, T2[] ts2, BinaryFunctor<T1,T2,R> fn)
          Returns the results of applying the given functor to corresponding elements.
static
<T> java.lang.Iterable<T>
unique(java.lang.Iterable<? extends T> i)
          Returns unduplicated results: will not return the same value twice in succession.
static
<T> java.lang.Iterable<T>
unique(java.lang.Iterable<? extends T> i, BinaryFunctor<T,T,java.lang.Boolean> eq)
          Returns unduplicated results: will not return the same value twice in succession, as determined by the given predicate.
static
<T> java.lang.Iterable<T>
unique(T[] ts)
          Returns unduplicated results: will not return the same value twice in succession.
static
<T> java.lang.Iterable<T>
unique(T[] ts, BinaryFunctor<T,T,java.lang.Boolean> eq)
          Returns unduplicated results: will not return the same value twice in succession, as determined by the given predicate.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Iterables

public Iterables()
Method Detail

findAll

public static <T> java.lang.Iterable<java.util.Iterator<? extends T>> findAll(java.lang.Iterable<? extends T> i,
                                                                              UnaryFunctor<java.util.Iterator<? extends T>,? extends java.util.Iterator<T>> fn)
Returns iterators that point to the next instance in an iterable that meets the condition described in the functor.


findAll

public static <T> java.lang.Iterable<java.util.Iterator<? extends T>> findAll(T[] ts,
                                                                              UnaryFunctor<java.util.Iterator<? extends T>,? extends java.util.Iterator<T>> fn)
Returns iterators that point to the next instance in an iterable that meets the condition described in the functor.


filter

public static <T> java.lang.Iterable<T> filter(java.lang.Iterable<? extends T> i,
                                               UnaryFunctor<T,java.lang.Boolean> pred)
Returns only elements for which the predicate is true


filter

public static <T> java.lang.Iterable<T> filter(T[] ts,
                                               UnaryFunctor<T,java.lang.Boolean> pred)
Returns only elements for which the predicate is true


merge

public static <T extends java.lang.Comparable<T>> java.lang.Iterable<T> merge(java.lang.Iterable<? extends T> i1,
                                                                              java.lang.Iterable<? extends T> i2)
Returns all elements of both iterables, always choosing the lesser of the two current elements.


merge

public static <T extends java.lang.Comparable<T>> java.lang.Iterable<T> merge(T[] ts1,
                                                                              T[] ts2)
Returns all elements of both arrays, always choosing the lesser of the two current elements.


merge

public static <T> java.lang.Iterable<T> merge(java.lang.Iterable<? extends T> i1,
                                              java.lang.Iterable<? extends T> i2,
                                              java.util.Comparator<T> comp)
Returns all elements of both iterables, always choosing the lesser of the two current elements.


merge

public static <T> java.lang.Iterable<T> merge(T[] ts1,
                                              T[] ts2,
                                              java.util.Comparator<T> comp)
Returns all elements of both arrays, always choosing the lesser of the two current elements.


merge

public static <T> java.lang.Iterable<T> merge(java.lang.Iterable<? extends T> i1,
                                              java.lang.Iterable<? extends T> i2,
                                              BinaryFunctor<T,T,java.lang.Boolean> fn)
Returns all elements of both iterables, using the given predicate to choose which element to return. If the predicate is true, choose the current element of the first iterable, otherwise choose the current element of the second iterable. When one is exhausted, returns elements remaining in the other.


merge

public static <T> java.lang.Iterable<T> merge(T[] ts1,
                                              T[] ts2,
                                              BinaryFunctor<T,T,java.lang.Boolean> fn)
Returns all elements of both arrays, using the given predicate to choose which element to return. If the predicate is true, choose the current element of the first iterable, otherwise choose the current element of the second iterable. When one is exhausted, returns elements remaining in the other.


transform

public static <T,R> java.lang.Iterable<R> transform(java.lang.Iterable<? extends T> i,
                                                    UnaryFunctor<T,R> fn)
Returns the results of applying the given functor to each element in turn.


transform

public static <T,R> java.lang.Iterable<R> transform(T[] ts,
                                                    UnaryFunctor<T,R> fn)
Returns the results of applying the given functor to each element in turn.


transform

public static <T1,T2,R> java.lang.Iterable<R> transform(java.lang.Iterable<? extends T1> i1,
                                                        java.lang.Iterable<? extends T2> i2,
                                                        BinaryFunctor<T1,T2,R> fn)
Returns the results of applying the given functor to corresponding elements.


transform

public static <T1,T2,R> java.lang.Iterable<R> transform(T1[] ts1,
                                                        T2[] ts2,
                                                        BinaryFunctor<T1,T2,R> fn)
Returns the results of applying the given functor to corresponding elements.


transform

public static <T,R> java.lang.Iterable<R> transform(java.lang.Iterable<? extends T> i,
                                                    BinaryFunctor<T,T,R> fn)
Returns the results of applying the given functor to succesive pairs of elements.


transform

public static <T,R> java.lang.Iterable<R> transform(T[] ts,
                                                    BinaryFunctor<T,T,R> fn)
Returns the results of applying the given functor to succesive pairs of elements.


unique

public static <T> java.lang.Iterable<T> unique(java.lang.Iterable<? extends T> i)
Returns unduplicated results: will not return the same value twice in succession. This version uses T.equals() to test for equality.


unique

public static <T> java.lang.Iterable<T> unique(T[] ts)
Returns unduplicated results: will not return the same value twice in succession. This version uses T.equals() to test for equality.


unique

public static <T> java.lang.Iterable<T> unique(java.lang.Iterable<? extends T> i,
                                               BinaryFunctor<T,T,java.lang.Boolean> eq)
Returns unduplicated results: will not return the same value twice in succession, as determined by the given predicate. The predicate should return true when the adjacent items are the same.


unique

public static <T> java.lang.Iterable<T> unique(T[] ts,
                                               BinaryFunctor<T,T,java.lang.Boolean> eq)
Returns unduplicated results: will not return the same value twice in succession, as determined by the given predicate. The predicate should return true when the adjacent items are the same.


addAll

public static <T> boolean addAll(java.util.Collection<? super T> c,
                                 java.lang.Iterable<T> iter)
Adds all of the elements of the iterable to the collection. If necessary and possible, the collection will be enlarged: if enlarging the collection is not possible, then the runtime exception thrown. Augmentation of the Collection.addAll(Collection) API method.


addAll

public static <T> boolean addAll(java.util.Collection<? super T> c,
                                 T[] ts)
Adds all of the elements of the array to the collection. If necessary and possible, the collection will be enlarged: if enlarging the collection is not possible, then the runtime exception thrown. Augmentation of the Collection.addAll(Collection) API method.



Copyright © 2002-2005 David A. Hall. All Rights Reserved.