|
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<Integer> dk.rode.thesis.memento.RangeSequence
@Participant(value="Originator") public class RangeSequence
A range sequence represents a positive Integer
value
that is increased with each call to next()
, which
can be reversed
to deliver previous values
as well unless unbounded.
A start value can be supplied (default is zero), and an integer sequence can also be found by supplying an end value. Default range sequences are unbounded.
It can have its state restored either by using the Java Serialization
mechanism, or by explicitly using mementos
.
A range sequence may be bounded
,
but is consistent
and
unique
.
Implementation notes:
The mementos created are GuardedSequenceMemento
instances. However,
regular mementos can still be set. This could easily be remedied by an
instanceof
check, though.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence |
---|
Sequence.State |
Field Summary | |
---|---|
private boolean |
direction
True to return the next higher sequence value ("forward"), false to return previous values ("reverse") |
private int |
end
End number of this sequence. |
private int |
sequence
The current sequence number. |
(package private) static long |
serialVersionUID
The serial version. |
private int |
start
Start number of this sequence. |
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence |
---|
state |
Constructor Summary | |
---|---|
RangeSequence()
No-arg constructor. |
|
RangeSequence(int start)
Constructor. |
|
RangeSequence(int start,
int end)
Constructor. |
|
RangeSequence(RangeSequence sequence)
Copy constructor. |
Method Summary | |
---|---|
boolean |
bounded()
If an end value was supplied at construction time, this sequence is bounded, and true is returned. |
boolean |
consistent()
Returns true. |
RangeSequence |
copy()
Returns a copy of this reversible sequence that will start at the same sequence index as this sequence in the same direction. |
Integer |
current()
Returns the current element from this sequence. |
SequenceMemento<Integer> |
getMemorizableState()
Returns a sequence memento representing the current state of this sequence. |
Integer |
next()
Returns the next element from this sequence. |
private void |
readObject(ObjectInputStream in)
Deserializes this memorizable sequence. |
void |
reset()
Resets this sequence to start over if it is consistent. |
Integer |
reverse()
Reverses this reversible sequence, if currently reversible . |
boolean |
reversible()
Returns true if this range sequence is bounded ,
false otherwise. |
void |
setMemorizableState(SequenceMemento<Integer> memento)
Updates this sequence to the state supplied as memento . |
boolean |
unique()
Returns true. |
private static void |
validate(int start,
int end)
Validates the start and end values supplied as start and end , respectively. |
private void |
writeObject(ObjectOutputStream out)
Serializes this memorizable range sequence. |
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 direction
private int end
Invariant: end >= start
>= 0
private int sequence
static final long serialVersionUID
private int start
Invariant: start >= 0
Constructor Detail |
---|
public RangeSequence()
This sequence starts at 0 and is unbound, i.e. infinite.
public RangeSequence(int start)
start
and is
unbound, i.e. infinite.
start
- The start value of this sequence; must be >= 0.
IllegalArgumentException
- If start
is negative.public RangeSequence(int start, int end)
start
and ends
at end
. This sequence is thus bounded.
start
- The start value of this sequence; must be >= 0.end
- The end value of this sequence; must be >= start
.
IllegalArgumentException
- If start
is negative,
or if end
is smaller than start
.public RangeSequence(RangeSequence 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 RangeSequence copy()
ReversibleSequence
copy
in interface ReversibleSequence<Integer>
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 SequenceMemento<Integer> getMemorizableState()
MemorizableSequence
getMemorizableState
in interface MemorizableSequence<Integer>
public Integer next()
Sequence
next
in interface Sequence<Integer>
Sequence.current()
,
Sequence.state()
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
To protect against malicious attacks, all read attributes are validated.
in
- The input stream to read from; never null.
IOException
- In case the deserialization fails.
ClassNotFoundException
- If this class could not be loaded.validate(int, int)
public void reset()
Sequence
If this sequence is consistent
, the
sequence will restart.
reset
in interface Sequence<Integer>
reset
in class AbstractSequence<Integer>
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()
bounded
,
false otherwise.
reversible
in interface ReversibleSequence<Integer>
public void setMemorizableState(SequenceMemento<Integer> memento) throws MemorizableException
MemorizableSequence
memento
.
setMemorizableState
in interface MemorizableSequence<Integer>
memento
- The memento supplying the state; cannot be null.
MemorizableException
- If the update fails, e.g. illegal
memento state for this sequence, or illegal memento type.public boolean unique()
unique
in interface Sequence<Integer>
Sequence.consistent()
private static void validate(int start, int end)
start
and end
, respectively. Returns silently upon success, or throws an exception otherwise.
start
- The start value; must be >= 0.end
- The end value; must be >= start
if not
infinite.
IllegalArgumentException
- If start
is negative,
or if end
is smaller than start
.RangeSequence(int, int)
,
readObject(ObjectInputStream)
private void writeObject(ObjectOutputStream out) throws IOException
The internal state is also serialized.
out
- The output stream to write to; never null.
IOException
- In case the serialization fails.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |