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

dk.rode.thesis.composite
Class AbstractCompositeSequence<E>

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.composite.AbstractCompositeSequence<E>
Type Parameters:
E - The type of values delivered by this composite sequence.
All Implemented Interfaces:
CompositeSequence<E>, Sequence<E>, Copyable<Sequence<E>>, StrictCopyable<Sequence<E>>, Stringable<Sequence<E>>, Iterable<Sequence<? extends E>>
Direct Known Subclasses:
CharSequenceCompositeSequence, VisitableCompositeSequence

@Participant(value="Composite")
public abstract class AbstractCompositeSequence<E>
extends AbstractSequence<E>
implements CompositeSequence<E>

An abstract composite sequence implements the basic traits of the CompositeSequence interface.

This implementation is not thread-safe.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
protected  List<Sequence<? extends E>> sequences
          The sequences associated with this sequence, in order.
private  CompositeStrategy strategy
          The strategy used for traversal starting from this composite unless a strategy is specifically requested.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
AbstractCompositeSequence()
          No-arg constructor.
AbstractCompositeSequence(CompositeSequence<? extends E> sequence)
          Copy constructor, which also copies associated sequences.
AbstractCompositeSequence(CompositeStrategy strategy, Sequence<? extends E>... sequences)
          Constructor.
 
Method Summary
 boolean addSequence(Sequence<? extends E> sequence)
          Associates the sequence supplied as sequence to this composite sequence, if not already.
 boolean bounded()
          Returns true if this sequence is bounded, i.e.
protected  void calculateNextState()
          Calculates a "meaningful" state based on all associated sequences: if all are reset, this sequence is reset.
 boolean consistent()
          Returns true if this sequence is consistent, i.e. deliver the same values, in order, after restart or reset.
 CompositeStrategy getCompositeStrategy()
          Return the current strategy used by this composite sequence to traverse the associated sequence graph.
 List<Sequence<? extends E>> getSequences()
          Returns the sequences currently associated with this composite sequence, in order.
<V extends Sequence<?>>
List<V>
getSequences(Class<V> clazz)
          Returns the sequences currently associated with this composite sequence, in order, assignable to the type supplied as clazz.
 List<Sequence<? extends E>> getSequences(CompositeStrategy strategy, boolean includeSelf, Boolean type)
          Returns the sequences currently reachable from this composite sequence, in the order specified by strategy.
<V extends Sequence<?>>
List<V>
getSequences(CompositeStrategy strategy, boolean includeSelf, Boolean type, Class<V> clazz)
          Returns the sequences currently reachable from this composite sequence, in the order specified by strategy.
 Iterator<Sequence<? extends E>> iterator()
           
 boolean removeSequence(Sequence<?> sequence)
          Removes the sequence supplied as sequence from this composite sequence, if associated.
 void reset()
          Resets this sequence to start over if it is consistent.
 void setCompositeStrategy(CompositeStrategy policy)
          Sets the current strategy used by this composite sequence to traverse the associated sequence graph to strategy.
 int size()
          Returns the number of sequences associated to this composite sequence.
 boolean unique()
          Returns false.
 
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence
getStringablePolicy, state, toString, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.meta.model.Sequence
copy, current, next, state
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Field Detail

sequences

protected final List<Sequence<? extends E>> sequences
The sequences associated with this sequence, in order.

Is a random access list for fast traversal.

Never null, but can be null.


strategy

private CompositeStrategy strategy
The strategy used for traversal starting from this composite unless a strategy is specifically requested.

Never null.

Constructor Detail

AbstractCompositeSequence

public AbstractCompositeSequence()
No-arg constructor.

The CompositeStrategy.DEFAULT strategy is used for traversal.


AbstractCompositeSequence

public AbstractCompositeSequence(CompositeSequence<? extends E> sequence)
Copy constructor, which also copies associated sequences.

Parameters:
sequence - The composite sequence to copy; cannot be null.
Throws:
NullPointerException - If sequence is null.

AbstractCompositeSequence

public AbstractCompositeSequence(CompositeStrategy strategy,
                                 Sequence<? extends E>... sequences)
Constructor.

Parameters:
strategy - The traversal strategy; can be null, in which case CompositeStrategy.DEFAULT is used.
sequences - Sequences to add to this composite, if any; can be null. Null entries are ignored.
Method Detail

addSequence

public boolean addSequence(Sequence<? extends E> sequence)
Description copied from interface: CompositeSequence
Associates the sequence supplied as sequence to this composite sequence, if not already.

If already added, this method does nothing.

Specified by:
addSequence in interface CompositeSequence<E>
Parameters:
sequence - The sequence to add; cannot be null.
Returns:
True if sequence was added, false if not, i.e. if already added.
See Also:
CompositeSequence.removeSequence(Sequence)

bounded

public boolean bounded()
Description copied from interface: Sequence
Returns true if this sequence is bounded, i.e. deliver values between the initial sequence value and some upper bound.

The same type of sequence may represent both bounded and unbounded sequences and the behaviour is determined and fixed at construction time. Bounded sequences will restart if they deliver consistent values.

Specified by:
bounded in interface Sequence<E>
Returns:
True if bounded, false if not.
See Also:
Sequence.unique()

calculateNextState

