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

dk.rode.thesis.memento
Class GuardedSequenceMemento<E>

java.lang.Object
  extended by dk.rode.thesis.memento.SequenceMemento<E>
      extended by dk.rode.thesis.memento.GuardedSequenceMemento<E>
Type Parameters:
E - The type of values delivered by applicable memorizable sequences.
All Implemented Interfaces:
Stringable<Sequence<E>>

public class GuardedSequenceMemento<E>
extends SequenceMemento<E>

A guarded sequence memento offer the same functionality as SequenceMemento, but enforce access rules to ensure that only a (memorizable) sequence can set or get the memento state.

Memento state is defined as data that can be set or acquired using the the state methods setSequence(Sequence), getSequence(), and current(), respectively.

Implementation notes:
All memento functionality has to be declared public if it must be used outside this package. It cannot have "two interfaces" in Java, a narrow (public) and a wide (public and private, using friends to access private part), as described by Gamma et al. [Gamma95, p.285-287]. This implementation therefore divides the memento functionality into two interfaces, namely GuardedSequenceMemento (private part of wide) and MemorizableSequence (narrow, public). However, the state methods setSequence(Sequence), getSequence(), and current() still have to be public. To enforce an access check at runtime, this class uses a CallerClass to identify the caller of the state methods, and only allows calls from valid contexts (memorizable sequences). The GuardedSequenceMemento class is thus a Guarded Type.

Author:
Gunni Rode / rode.dk

Constructor Summary
GuardedSequenceMemento()
          No-arg constructor.
GuardedSequenceMemento(Sequence<E> sequence)
          Constructor.
 
Method Summary
 E current()
          Returns the value stored in the memorised sequence.
 Sequence<E> getSequence()
          Return the sequence memorised by this memento.
 void setSequence(Sequence<E> sequence)
          Memorises the state of the Sequence supplied as sequence by copying it.
 
Methods inherited from class dk.rode.thesis.memento.SequenceMemento
getStringablePolicy, toString, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GuardedSequenceMemento

public GuardedSequenceMemento()
No-arg constructor.

The sequence representing the state to memorise must be set explicitly using the setSequence(Sequence) method.


GuardedSequenceMemento

public GuardedSequenceMemento(Sequence<E> sequence)
Constructor.

Parameters:
sequence - The sequence representing the state to memorise; cannot be null. This memento does not keep a reference to sequence, but copies it.
Throws:
NullPointerException - If sequence is null.
Method Detail

current

public final E current()
Returns the value stored in the memorised sequence.

Overrides:
current in class SequenceMemento<E>
Returns:
The current (fixed) value; never null.
Throws:
IllegalStateException - If no sequence has been set.
UnsupportedOperationException - If the caller is not a sequence of the type stored internally in this memento.

getSequence

public final Sequence<E> getSequence()
Return the sequence memorised by this memento.

Mutating the returned sequence will not alter the internal state of this memento.

Overrides:
getSequence in class SequenceMemento<E>
Returns:
The memorised sequence; never null.
Throws:
IllegalStateException - If no sequence has been set.
UnsupportedOperationException - If the caller is not a sequence of the type stored internally in this memento.
See Also:
SequenceMemento.setSequence(Sequence)

setSequence

public final void setSequence(Sequence<E> sequence)
Memorises the state of the Sequence supplied as sequence by copying it. The copy can then be retrieved using the SequenceMemento.getSequence() method.

Overrides:
setSequence in class SequenceMemento<E>
Parameters:
sequence - The sequence representing the state to memorise; cannot be null. This memento does not keep a reference to sequence, but copies it.
Throws:
NullPointerException - If sequence is null.
IllegalStateException - If a sequence has already been set.
UnsupportedOperationException - If sequence does not have the same class as the caller of this method.

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.