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

dk.rode.thesis.state
Class StepSequenceImpl

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<Integer>
      extended by dk.rode.thesis.state.StepSequenceImpl
All Implemented Interfaces:
Sequence<Integer>, Copyable<Sequence<Integer>>, StrictCopyable<Sequence<Integer>>, StepSequence, Stringable<Sequence<Integer>>
Direct Known Subclasses:
EvenSequence, OddSequence

@Participant(value="State")
abstract class StepSequenceImpl
extends AbstractSequence<Integer>
implements StepSequence

An abstract class representing the basic traits of the StepSequence interface.

This class has two sub-classes: one that handles even sequence values, and one that handles odd values. It is possible to "flip" between the two types using the flip() method.

Implementation notes:
This class could implement almost all sequence functionality, but that is left to the sub-classes to better illustrate that an different target is used once flipped.

Author:
Gunni Rode / rode.dk

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.state.StepSequence
StepSequence.Factory
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
(package private)  int current
          The current value of this sequence.
private  int start
          The initial value of this sequence.
(package private)  int step
          The step value to increase the sequence value with each call to next().
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
StepSequenceImpl(int step, int value)
          Constructor.
StepSequenceImpl(StepSequenceImpl sequence)
          Copy constructor.
 
Method Summary
 boolean bounded()
          Returns true if this sequence is bounded, i.e.
 boolean consistent()
          Returns true if this sequence is consistent, i.e. deliver the same values, in order, after restart or reset.
(package private) abstract  StepSequenceImpl flip()
          Flips this sequence from even to odd or vice versa.
 void reset()
          Resets this sequence to start over if it is consistent.
 boolean unique()
          Returns true if this sequence deliver a given sequence value at most one time until reset or restarted.
 
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

current

int current
The current value of this sequence.


start

private final int start
The initial value of this sequence.


step

final int step
The step value to increase the sequence value with each call to next().

Never zero, but can be negative.

Constructor Detail

StepSequenceImpl

StepSequenceImpl(int step,
                 int value)
Constructor.

Parameters:
step - The step; cannot be zero.
value - The initial sequence value.
Throws:
IllegalArgumentException - If step is zero.
See Also:
StepSequence.Factory

StepSequenceImpl

StepSequenceImpl(StepSequenceImpl sequence)
Copy constructor.

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

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<Integer>
Returns:
True if bounded, false if not.
See Also:
Sequence.unique()

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<Integer>
Returns:
True if consistent, false if not.

flip

abstract StepSequenceImpl flip()
Flips this sequence from even to odd or vice versa.

Returns:
A flipped instance; never null.

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<Integer>
Overrides:
reset in class AbstractSequence<Integer>

unique

public boolean unique()
Description copied from interface: Sequence
Returns true if this sequence deliver a given sequence value at most one time until reset or restarted.

Unbounded sequences that are unique will never deliver the same sequence value more than once.

The same type of sequence may represent both unique and not unique sequences and the behaviour is determined and fixed at construction time.

Specified by:
unique in interface Sequence<Integer>
Returns:
True if unique, false if not.
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.