protected void calculateNextState()
Calculates a "meaningful" state based on all associated sequences: if all are reset, this sequence is reset. Otherwise it functions normally.

Sub-classes should invoke this method in their next() implementation after the individual sequences have been advanced.


consistent

public boolean consistent()
Description copied from interface: Sequence
Returns true if this sequence is consistent, i.e. deliver the same values, in order, after restart or reset.

Only bounded consistent sequences will restart. Consistent sequences need not deliver unique sequence values.

Instances of the same type of sequences are always consistent or inconsistent; it is determined at design time, not construction time.

Specified by:
consistent in interface Sequence<E>
Returns:
True if consistent, false if not.

getCompositeStrategy

public CompositeStrategy getCompositeStrategy()
Description copied from interface: CompositeSequence
Return the current strategy used by this composite sequence to traverse the associated sequence graph.

Specified by:
getCompositeStrategy in interface CompositeSequence<E>
Returns:
The strategy; never null.

getSequences

public List<Sequence<? extends E>> getSequences()
Description copied from interface: CompositeSequence
Returns the sequences currently associated with this composite sequence, in order.

Modifying the returned collection will not affect this sequence.

Specified by:
getSequences in interface CompositeSequence<E>
Returns:
The collection of associated sequences; never null, but can be empty.

getSequences

public <V extends Sequence<?>> List<V> getSequences(Class<V> clazz)
Description copied from interface: CompositeSequence
Returns the sequences currently associated with this composite sequence, in order, assignable to the type supplied as clazz.

Invoking this method with the Sequence interface corresponds to invoking CompositeSequence.getSequences().

Modifying the returned collection will not affect this sequence.

Specified by:
getSequences in interface CompositeSequence<E>
Type Parameters:
V - The type of applicable sequences.
Parameters:
clazz - The (super-)class of sequences to include; cannot be null.
Returns:
The collection of associated sequences of the type supplied as clazz; never null, but can be empty.

getSequences

public List<Sequence<? extends E>> getSequences(CompositeStrategy strategy,
                                                boolean includeSelf,
                                                Boolean type)
Description copied from interface: CompositeSequence
Returns the sequences currently reachable from this composite sequence, in the order specified by strategy.

This sequence is included in the returned list if includeSelf is true and it matches type; type determines the types of sequences to include in the returned list:

If strategy is null, the default strategy for this sequence is used.

Modifying the returned collection will not affect this sequence.

Specified by:
getSequences in interface CompositeSequence<E>
Parameters:
strategy - The strategy dictating the traversal; can be null.
includeSelf - True to include this composite sequence in the returned list (if it matches type), false to exclude.
type - The type of sequences to include in the returned list; can be null.
Returns:
The list of reachable sequences; never null, but can be empty.

getSequences

public <V extends Sequence<?>> List<V> getSequences(CompositeStrategy strategy,
                                                    boolean includeSelf,
                                                    Boolean type,
                                                    Class<V> clazz)
Description copied from interface: CompositeSequence
Returns the sequences currently reachable from this composite sequence, in the order specified by strategy. Each sequence included in the returned list is assignable to the type supplied as clazz.

This sequence is included in the returned list if it matches clazz and type; type determines the types of sequences to include in the returned list:

If strategy is null, the default strategy for this sequence is used.

Modifying the returned collection will not affect this sequence.

Specified by:
getSequences in interface CompositeSequence<E>
Type Parameters:
V - The type of applicable sequences.
Parameters:
strategy - The strategy dictating the traversal; can be null.
includeSelf - True to include this composite sequence in the returned list (if it matches type and clazz), false to exclude.
type - The type of sequences to include in the returned list; can be null.
clazz - The (super-)class of sequences to include; cannot be null.
Returns:
The collection of reachable sequences of the type supplied as clazz; never null, but can be empty.

iterator

public Iterator<Sequence<? extends E>> iterator()
Specified by:
iterator in interface Iterable<Sequence<? extends E>>

removeSequence

public boolean removeSequence(Sequence<?> sequence)
Description copied from interface: CompositeSequence
Removes the sequence supplied as sequence from this composite sequence, if associated.

If not associated, this method does nothing.

Specified by:
removeSequence in interface CompositeSequence<E>
Parameters:
sequence - The sequence to remove; cannot be null.
Returns:
True if sequence was removed, false if not, i.e. if not previously added.
See Also:
CompositeSequence.addSequence(Sequence)

reset

public void reset()
Description copied from interface: Sequence
Resets this sequence to start over if it is consistent.

If this sequence is consistent, the sequence will restart.

Specified by:
reset in interface Sequence<E>
Overrides:
reset in class AbstractSequence<E>

setCompositeStrategy

public void setCompositeStrategy(CompositeStrategy policy)
Description copied from interface: CompositeSequence
Sets the current strategy used by this composite sequence to traverse the associated sequence graph to strategy.

Specified by:
setCompositeStrategy in interface CompositeSequence<E>
Parameters:
policy - The strategy; can be null, in which case the default strategy is used.

size

public int size()
Description copied from interface: CompositeSequence
Returns the number of sequences associated to this composite sequence.

Specified by:
size in interface CompositeSequence<E>
Returns:
The number of sequences.

unique

public boolean unique()
Returns false.

Specified by:
unique in interface Sequence<E>
Returns:
False.
See Also:
Sequence.consistent()

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.