|
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.bridge.SequenceAbstraction<E>
E
- The type of values delivered by this sequence. Note that the
values used by this sequence may generate any type S
that satisfies <S extends E>
.@Participant(value="Abstraction") public class SequenceAbstraction<E>
A sequence abstraction defers the creation of
sequence
values to a value
generator
.
The generator to use can either be supplied at
construction
time, or any time before sequence use via the
setGenerator(SequenceValueGenerator)
method. Any
sequence method expect copy
and setGenerator
will throw an IllegalStateException
if accessed
before a generator has been set! The toString(StringablePolicy)
method will only fail in case the methods described above is used by
the specified SequencePolicy
.
The size
,
ordered
, and
duplicate
properties of a
SequenceValueGenerator
makes it very easy to alter the
behaviour of a given sequence abstraction!
Implementation notes:
The separation between the abstraction and implementation allows the
abstraction to use any kind of implementation that delivers
a sub-type of the value delivered by the sequence. Here, the implementation
is only allowed to be set once, because changing a sequence while
traversing it seems illogical, but other Bridge implementations may benefit
from changing the implementation even after the abstraction is in use.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence |
---|
Sequence.State |
Field Summary | |
---|---|
protected E |
current
The current sequence value. |
protected SequenceValueGenerator<? extends E> |
generator
The generator used to
generate the next sequence value on an invocation of
next() . |
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence |
---|
state |
Constructor Summary | |
---|---|
SequenceAbstraction()
Constructor. |
|
SequenceAbstraction(SequenceAbstraction<E> sequence)
Copy constructor. |
|
SequenceAbstraction(SequenceValueGenerator<? extends E> generator)
Constructor, which creates this sequence abstraction to use the value generator
supplied as generator . |
Method Summary | |
---|---|
boolean |
bounded()
Returns true if this sequence is bounded, i.e. |
boolean |
consistent()
Returns true if this sequence is consistent, i.e. deliver the same values, in order, after restart or reset . |
SequenceAbstraction<E> |
copy()
Returns a copy of this sequence that will start at the same sequence index as this sequence. |
E |
current()
Returns the current element from this sequence. |
boolean |
isInitialised()
Returns true if a generator has been set for this sequence abstraction, false if not. |
E |
next()
Returns the next element from this sequence. |
void |
reset()
Resets this sequence to start over if it is consistent. |
SequenceAbstraction<E> |
setGenerator(SequenceValueGenerator<? extends E> generator)
Sets the generator to use
for this sequence to generator . |
Sequence.State |
state()
Returns the internal state of this sequence. |
CharSequence |
toString(StringablePolicy<? super Sequence<E>> policy)
Returns a char sequence representation of this stringable object using the format determined by policy or the
default policy
in case policy is null. |
boolean |
unique()
Returns true if this sequence deliver a given sequence value at most one time until reset or restarted. |
Methods inherited from class dk.rode.thesis.meta.model.AbstractSequence |
---|
getStringablePolicy, 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.strategy.Stringable |
---|
getStringablePolicy |
Field Detail |
---|
protected E current
Never null if this sequence has had a generator set.
@Participant(value="Implementation") protected SequenceValueGenerator<? extends E> generator
generator
used to
generate the next sequence value on an invocation of
next()
.
Supplied either at construction time, or set explicitly
using the setGenerator(SequenceValueGenerator)
method. Can be set at most once, i.e. the generator
cannot change once set, but must be set before this
sequence is valid and can be used.
Never empty, and never null for all practical purposes.
Constructor Detail |
---|
public SequenceAbstraction()
A value generator
must explicitly
be set using the setGenerator(SequenceValueGenerator)
method before this sequence is valid!
public SequenceAbstraction(SequenceAbstraction<E> sequence)
If sequence
does not have a value generator
set, a generator must explicitly
be set for this sequence using the
setGenerator(SequenceValueGenerator)
method before
this sequence is valid!
sequence
- The sequence abstraction to copy; cannot be null.
NullPointerException
- If sequence
is null.public SequenceAbstraction(@Participant(value="Implementation") SequenceValueGenerator<? extends E> generator)
generator
supplied as generator
.
The setGenerator(SequenceValueGenerator)
must not
be invoked for this sequence.
generator
- The generator to use; cannot be null.
NullPointerException
- If generator
is null.Method Detail |
---|
public final boolean bounded() throws IllegalStateException
Sequence
The same type of sequence may represent both bounded and
unbounded sequences and the behaviour is determined and fixed
at construction time.
Bounded sequences will restart if they deliver
consistent
values.
bounded
in interface Sequence<E>
IllegalStateException
- If a generator has not been set.Sequence.unique()
public final boolean consistent() throws IllegalStateException
Sequence
reset
.
Only bounded
consistent sequences will restart.
Consistent sequences need not deliver unique
sequence values.
Instances of the same type of sequences are always consistent or inconsistent; it is determined at design time, not construction time.
consistent
in interface Sequence<E>
IllegalStateException
- If a generator has not been set.public SequenceAbstraction<E> copy()
Sequence
copy
in interface Sequence<E>
copy
in interface Copyable<Sequence<E>>
public E current() throws IllegalStateException
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<E>
IllegalStateException
- If a generator has not been set.public boolean isInitialised()
If this sequence is not initialised, all sequence methods will throw an exception on use!
public E next() throws IllegalStateException
Sequence
next
in interface Sequence<E>
IllegalStateException
- If a generator has not been set.Sequence.current()
,
Sequence.state()
public void reset() throws IllegalStateException
Sequence
If this sequence is consistent
, the
sequence will restart.
reset
in interface Sequence<E>
reset
in class AbstractSequence<E>
IllegalStateException
- If a generator has not been set.public SequenceAbstraction<E> setGenerator(@Participant(value="Implementation") SequenceValueGenerator<? extends E> generator) throws IllegalStateException
generator
to use
for this sequence to generator
. A generator can only be set once, and cannot be changed once set. This sequence is not valid until a generator has been set.
generator
- The generator to use; cannot be null.
NullPointerException
- If generator
is null.
IllegalStateException
- If a generator has already been set.SequenceAbstraction(SequenceValueGenerator)
public Sequence.State state() throws IllegalStateException
Sequence
state
in interface Sequence<E>
state
in class AbstractSequence<E>
IllegalStateException
- If a generator has not been set.public CharSequence toString(StringablePolicy<? super Sequence<E>> policy)
Stringable
policy
or the
default
policy
in case policy
is null.
In Foo
, a typical implementation of this method
could be:
public CharSequence toString(StringablePolicy<? super Foo> policy) { return this.There are two approaches to formatting this stringable object into a char sequence representation:getStringablePolicy
(policy).toString
(this); }
policy
decide the entire format,
as in the Foo
example above; or
policy
to format part of the overall
representation, for example letting this method append
certain text regardless of the policy used.
StringablePolicy.toString(Object)
method, for
example in case multiple stringable objects should be formatted
into an overall representation.
In case an implementation uses the approach from bullet 2), care
must be take to respect the policy hints
so the overall format remains meaningful.
toString
in interface Stringable<Sequence<E>>
toString
in class AbstractSequence<E>
policy
- The policy to dictate the formatting; can be null, in
which case the result of toString
method
is returned.
StringablePolicy.Type
public boolean unique() throws IllegalStateException
Sequence
reset
or restarted.
Unbounded
sequences that are unique will never
deliver the same sequence value more than once.
The same type of sequence may represent both unique and not unique sequences and the behaviour is determined and fixed at construction time.
unique
in interface Sequence<E>
IllegalStateException
- If a generator has not been set.Sequence.consistent()
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |