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

dk.rode.thesis.command
Class ResetCommand<E>

java.lang.Object
  extended by dk.rode.thesis.command.SequenceCommand<E>
      extended by dk.rode.thesis.command.ResetCommand<E>
Type Parameters:
E - The type of result obtained by executing this command. The type corresponds to the type of values delivered by the receiver sequence.
All Implemented Interfaces:
Command<E>, Copyable<Command<E>>, StrictCopyable<Command<E>>

@Participant(value="ConcreteCommand")
public class ResetCommand<E>
extends SequenceCommand<E>

A reset command command will invoke reset() on its receiving sequence when it is executed.

Reset commands are undoable if the receiving sequence is a memorizable sequence, and/or if it is consistent and unique. Notice, however, that the time to undo a non-memorizable sequence by this command is not constant: it is linear with the number of times next() has to be invoked before the old sequence value is found again.

Author:
Gunni Rode / rode.dk

Field Summary
private  E previous
          The sequence value before the last reset.
 
Fields inherited from class dk.rode.thesis.command.SequenceCommand
result, sequence
 
Constructor Summary
ResetCommand(Sequence<E> sequence)
          Constructor.
 
Method Summary
 ResetCommand<E> copy()
          Creates a copy of this command, excluding internal state information required for execution and undo.
protected  List<Command<E>> doExecute(boolean saved)
          Resets the sequence value and sets the result to the (new) current value.
 boolean isUndoable()
          Returns true if this command has been executed and the receiving sequence used by this command is either memorizable and/or the sequence is bounded and consistent.
 E undo()
          Performs undo of this sequence command if possible.
 
Methods inherited from class dk.rode.thesis.command.SequenceCommand
equals, execute, getResult, hashCode, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

previous

private E previous
The sequence value before the last reset.

Can be null.

Constructor Detail

ResetCommand

public ResetCommand(@Participant(value="Receiver")
                    Sequence<E> sequence)
Constructor.

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

copy

public ResetCommand<E> copy()
Description copied from interface: Command
Creates a copy of this command, excluding internal state information required for execution and undo.

Returns:
A new instance of this command; never null.

doExecute

protected List<Command<E>> doExecute(boolean saved)
Resets the sequence value and sets the result to the (new) current value.

Specified by:
doExecute in class SequenceCommand<E>
Parameters:
saved - True if the before state was successfully saved before this method is invoked, false if not.
Returns:
A list of spawned commands, if any, to be executed by the command processor currently executing this command. Can be null or empty.
See Also:
SequenceCommand.execute()

isUndoable

public boolean isUndoable()
Returns true if this command has been executed and the receiving sequence used by this command is either memorizable and/or the sequence is bounded and consistent.

Specified by:
isUndoable in interface Command<E>
Overrides:
isUndoable in class SequenceCommand<E>
Returns:
True if undoable, false if not.

undo

public E undo()
       throws CommandException
Description copied from class: SequenceCommand
Performs undo of this sequence command if possible.

If this command is not undoable an exception will be thrown. If this command cannot be undone otherwise, null will be returned.

The command result value is cleared.

Specified by:
undo in interface Command<E>
Overrides:
undo in class SequenceCommand<E>
Returns:
If this command could be undone, the (new) current sequence value is returned; otherwise null.
Throws:
CommandException - In case this command is not undoable.

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.