Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.iterator
Class IterableSequence<E>

java.lang.Object
  extended by dk.rode.thesis.decorator.SequenceDecorator<E>
      extended by dk.rode.thesis.iterator.IterableSequence<E>
Type Parameters:
E - The type of values delivered by this iterable sequence.
All Implemented Interfaces:
Sequence<E>, Copyable<Sequence<E>>, StrictCopyable<Sequence<E>>, Stringable<Sequence<E>>, Iterable<E>

@ParticipantUsage(value="Aggregate",
                  type=java.lang.Iterable.class)
@Participant(value="ConcreteAggregate")
public class IterableSequence<E>
extends SequenceDecorator<E>
implements Sequence<E>, Iterable<E>

An iterable sequence is a bounded sequence that can be accessed as any other iterable object.

When iterating, the iterable sequence will be advanced! To avoid modification of the sequence, simply copy it before use.

Example usage:

   // Highest number to consider as a prime number...
   int n = 100;
   // Create a bounded sequence to deliver primes...
   PrimeSequence sequence = new PrimeSequence(n);
   
   // Create iterable sequence based on "sequence"...
   IterableSequence<Integer> is = new IterableSequence<Integer>(sequence);
   
   // Print all primes between 2 and n (implicit usage of iterator())...
   for (int prime : is) {
     LogFactory.getLog().println("Prime: ", prime);
   }
 
An iterable sequence is not thread-safe.

Author:
Gunni Rode / rode.dk
See Also:
SequenceIterator, ProcessableSequence

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Constructor Summary
IterableSequence(Sequence<E> sequence)
          Constructor, which creates this iterable sequence to iterate through the sequence values from sequence, starting from its current value.
 
Method Summary
 IterableSequence<E> copy()
          Returns a copy of this sequence that will start at the same sequence index as this sequence.
 Iterator<E> iterator()
          Returns an iterator to iterate over this sequence.
 
Methods inherited from class dk.rode.thesis.decorator.SequenceDecorator
bounded, consistent, current, equals, getSequence, getStringablePolicy, hashCode, next, reset, state, toString, toString, unique
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.meta.model.Sequence
bounded, consistent, current, next, reset, state, unique
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Constructor Detail

IterableSequence

public IterableSequence(Sequence<E> sequence)
Constructor, which creates this iterable sequence to iterate through the sequence values from sequence, starting from its current value.

sequence must be bounded and is not altered by this constructor, but copied before returned by the iterator() method. If not bounded, iterator() will thrown an exception.

Parameters:
sequence - The sequence to make iterable; cannot be null.
Throws:
NullPointerException - If sequence is null.
IllegalArgumentException - If sequence is unbounded.
Method Detail

copy

public IterableSequence<E> copy()
Description copied from interface: Sequence
Returns a copy of this sequence that will start at the same sequence index as this sequence.

Specified by:
copy in interface Sequence<E>
Specified by:
copy in interface Copyable<Sequence<E>>
Returns:
A copy of this sequence; never null.

iterator

public Iterator<E> iterator()
Returns an iterator to iterate over this sequence.

The returned iterator will advance this sequence because this sequence is iterable!

Specified by:
iterator in interface Iterable<E>
Returns:
A new iterator; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.