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

dk.rode.thesis.templatemethod
Class NegativeSequence

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.templatemethod.SequenceTemplate<Integer,Integer>
          extended by dk.rode.thesis.templatemethod.NegativeSequence
All Implemented Interfaces:
Sequence<Integer>, Copyable<Sequence<Integer>>, StrictCopyable<Sequence<Integer>>, Stringable<Sequence<Integer>>, Closeable

@Participant(value="ConcreteClass")
public class NegativeSequence
extends SequenceTemplate<Integer,Integer>

A negative sequence represents a perhaps bounded integer value that is decreased with each call to next(), starting at minus one.

A negative sequence is unbounded unless a specific minimum number is supplied at construction time, but is unique and consistent.

Implementation notes:
A negative sequence quite possibly represents the simplest implementation of a sequence template, and it could easily have been implemented in a fashion like the LongSequence class. Note that no explicit class attribute is required to store the sequence value! The use of the sequence template here is only to illustrate its use.

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  int minimum
          The minimum value of this sequence.
 
Fields inherited from class dk.rode.thesis.templatemethod.SequenceTemplate
POLICY
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
NegativeSequence()
          Constructor, which creates this negative sequence as unbounded.
NegativeSequence(int minimum)
          Constructor, which creates this negative sequence as bounded.
NegativeSequence(NegativeSequence sequence)
          Copy constructor.
 
Method Summary
 boolean bounded()
          Returns true if a minimum sequence value were supplied at construction time, false if not.
 NegativeSequence copy()
          Returns a copy of this sequence that will start at the same sequence index as this sequence.
protected  Integer nextValue(Integer key, Integer current)
          This abstract operation creates the next sequence value to be delivered.
protected  Integer nextValueOpen(Integer current)
          Prepares this sequence for the next sequence value to be delivered based on the current value supplied as current.
protected  boolean sequenceOpen(Object... arguments)
          Performs sub-class specific initialisation of this sequence before sequence values will be delivered, and returns true upon success.
 
Methods inherited from class dk.rode.thesis.templatemethod.SequenceTemplate
close, consistent, current, ensureOpen, finalize, index, isClosed, next, nextValueClose, reset, sequenceClose, sequenceReset, size, unique
 
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence
getStringablePolicy, state, toString, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

minimum

private int minimum
The minimum value of this sequence.

A value of zero means no minimum, i.e. unbounded.

Constructor Detail

NegativeSequence

public NegativeSequence()
Constructor, which creates this negative sequence as unbounded.


NegativeSequence

public NegativeSequence(int minimum)
Constructor, which creates this negative sequence as bounded.

Parameters:
minimum - The minimum value of this sequence; cannot be positive.
Throws:
IllegalArgumentException - If minimum is positive.

NegativeSequence

public NegativeSequence(NegativeSequence sequence)
Copy constructor.

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

bounded

public boolean bounded()
Returns true if a minimum sequence value were supplied at construction time, false if not.

Specified by:
bounded in interface Sequence<Integer>
Overrides:
bounded in class SequenceTemplate<Integer,Integer>
Returns:
True if a minimum value was supplied at construction time, false if not.
See Also:
Sequence.unique()

copy

public NegativeSequence copy()
Description copied from interface: Sequence
Returns a copy of this sequence that will start at the same sequence index as this sequence.

Returns:
A copy of this sequence; never null.

nextValue

protected Integer nextValue(Integer key,
                            Integer current)
Description copied from class: SequenceTemplate
This abstract operation creates the next sequence value to be delivered. This is step 2/3 in value creation, where the previous step was nextValueOpen(E) and the last is nextValueClose(K, E).

Specified by:
nextValue in class SequenceTemplate<Integer,Integer>
Parameters:
key - The correlation key just created by nextValueOpen(E); can be null.
current - The current (soon to be previous) sequence value; never null unless initialising this sequence (once).
Returns:
The next sequence value, which will be set as the new current value once SequenceTemplate.next() returns; null means sequence restart, which is only allowed if this sequence is bounded.

nextValueOpen

protected Integer nextValueOpen(Integer current)
Description copied from class: SequenceTemplate
Prepares this sequence for the next sequence value to be delivered based on the current value supplied as current. This is step 1/3 in value creation, where the next steps are nextValue(K, E) and nextValueClose(K, E).

The returned correlation key will be used to identify the same creation process in subsequent calls to nextValue and nextValueClose.

Specified by:
nextValueOpen in class SequenceTemplate<Integer,Integer>
Parameters:
current - The current (soon to be previous) sequence value; never null unless initialising this sequence (once).
Returns:
The correlation key to identify the next sequence value; can be null if not used.

sequenceOpen

protected boolean sequenceOpen(Object... arguments)
                        throws Exception
Description copied from class: SequenceTemplate
Performs sub-class specific initialisation of this sequence before sequence values will be delivered, and returns true upon success. In case the initialisation could not be performed, this method returns false, or alternatively throws an exception. If the initialisation fails, this sequences is considered closed.

Initialisation is performed upon construction of new sequences and when copying existing sequences, if needed. The arguments supplied to this (abstract) operation is the varargs arguments supplied at construction time (e.g. via sub-class implementations).

Specified by:
sequenceOpen in class SequenceTemplate<Integer,Integer>
Parameters:
arguments - Arguments required by the initialisation, if any; never null, but can be empty.
Returns:
True if the initialisation succeeded, false if it failed.
Throws:
IllegalArgumentException - If the minimum sequence value is positive.
Exception - If the super sequence open method throws an exception.
See Also:
SequenceTemplate.sequenceReset(), SequenceTemplate.sequenceClose()

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.