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

dk.rode.thesis.facade
Class RandomSequence

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<Integer>
      extended by dk.rode.thesis.facade.RandomSequence
All Implemented Interfaces:
Sequence<Integer>, Copyable<Sequence<Integer>>, StrictCopyable<Sequence<Integer>>, Stringable<Sequence<Integer>>
Direct Known Subclasses:
UnboundedRandomSequence, VisitableRandomSequence

@Participant(value="SubsystemClass")
public class RandomSequence
extends AbstractSequence<Integer>
implements Sequence<Integer>

A random sequence returns a pseudo integer random number with each call to next() between [0, maximum], where maximum is supplied at construction time.

A random sequence is bounded, but inconsistent and not unique.

Author:
Gunni Rode / rode.dk
See Also:
UnboundedRandomSequence

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
private  int maximum
          The maximum random number delivered by this sequence.
private  Random random
          The random generator used.
private  int sequence
          The current random sequence number.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
RandomSequence(int maximum)
          Constructor.
RandomSequence(RandomSequence sequence)
          Copy constructor.
 
Method Summary
 boolean bounded()
          Returns true.
 boolean consistent()
          Returns false.
 RandomSequence copy()
          Returns a copy of this sequence that will start at the same sequence index as this sequence.
 Integer current()
          Returns the current element from this sequence.
 Integer next()
          Returns the next element from this sequence.
 void reset()
          Resets this sequence to start over if it is consistent.
 boolean unique()
          Returns false.
 
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
state
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Field Detail

maximum

private final int maximum
The maximum random number delivered by this sequence.


random

private final Random random
The random generator used.

Never null.


sequence

private int sequence
The current random sequence number.

Range: [0, maximum].

Constructor Detail

RandomSequence

public RandomSequence(int maximum)
Constructor.

Parameters:
maximum - The maximum random value. Must be larger than 1.
Throws:
IllegalArgumentException - If max is smaller than 1.

RandomSequence

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

Specified by:
bounded in interface Sequence<Integer>
Returns:
True.
See Also:
Sequence.unique()

consistent

public boolean consistent()
Returns false.

Specified by:
consistent in interface Sequence<Integer>
Returns:
False.

copy

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

Specified by:
copy in interface Sequence<Integer>
Specified by:
copy in interface Copyable<Sequence<Integer>>
Returns:
A copy of this sequence; never null.

current

public Integer 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<Integer>
Returns:
The current element; never null.

next

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

Specified by:
next in interface Sequence<Integer>
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<Integer>
Overrides:
reset in class AbstractSequence<Integer>

unique

public boolean unique()
Returns false.

Specified by:
unique in interface Sequence<Integer>
Returns:
False.
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.