|
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.Objectdk.rode.thesis.meta.model.AbstractSequence<Integer>
dk.rode.thesis.meta.model.PrimeSequence
public class PrimeSequence
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.
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 |
|---|
private boolean calculate
private int index
next(). After each call to next has
completed, the value is prime + 1.
Range: [3, maximum].
private final int maximum
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 PrimeSequence(int maximum)
maximum - The maximum number to consider. Must be larger than 1.
IllegalArgumentException - If maximum is smaller than 2.public PrimeSequence(PrimeSequence sequence)
sequence - The 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 PrimeSequence copy()
Sequence
copy in interface Sequence<Integer>copy in interface Copyable<Sequence<Integer>>public Integer current()
Sequence
This method can be invoked even if Sequence.next()
has not been invoked yet, thus delivering the initial
value of this sequence.
current in interface Sequence<Integer>public Integer next()
Sequence
next in interface Sequence<Integer>Sequence.current(),
Sequence.state()public void reset()
Sequence
If this sequence is consistent, the
sequence will restart.
reset in interface Sequence<Integer>reset in class AbstractSequence<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 | ||||||||