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

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

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.meta.model.ArraySequence<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:
CountdownSequence, DigitSequence, EnglishAlphabetSequence, HexDigitSequence, NorwegianAlphabetSequence, Sentence, SimpsonsFamilySequence, SmileySequence, SymbolSequence, Word

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

A simple abstract array sequence to deliver all the entries in a given array, in order. An invocation of next() returns the next element in the array.

Sub-classes need only supply the actual array on construction.

An array sequence is bounded, consistent, and unless otherwise specified unique().

Array sequences are by default equal to any array sequence of the same type using an equivalent array that is at the same sequence index.

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  E[] elements
          The array used.
protected  int index
          The current index.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
protected ArraySequence(ArraySequence<E> sequence)
          Copy constructor.
protected ArraySequence(E... array)
          Constructor, which creates this sequence to use the array supplied as array.
 
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.
 E current()
          Returns the current element from this sequence.
 boolean equals(Object object)
          Returns true if object is an array sequence having the same type as this array sequence, using an equivalent array, and is at the same sequence index, false if not.
 int hashCode()
          Returns the hash code of this sequence.
 E next()
          Returns the next element from this sequence.
 void reset()
          Resets this sequence to start over if it is consistent.
 int size()
          Returns the number of elements in this array sequence.
 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, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.meta.model.Sequence
copy, state
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Field Detail

elements

protected final E[] elements
The array used.

Never null, and never empty.


index

protected int index
The current index.

Invariant: 0 <= index < elements.length

Constructor Detail

ArraySequence

protected ArraySequence(ArraySequence<E> sequence)
Copy constructor.

The array is not copied, but used as is. This sequence starts at the current index of sequence.

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

ArraySequence

protected ArraySequence(E... array)
Constructor, which creates this sequence to use the array supplied as array.

The array is not copied, but used as is.

Parameters:
array - The array; cannot be null or empty.
Throws:
NullPointerException - If array is null.
IllegalArgumentException - If array is empty.
Method Detail

bounded

public final 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, as the array used by this sequence is finite.
See Also:
size()

consistent

public final 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, as the array used by this sequence is finite.

current

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

equals

public boolean equals(Object object)
Returns true if object is an array sequence having the same type as this array sequence, using an equivalent array, and is at the same sequence index, false if not.

Overrides:
equals in class Object
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

hashCode

public int hashCode()
Returns the hash code of this sequence.

Overrides:
hashCode in class Object
Returns:
The hash code.

next

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

size

public int size()
Returns the number of elements in this array sequence.

Returns:
The number of elements.

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<E>
Returns:
True.
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.