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

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

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

public class ProcessableSequence<E>
extends SequenceDecorator<E>
implements Sequence<E>

A processable sequence represents an internal iterator that can iterate over bounded sequences using a value processor to perform the actual processing of delivered sequence values. The processing will advance the sequence.

The iteration is performed using the process(ValueProcessor) method. The processing will stop when the processable sequence has no more values, or in case the processor returns false.

The processing performed by a processable sequence is not thread-safe.

Implementation notes:
This implementation uses the Decorator and Strategy patterns as opposed to inheritance: the processing is delegated to a ValueProcessor supplied at iteration time, and hence this iterator need not be sub-classed. The internal iterator example provided by Gamma et al. uses inheritance, but Gamma et al. note that it might be feasible to parameterise the iterator with the processing operation in languages with different features than those offered by C++ [Gamma95, p.267-269].

This is a specialised case of using composition instead of inheritance. More specifically, the internal iterators presented by Gamma et al. can be viewed as variants of the Template Method pattern, and the use of the Template Method pattern can thus be replaced by composition, providing all required information is supplied as arguments.

The is no interface in Java to support internal iterators.

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

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Constructor Summary
ProcessableSequence(Sequence<E> sequence)
          Constructor, which creates this processable sequence to process through the sequence values from sequence.
 
Method Summary
 ProcessableSequence<E> copy()
          Returns a copy of this sequence that will start at the same sequence index as this sequence.
 boolean process(ValueProcessor<? super E> processor)
          Process values in this sequence using the processor supplied as processor until all values have been exhausted, or processor returns false.
 
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

ProcessableSequence

public ProcessableSequence(Sequence<E> sequence)
Constructor, which creates this processable sequence to process through the sequence values from sequence.

Before each time this sequence is processed, it will be reset.

Modifying sequence externally will affect this iterator.

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 ProcessableSequence<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.

process

public boolean process(ValueProcessor<? super E> processor)
Process values in this sequence using the processor supplied as processor until all values have been exhausted, or processor returns false.

This sequence will be reset before the processing is performed, and the processor initialised.

The processing will advance this sequence.

Parameters:
processor - The value processor to use; cannot be null.
Returns:
True if all sequence values were processed, false if processor terminated the iteration by returning false.
Throws:
NullPointerException - If processor is 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.