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

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

java.lang.Object
  extended by dk.rode.thesis.command.SequenceCommand<E>
      extended by dk.rode.thesis.command.ReverseCommand<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 ReverseCommand<E>
extends SequenceCommand<E>

A reserve command command will reverse a given sequence when it is executed, if possible.

Only currently reversible sequences are affected by this command per default. However, a list of commands can be supplied at construction time to be spawned during execution for non-reversible sequences.

Undo of spawned commands is not handled by this command.

Author:
Gunni Rode / rode.dk

Field Summary
private  List<Command<E>> commands
          Command instances to spawn in case the sequence is not reversible, if any.
private  Boolean reversed
          True if this command successfully reversed a reversible sequence, false if not.
 
Fields inherited from class dk.rode.thesis.command.SequenceCommand
result, sequence
 
Constructor Summary
ReverseCommand(ReverseCommand<E> command)
          Copy constructor.
ReverseCommand(Sequence<E> sequence)
          Constructor.
ReverseCommand(Sequence<E> sequence, Command<E>... commands)
          Constructor.
 
Method Summary
 ReverseCommand<E> copy()
          Creates a copy of this command, excluding internal state information required for execution and undo.
protected  List<Command<E>> doExecute(boolean saved)
          Reverses the sequence if reversible and returns the current (same) value.
 boolean isUndoable()
          Returns true if the receiving sequence is reversible, false if not.
 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

commands

private final List<Command<E>> commands
Command instances to spawn in case the sequence is not reversible, if any.

Undo of spawned commands is not handled by this command.

Can be null.


reversed

private Boolean reversed
True if this command successfully reversed a reversible sequence, false if not.

Will be null if this command has not been executed.

Constructor Detail

ReverseCommand

public ReverseCommand(ReverseCommand<E> command)
Copy constructor.

Commands to spawned, if any, will also be copied.

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

ReverseCommand

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

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

ReverseCommand

public ReverseCommand(@Participant(value="Receiver")
                      Sequence<E> sequence,
                      Command<E>... commands)
Constructor.

The commands supplied as commands, if any, will be spawned in order during execution in case sequence cannot be reversed.

Parameters:
sequence - The sequence; cannot be null.
commands - The commands to possible spawn; can be null or empty.
Throws:
NullPointerException - If sequence is null.
Method Detail

copy

public ReverseCommand<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)
Reverses the sequence if reversible and returns the current (same) value.

If the sequence is not reversible, the commands supplied at construction time will be spawned, if any.

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:
If the sequence was not reversible, a list containing the commands supplied at construction time, if any; never null, but can be empty.
See Also:
SequenceCommand.execute()

isUndoable

public boolean isUndoable()
Returns true if the receiving sequence is reversible, false if not.

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.