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

dk.rode.thesis.state
Class ReversiblePrimeSequence

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.state.AbstractStateableSequence<Integer>
          extended by dk.rode.thesis.state.ReversiblePrimeSequence
All Implemented Interfaces:
ReversibleSequence<Integer>, Sequence<Integer>, Copyable<Sequence<Integer>>, StrictCopyable<Sequence<Integer>>, StateableSequence<Integer>, Stringable<Sequence<Integer>>
Direct Known Subclasses:
VisitableReversiblePrimeSequence

@Participant(value="Context")
public class ReversiblePrimeSequence
extends AbstractStateableSequence<Integer>
implements ReversibleSequence<Integer>

A reversible 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, or a previously calculated prime number with each call to next() after reverse() has been invoked. Hence, returned values are in range of [2, maximum].

A reversible prime sequence is bounded, consistent, and unique.

Implementation notes:
A very simple version of the Sieve of Eratosthenes algorithm is used in this implementation. The amount of memory used by an a reversible prime sequence is linear with maximum. The general idea behind the implementation of the algorithm is identical with the standard PrimeSequence implementation, but the internal sequence design differs considerable between the two except for required attributes. Hence, it really makes no sense to inherit PrimeSequence here. 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

Nested Class Summary
private static class ReversiblePrimeSequence.PrimeState
          A prime state represents a stateless functional state for all reversible prime sequences, while the actual internal state and attributes are stored in a given reversible prime sequence instance.
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
private  boolean calculate
          True 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
ReversiblePrimeSequence(int maximum)
          Constructor.
ReversiblePrimeSequence(ReversiblePrimeSequence sequence)
          Copy constructor.
 
Method Summary
 boolean bounded()
          Returns true.
 boolean consistent()
          Returns true.
 ReversiblePrimeSequence copy()
          Returns a copy of this reversible sequence that will start at the same sequence index as this sequence in the same direction.
 int maximum()
          The maximum prime considered by this sequence.
 Integer reverse()
          Reverses this reversible sequence, if currently reversible.
 boolean reversible()
          Returns true if this reversible sequence can be reversed in its current state, false if not.
 boolean unique()
          Returns true.
 
Methods inherited from class dk.rode.thesis.state.AbstractStateableSequence
current, getFunctionalState, next, reset, setFunctionalState, toString
 
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence
getStringablePolicy, state, 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
current, next, reset, state
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Field Detail

calculate

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

See Also:
ReversiblePrimeSequence.PrimeState.Calculate

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 (forward) or prime - 1 (reverse).

Range: [2, maximum].


maximum

private int maximum
Maximum number to consider.

Will be adjusted to the last prime number on first restart.


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

ReversiblePrimeSequence

public ReversiblePrimeSequence(int maximum)
Constructor.

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

ReversiblePrimeSequence

public ReversiblePrimeSequence(ReversiblePrimeSequence sequence)
Copy constructor.

Parameters:
sequence - The reversible prime 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 ReversiblePrimeSequence copy()
Description copied from interface: ReversibleSequence
Returns a copy of this reversible sequence that will start at the same sequence index as this sequence in the same direction.

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

maximum

public int maximum()
The maximum prime considered by this sequence.

The returned value is not guaranteed to be equal to the maximum value supplied at construction time.

Returns:
The maximum prime considered.

reverse

public Integer reverse()
Description copied from interface: ReversibleSequence
Reverses this reversible sequence, if currently reversible.

The current value is not affected by this method, and is returned by this method.

Specified by:
reverse in interface ReversibleSequence<Integer>
Returns:
The current sequence value; never null.
Throws:
IllegalStateException - If this sequence cannot be reversed at this point.

reversible

public boolean reversible()
Description copied from interface: ReversibleSequence
Returns true if this reversible sequence can be reversed in its current state, false if not.

If this method returns true, it is safe to invoke reverse(). If it returns false, an illegal state exception will be thrown by reverse().

Specified by:
reversible in interface ReversibleSequence<Integer>
Returns:
True if this reversible sequence can be reversed in its current state, false if not.

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.