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

dk.rode.thesis.proxy
Class SynchronisedSequence<E>

java.lang.Object
  extended by dk.rode.thesis.decorator.SequenceDecorator<E>
      extended by dk.rode.thesis.proxy.SynchronisedSequence<E>
Type Parameters:
E - The type of generator delivered by this sequence.
All Implemented Interfaces:
Sequence<E>, Copyable<Sequence<E>>, StrictCopyable<Sequence<E>>, Stringable<Sequence<E>>

@ParticipantUsage(value="Subject",
                  type=Sequence.class)
@Participant(value="Proxy")
public class SynchronisedSequence<E>
extends SequenceDecorator<E>
implements Sequence<E>

A synchronised sequence ensures thread-safe access to a proxied sequence if and only if the proxied sequence is accessed through the proxy only. The lock used in the proxied sequence instance.

A copy of a synchronised sequence will proxy the same original proxied object and use the same lock.

A synchronised sequence exhibit the same bounded, consistent, and unique properties as the proxied sequence.

Implementation notes:
A synchronised sequence corresponds to a smart reference as described by Gamma et al. [Gamma95, p.209].

Author:
Gunni Rode / rode.dk
See Also:
SequenceProxyFactory.getSynchronisedSequence(Sequence)

Nested Class Summary
 
Nested classes/interfaces inherited from interface dk.rode.thesis.meta.model.Sequence
Sequence.State
 
Field Summary
private  Object lock
          The lock to synchronise on.
 
Constructor Summary
SynchronisedSequence(Sequence<E> sequence)
          Constructor.
SynchronisedSequence(Sequence<E> sequence, Object lock)
          Constructor.
 
Method Summary
 SynchronisedSequence<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.
 E next()
          Returns the next element from this sequence.
 void reset()
          Resets this sequence to start over if it is consistent.
 
Methods inherited from class dk.rode.thesis.decorator.SequenceDecorator
bounded, consistent, equals, getSequence, getStringablePolicy, hashCode, state, toString, toString, unique
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface dk.rode.thesis.meta.model.Sequence
bounded, consistent, state, unique
 
Methods inherited from interface dk.rode.thesis.strategy.Stringable
getStringablePolicy, toString
 

Field Detail

lock

private final Object lock
The lock to synchronise on.

Constructor Detail

SynchronisedSequence

public SynchronisedSequence(@Participant(value="RealSubject")
                            Sequence<E> sequence)
Constructor.

The lock used is sequence.

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

SynchronisedSequence

public SynchronisedSequence(@Participant(value="RealSubject")
                            Sequence<E> sequence,
                            Object lock)
Constructor.

Parameters:
sequence - The sequence to proxy; cannot be null.
lock - The lock object to synchronise on; cannot be null.
Throws:
NullPointerException - If either argument is null.
Method Detail

copy

public SynchronisedSequence<E> copy()
Description copied from interface: Sequence
Returns a copy of this sequence that will start at the same sequence index as this sequence.

Specified by:
copy in interface Sequence<E>
Specified by:
copy in interface Copyable<Sequence<E>>
Returns:
A copy of this sequence; 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.

Specified by:
current in interface Sequence<E>
Overrides:
current in class SequenceDecorator<E>
Returns:
The current element; never null.

next

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

Specified by:
next in interface Sequence<E>
Overrides:
next in class SequenceDecorator<E>
Returns:
The next element; never null.
See Also:
Sequence.current(), Sequence.state()

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 SequenceDecorator<E>

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.