edu.rice.cs.plt.iter
Class SequenceIterable<T>

java.lang.Object
  extended by edu.rice.cs.plt.iter.SequenceIterable<T>
All Implemented Interfaces:
SizedIterable<T>, Serializable, Iterable<T>

public class SequenceIterable<T>
extends Object
implements SizedIterable<T>, Serializable

An iterable representing an infinite sequence. The sequence is defined by an initial value and a successor function (described by a Lambda).

Note that the infinite nature of this list makes it impossible to use the standard equals and hashCode implementations (in AbstractIterable). Care must also be taken in invoking many iterable-handling methods that assume finite length, such as those in IterUtil.

See Also:
Serialized Form

Constructor Summary
SequenceIterable(T initial, Lambda<? super T,? extends T> successor)
           
 
Method Summary
 boolean equals(Object o)
          Returns true iff o is a SequenceIterable with the same initial value and successor function (according to equals)
 boolean hasFixedSize()
          true if this iterable is known to have a fixed size.
 int hashCode()
           
 boolean isEmpty()
          Whether the iterable does not contain any elements.
 boolean isInfinite()
          true if the iterable is known to have infinite size.
 boolean isStatic()
          Always false: results of a lambda may be arbitrary.
 SequenceIterator<T> iterator()
          Create a new SequenceIterator based on this iterable's parameters
static
<T> SequenceIterable<T>
make(T initial, Lambda<? super T,? extends T> successor)
          Call the constructor (allows T to be inferred)
 int size()
          Compute the number of elements in the iterable.
 int size(int bound)
          Compute the number of elements in the iterable, up to the given bound.
 String toString()
          Defers to IterUtil.toString(java.lang.Iterable)
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SequenceIterable

public SequenceIterable(T initial,
                        Lambda<? super T,? extends T> successor)
Parameters:
initial - The first value in the sequence
successor - A function that, given the nth sequence value, produces the n+1st value
Method Detail

iterator

public SequenceIterator<T> iterator()
Create a new SequenceIterator based on this iterable's parameters

Specified by:
iterator in interface Iterable<T>

isEmpty

public boolean isEmpty()
Description copied from interface: SizedIterable
Whether the iterable does not contain any elements.

Specified by:
isEmpty in interface SizedIterable<T>

size

public int size()
Description copied from interface: SizedIterable
Compute the number of elements in the iterable. If the size is infinite or too large to be represented as an int, Integer.MAX_VALUE should be returned. Otherwise, next() may be safely invoked on the iterator exactly this number of times.

Specified by:
size in interface SizedIterable<T>

size

public int size(int bound)
Description copied from interface: SizedIterable
Compute the number of elements in the iterable, up to the given bound. If the size is infinite or greater than bound, bound is returned.

Specified by:
size in interface SizedIterable<T>
Parameters:
bound - Maximum result. Assumed to be nonnegative.

isInfinite

public boolean isInfinite()
Description copied from interface: SizedIterable
true if the iterable is known to have infinite size. If true, an iterator over the iterable in its current state will never return false from hasNext().

Specified by:
isInfinite in interface SizedIterable<T>

hasFixedSize

public boolean hasFixedSize()
Description copied from interface: SizedIterable
true if this iterable is known to have a fixed size. This is the case if the iterable is immutable, or if changes can only replace values, not remove or add them. An infinite iterable may be fixed if it is guaranteed to never become finite.

Specified by:
hasFixedSize in interface SizedIterable<T>

isStatic

public boolean isStatic()
Always false: results of a lambda may be arbitrary.

Specified by:
isStatic in interface SizedIterable<T>

toString

public String toString()
Defers to IterUtil.toString(java.lang.Iterable)

Overrides:
toString in class Object

equals

public boolean equals(Object o)
Returns true iff o is a SequenceIterable with the same initial value and successor function (according to equals)

Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

make

public static <T> SequenceIterable<T> make(T initial,
                                           Lambda<? super T,? extends T> successor)
Call the constructor (allows T to be inferred)