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

dk.rode.thesis.state
Class AbstractStateableSequence<E>

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.state.AbstractStateableSequence<E>
Type Parameters:
E - The type of values delivered by this stateable sequence.
All Implemented Interfaces:
Sequence<E>, Copyable<Sequence<E>>, StrictCopyable<Sequence<E>>, StateableSequence<E>, Stringable<Sequence<E>>
Direct Known Subclasses:
ReversiblePrimeSequence

@Participant(value="Context")
public abstract class AbstractStateableSequence<E>
extends AbstractSequence<E>
implements StateableSequence<E>

An abstract stateable sequence implements the basic traits of a stateable sequence.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
private  FunctionalState<E> functionalState
          The current functional sequence state encapsulating the current behaviour of this sequence.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
protected AbstractStateableSequence()
          No-arg constructor.
protected AbstractStateableSequence(FunctionalState<E> functionalState)
          Constructor.
protected AbstractStateableSequence(StateableSequence<E> sequence)
          Copy constructor.
 
Method Summary
 E current()
          Returns the current element from this sequence.
 FunctionalState<E> getFunctionalState()
          Returns the functional state currently set for this stateable sequence if this sequence allows disclosure of such states.
 E next()
          Returns the next element from this sequence.
 void reset()
          Resets this sequence to start over if it is consistent.
 FunctionalState<E> setFunctionalState(FunctionalState<E> functionalState, Sequence.State internalState)
          Updates the functional state for this stateable sequence to functionalState, as well as the internal state to state, if supplied.
 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 dk.rode.thesis.meta.model.AbstractSequence
getStringablePolicy, state, 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
bounded, consistent, copy, state, unique
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy
 

Field Detail

functionalState

private FunctionalState<E> functionalState
The current functional sequence state encapsulating the current behaviour of this sequence.

Never null.

Constructor Detail

AbstractStateableSequence

protected AbstractStateableSequence()
No-arg constructor.

No initial functional state is set, and must be done in the sub-class constructor.


AbstractStateableSequence

protected AbstractStateableSequence(FunctionalState<E> functionalState)
Constructor.

Parameters:
functionalState - The initial functional state of this sequence; cannot be null.
Throws:
NullPointerException - If functionalState is null.

AbstractStateableSequence

protected AbstractStateableSequence(StateableSequence<E> sequence)
Copy constructor.

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

current

public final E current()
Description copied from interface: Sequence
Returns the current element from this sequence.

This method can be invoked even if Sequence.next() has not been invoked yet, thus delivering the initial value of this sequence.

Specified by:
current in interface Sequence<E>
Returns:
The current element; never null.

getFunctionalState

public FunctionalState<E> getFunctionalState()
Description copied from interface: StateableSequence
Returns the functional state currently set for this stateable sequence if this sequence allows disclosure of such states.

A functional state is not the same as an internal sequence state, but functional states will update the internal state.

Specified by:
getFunctionalState in interface StateableSequence<E>
Returns:
The current functional state; can be null if the this sequence does not allow functional states to be disclosed.
See Also:
StateableSequence.setFunctionalState(FunctionalState, Sequence.State)

next

public final E next()
Description copied from interface: Sequence
Returns the next element from this sequence.

Specified by:
next in interface Sequence<E>
Returns:
The next element; never null.
See Also:
Sequence.current(), Sequence.state()

reset

public final 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>

setFunctionalState

public FunctionalState<E> setFunctionalState(FunctionalState<E> functionalState,
                                             Sequence.State internalState)
Description copied from interface: StateableSequence
Updates the functional state for this stateable sequence to functionalState, as well as the internal state to state, if supplied.

A functional state is not the same as an internal sequence state, but functional states will update the internal state. After the functional sequence state has been set, its FunctionalState.action(Sequence.State, StateableSequence) method is invoked using the internal state supplied as argument.

Specified by:
setFunctionalState in interface StateableSequence<E>
Parameters:
functionalState - The functional state; cannot be null.
internalState - The internal state; can be null.
Returns:
The previous functional state; never null.
See Also:
StateableSequence.getFunctionalState()

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>>
Overrides:
toString in class AbstractSequence<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.