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

dk.rode.thesis.meta.model
Class PrimeSequence

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

public class PrimeSequence
extends AbstractSequence<Integer>
implements Sequence<Integer>

A prime sequence returns with each call to next() the next prime number in the sequence of prime numbers smaller than maximum, where maximum is supplied at construction time. Hence, returned values are in the range of [2, maximum].

A prime sequence is bounded, consistent, and unique.

Implementation notes:
Though this class is supposed to simulate complex behaviour that has to be calculated on the fly to illustrate the use of sequences, the implementation uses a very simple version of the Sieve of Eratosthenes. The amount of memory used by an a prime sequence is linear with maximum. Java has built-in support for generating primes in form of java.math.BigInteger, but that is beside the point.

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

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
private  boolean calculate
          Returns false if all primes have been calculated, false if not.
private  int index
          The next index to start processing from on calls to next().
private  int maximum
          Maximum number to consider.
private  int prime
          The current prime number.
private  boolean[] sieve
          The actual sieve.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
PrimeSequence(int maximum)
          Constructor.
PrimeSequence(PrimeSequence sequence)
          Copy constructor.
 
Method Summary
 boolean bounded()
          Returns true.
 boolean consistent()
          Returns true.
 PrimeSequence 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 true.
 
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

calculate

private boolean calculate
Returns false if all primes have been calculated, false if not.


index

private int index
The next index to start processing from on calls to next(). After each call to next has completed, the value is prime + 1.

Range: [3, maximum].


maximum

private final int maximum
Maximum number to consider.


prime

private int prime
The current prime number.

Range: [2, maximum].


sieve

private boolean[] sieve
The actual sieve.

If calculate is true, an index smaller than index having a false value represents a prime number (normally a true value represent a prime number, but by flipping it, we do not have to initialise the array with true).

If calculate is false, any index having a false value represents a prime number.

The length is maximum + 1. Index zero and one are unused.

Constructor Detail

PrimeSequence

public PrimeSequence(int maximum)
Constructor.

Parameters:
maximum - The maximum number to consider. Must be larger than 1.
Throws:
IllegalArgumentException - If maximum is smaller than 2.

PrimeSequence

public PrimeSequence(PrimeSequence 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 true.

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

copy

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

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