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

dk.rode.thesis.meta.model
Class AbstractSequence<E>

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
Type Parameters:
E - The type of values delivered by this sequence.
All Implemented Interfaces:
Sequence<E>, Copyable<Sequence<E>>, StrictCopyable<Sequence<E>>, Stringable<Sequence<E>>
Direct Known Subclasses:
AbstractCompositeSequence, AbstractStateableSequence, AckermannSequence, AnnotatedObserversSequence, ArraySequence, FibonacciSequence, LongSequence, PrimeSequence, RandomSequence, RangeSequence, SequenceAbstraction, SequenceObserversSequence, SequenceTemplate, StepSequenceImpl

public abstract class AbstractSequence<E>
extends Object
implements Sequence<E>

An abstract sequence implements the basic traits of the Sequence interface.

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  Sequence.State state
          The internal state.
 
Constructor Summary
protected AbstractSequence()
          No-arg constructor, which reset this sequence and then initialise the internal state to Sequence.State.START.
protected AbstractSequence(boolean reset)
          Constructor, which reset this sequence if reset is true and then initialises the internal state to Sequence.State.START.
protected AbstractSequence(Sequence<? super E> sequence)
          Copy constructor.
 
Method Summary
 StringablePolicy<? super Sequence<E>> getStringablePolicy(StringablePolicy<? super Sequence<E>> policy)
          Always return a non-null policy: policy is not null: policy is returned.
 void reset()
          Resets this sequence to start over if it is consistent.
 Sequence.State state()
          Returns the internal state of this sequence.
 String toString()
          Returns the default string representation specified by the toString(StringablePolicy) method.
 CharSequence toString(StringablePolicy<? super Sequence<E>> policy)
          Returns a char sequence representation of this stringable object using the format determined by policy or the default policy in case policy is null.
 
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
bounded, consistent, copy, current, next, unique
 

Field Detail

state

protected Sequence.State state
The internal state.

Never null.

Constructor Detail

AbstractSequence

protected AbstractSequence()
No-arg constructor, which reset this sequence and then initialise the internal state to Sequence.State.START.

Note, that this sequence may be reset before completely initialised!


AbstractSequence

protected AbstractSequence(boolean reset)
Constructor, which reset this sequence if reset is true and then initialises the internal state to Sequence.State.START.

Note, that this sequence can be reset before completely initialised!

Parameters:
reset - True to reset.

AbstractSequence

protected AbstractSequence(Sequence<? super E> sequence)
Copy constructor.

The state of sequence is set for this sequence.

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

getStringablePolicy

public StringablePolicy<? super Sequence<E>> getStringablePolicy(StringablePolicy<? super Sequence<E>> policy)
Description copied from interface: Stringable
Always return a non-null policy:

  1. policy is not null: policy is returned.

  2. policy is null: a default, non-null policy is returned.

Specified by:
getStringablePolicy in interface Stringable<Sequence<E>>
Parameters:
policy - The supplied policy; can be null.
Returns:
The policy to use; never null.
See Also:
Stringable.toString(StringablePolicy)

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>

state

public Sequence.State state()
Description copied from interface: Sequence
Returns the internal state of this sequence.

Specified by:
state in interface Sequence<E>
Returns:
The internal state; never null.

toString

public String toString()
Returns the default string representation specified by the toString(StringablePolicy) method.

Overrides:
toString in class Object
Returns:
The string representation; never null.

toString

public CharSequence toString(StringablePolicy<? super Sequence<E>> policy)
Description copied from interface: Stringable
Returns a char sequence representation of this stringable object using the format determined by policy or the default policy in case policy is null.

In Foo, a typical implementation of this method could be:

    public CharSequence toString(StringablePolicy<? super Foo> policy) {
      return this.getStringablePolicy(policy).toString(this);
    }
 
There are two approaches to formatting this stringable object into a char sequence representation:

  1. Let policy decide the entire format, as in the Foo example above; or

  2. Use policy to format part of the overall representation, for example letting this method append certain text regardless of the policy used.

Bullet 1) is not always applicable because a given policy implementation may not have access to all required information in its StringablePolicy.toString(Object) method, for example in case multiple stringable objects should be formatted into an overall representation.

In case an implementation uses the approach from bullet 2), care must be take to respect the policy hints so the overall format remains meaningful.

Specified by:
toString in interface Stringable<Sequence<E>>
Parameters:
policy - The policy to dictate the formatting; can be null, in which case the result of toString method is returned.
Returns:
The char sequence representation; never null.
See Also:
StringablePolicy.Type

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.