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

dk.rode.thesis.observer
Class AnnotatedObserversSequenceDecorator<E>

java.lang.Object
  extended by dk.rode.thesis.meta.model.AbstractSequence<E>
      extended by dk.rode.thesis.observer.AnnotatedObserversSequence<E>
          extended by dk.rode.thesis.observer.AnnotatedObserversSequenceDecorator<E>
Type Parameters:
E - The type of values delivered by this sequence.
All Implemented Interfaces:
Sequence<E>, Observable<Object>, ObservableSequence<Object,Sequence.State,E>, SequenceObserver<Sequence.State>, Copyable<Sequence<E>>, StrictCopyable<Sequence<E>>, Stringable<Sequence<E>>

@Participant(value="ConcreteSubject")
public class AnnotatedObserversSequenceDecorator<E>
extends AnnotatedObserversSequence<E>

An annotated observers sequence decorator decorates any Sequence to become an observable sequence that uses the Executor annotation to identify notification methods for observers that accepts a Sequence type as the first argument and a Sequence.State type as the second. The context type must furthermore be a type the decorated sequence type is assignable to.

Implementation notes:
Because Java does not support functional multiple inheritance, this decorator cannot inherit both SequenceDecorator and AnnotatedObserversSequence. Hence, all Sequence methods have to be implemented explicitly in this class, while the observable functionality is inherited from AnnotatedObserversSequence.

Author:
Gunni Rode / rode.dk
See Also:
Executor

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
private  Sequence<E> sequence
          The decorated sequence.
 
Fields inherited from class dk.rode.thesis.meta.model.AbstractSequence
state
 
Constructor Summary
AnnotatedObserversSequenceDecorator(AnnotatedObserversSequenceDecorator<E> sequence)
          Copy constructor.
AnnotatedObserversSequenceDecorator(Sequence<E> sequence)
          Constructor.
 
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.
 AnnotatedObserversSequenceDecorator<E> copy()
          Returns a deep copy of this object.
 E current()
          Returns the current element from this sequence.
protected  E doNext()
          Hook for sub-classes to perform the actual next() operation without notifying observers.
 boolean equals(Object object)
           
 StringablePolicy<? super Sequence<E>> getStringablePolicy(StringablePolicy<? super Sequence<E>> policy)
          Always return a non-null policy: policy is not null: policy is returned.
 int hashCode()
           
 void reset()
          Resets this sequence to start over if it is consistent.
 Sequence.State state()
          Returns the internal state of this sequence.
 String toString()
          Returns the default string representation specified by the AbstractSequence.toString(StringablePolicy) method.
 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.observer.AnnotatedObserversSequence
addObserver, getObservers, next, removeObserver, sequenceEvent
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

sequence

private final Sequence<E> sequence
The decorated sequence.

Never null.

Constructor Detail

AnnotatedObserversSequenceDecorator

public AnnotatedObserversSequenceDecorator(AnnotatedObserversSequenceDecorator<E> sequence)
Copy constructor.

Parameters:
sequence - The observable sequence to copy; cannot be null.
Throws:
NullPointerException - If sequence is null.

AnnotatedObserversSequenceDecorator

public AnnotatedObserversSequenceDecorator(Sequence<E> sequence)
Constructor.

Parameters:
sequence - The sequence to decorate; cannot be null.
Throws:
NullPointerException - If sequence is null.
Method Detail

bounded

public boolean bounded()
Description copied from interface: Sequence
Returns true if this sequence is bounded, i.e. deliver values between the initial sequence value and some upper bound.

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.

Returns:
True if bounded, false if not.
See Also:
Sequence.unique()

consistent

public boolean consistent()
Description copied from interface: Sequence
Returns true if this sequence is consistent, i.e. deliver the same values, in order, after restart or 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.

Returns:
True if consistent, false if not.

copy

public AnnotatedObserversSequenceDecorator<E> copy()
Description copied from interface: Copyable
Returns a deep copy of this object.

Returns:
The copy; never null.

current

public E current()
Description copied from interface: Sequence
Returns the current element from this sequence.

This method can be invoked even if Sequence.next() has not been invoked yet, thus delivering the initial value of this sequence.

Returns:
The current element; never null.

doNext

protected E doNext()
Description copied from class: AnnotatedObserversSequence
Hook for sub-classes to perform the actual next() operation without notifying observers.

Specified by:
doNext in class AnnotatedObserversSequence<E>
Returns:
The next element; never null.

equals

public boolean equals(Object object)
Overrides:
equals in class Object

getStringablePolicy

public StringablePolicy<? super Sequence<E>> getStringablePolicy(StringablePolicy<? super Sequence<E>> policy)
Description copied from interface: Stringable
Always return a non-null policy:

  1. policy is not null: policy is returned.

  2. policy is null: a default, non-null policy is returned.

Specified by:
getStringablePolicy in interface Stringable<Sequence<E>>
Overrides:
getStringablePolicy in class AbstractSequence<E>
Parameters:
policy - The supplied policy; can be null.
Returns:
The policy to use; never null.
See Also:
Stringable.toString(StringablePolicy)

hashCode

public int hashCode()
Overrides:
hashCode in class Object

reset

public void reset()
Description copied from interface: Sequence
Resets this sequence to start over if it is consistent.

If this sequence is consistent, the sequence will restart.

Specified by:
reset in interface Sequence<E>
Overrides:
reset in class AbstractSequence<E>

state

public Sequence.State state()
Description copied from interface: Sequence
Returns the internal state of this sequence.

Specified by:
state in interface Sequence<E>
Overrides:
state in class AbstractSequence<E>
Returns:
The internal state; never null.

toString

public String toString()
Description copied from class: AbstractSequence
Returns the default string representation specified by the AbstractSequence.toString(StringablePolicy) method.

Overrides:
toString in class AbstractSequence<E>
Returns:
The string representation; never null.

toString

public CharSequence toString(StringablePolicy<? super Sequence<E>> policy)
Description copied from interface: Stringable
Returns a char sequence representation of this stringable object using the format determined by 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.getStringablePolicy(policy).toString(this);
    }
 
There are two approaches to formatting this stringable object into a char sequence representation:

  1. Let policy decide the entire format, as in the Foo example above; or

  2. Use policy to format part of the overall representation, for example letting this method append certain text regardless of the policy used.

Bullet 1) is not always applicable because a given policy implementation may not have access to all required information in its 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.

Specified by:
toString in interface Stringable<Sequence<E>>
Overrides:
toString in class AbstractSequence<E>
Parameters:
policy - The policy to dictate the formatting; can be null, in which case the result of toString method is returned.
Returns:
The char sequence representation; never null.
See Also:
StringablePolicy.Type

unique

public boolean unique()
Description copied from interface: Sequence
Returns true if this sequence deliver a given sequence value at most one time until 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.

Returns:
True if unique, false if not.
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.