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

PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
java.lang.Object dk.rode.thesis.meta.model.AbstractSequence<E> dk.rode.thesis.state.AbstractStateableSequence<Integer> dk.rode.thesis.state.ReversiblePrimeSequence
@Participant(value="Context") public class ReversiblePrimeSequence
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
builtin support for generating primes in form of
java.math.BigInteger
, but that is
beside the point.
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 

private boolean calculate
ReversiblePrimeSequence.PrimeState.Calculate
private int index
next()
. After each call to next has
completed, the value is prime
+ 1 (forward) or
prime
 1 (reverse).
Range: [2, maximum
].
private int maximum
Will be adjusted to the last prime number on first restart.
private int prime
Range: [2, maximum
].
private boolean[] 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 

public ReversiblePrimeSequence(int maximum)
maximum
 The maximum number to consider. Must be larger than 1.
IllegalStateException
 If maximum
is smaller than 2.public ReversiblePrimeSequence(ReversiblePrimeSequence sequence)
sequence
 The reversible prime sequence to copy; cannot be null.
NullPointerException
 If sequence
is null.Method Detail 

public boolean bounded()
bounded
in interface Sequence<Integer>
Sequence.unique()
public boolean consistent()
consistent
in interface Sequence<Integer>
public ReversiblePrimeSequence copy()
ReversibleSequence
copy
in interface ReversibleSequence<Integer>
copy
in interface Sequence<Integer>
copy
in interface Copyable<Sequence<Integer>>
public int maximum()
The returned value is not guaranteed to be equal to the maximum value supplied at construction time.
public Integer reverse()
ReversibleSequence
reversible
.
The current
value is not
affected by this method, and is returned by this method.
reverse
in interface ReversibleSequence<Integer>
IllegalStateException
 If this sequence cannot be
reversed at this point.public boolean reversible()
ReversibleSequence
If this method returns true, it is safe to invoke
reverse()
. If it returns false, an illegal
state exception will be thrown by reverse()
.
reversible
in interface ReversibleSequence<Integer>
public boolean unique()
unique
in interface Sequence<Integer>
Sequence.consistent()

Gunni Rode / rode.dk  
PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 