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

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

public class FiniteSequenceIterable<T>
extends TruncatedIterable<T>

An iterable representing a finite sequence. The sequence is defined by an initial value and a successor function (described by a Lambda), along with a size that truncates the (otherwise infinite) list. This is a trivial extension to TruncatedIterable, but does add optimized implementations of size() and hasFixedSize() (since the nature of the nested iterable is known).

See Also:
Serialized Form

Field Summary
 
Fields inherited from class edu.rice.cs.plt.iter.TruncatedIterable
_size
 
Constructor Summary
FiniteSequenceIterable(T initial, Lambda<? super T,? extends T> successor, int size)
           
 
Method Summary
 boolean hasFixedSize()
          true if this iterable is known to have a fixed size.
 boolean isStatic()
          Always false: results of a lambda may be arbitrary.
static
<T> FiniteSequenceIterable<T>
make(T initial, Lambda<? super T,? extends T> successor, int size)
          Call the constructor (allows T to be inferred)
static
<T> FiniteSequenceIterable<T>
makeCopies(T value, int copies)
          Create a sequence containing copies instances of the given value.
static FiniteSequenceIterable<Integer> makeIntegerSequence(int start, int end)
          Create a simple sequence containing the numbers between start and end (inclusive).
static
<T> SnapshotIterable<T>
makeSnapshot(T initial, Lambda<? super T,? extends T> successor, int size)
          Create a FiniteSequenceIterable and wrap it in a SnapshotIterable, forcing immediate evaluation of the sequence.
 int size()
          Return size, unless the nested iterable is smaller than size; in that case, returns the iterable's size.
 int size(int bound)
          Compute the number of elements in the iterable, up to the given bound.
 
Methods inherited from class edu.rice.cs.plt.iter.TruncatedIterable
compositeHeight, compositeSize, isEmpty, isInfinite, iterator, make, makeSnapshot
 
Methods inherited from class edu.rice.cs.plt.iter.AbstractIterable
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FiniteSequenceIterable

public FiniteSequenceIterable(T initial,
                              Lambda<? super T,? extends T> successor,
                              int size)
Parameters:
initial - The first value in the sequence
successor - A function that, given the nth sequence value, produces the n+1st value
size - The number of elements in the sequence
Throws:
IllegalArgumentException - If size is less than 0
Method Detail

size

public int size()
Description copied from class: TruncatedIterable
Return size, unless the nested iterable is smaller than size; in that case, returns the iterable's size.

Specified by:
size in interface SizedIterable<T>
Overrides:
size in class TruncatedIterable<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>
Overrides:
size in class TruncatedIterable<T>
Parameters:
bound - Maximum result. Assumed to be nonnegative.

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>
Overrides:
hasFixedSize in class TruncatedIterable<T>

isStatic

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

Specified by:
isStatic in interface SizedIterable<T>
Overrides:
isStatic in class TruncatedIterable<T>

make

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


makeSnapshot

public static <T> SnapshotIterable<T> makeSnapshot(T initial,
                                                   Lambda<? super T,? extends T> successor,
                                                   int size)
Create a FiniteSequenceIterable and wrap it in a SnapshotIterable, forcing immediate evaluation of the sequence.


makeIntegerSequence

public static FiniteSequenceIterable<Integer> makeIntegerSequence(int start,
                                                                  int end)
Create a simple sequence containing the numbers between start and end (inclusive). start may be less than or greater than end (or even equal to it); the resulting iterator will increment or decrement as necessary.


makeCopies

public static <T> FiniteSequenceIterable<T> makeCopies(T value,
                                                       int copies)
Create a sequence containing copies instances of the given value.