|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.command.CompositeCommand<E>
E
- The type of result obtained by executing all contained
commands in this composite command.@Participant(value="ConcreteCommand") public class CompositeCommand<E>
A composite command is a command that represents an number of other contained commands, possibly composite as well, thus forming a tree-like structure.
Composite commands are simple containers that do not maintain
parent references. Contained commands do not know they are
contained unless this information is stored locally. Hence,
when a composite command is executed
,
graph traversal is top-down only, in a depth-first manner; the
result of the last executed command is returned.
A composite command is undoable
if all
contained commands are undoable. If the execution fails
for the n'th command, the commands from n to
zero will be undone when undo()
is invoked, i.e. in
reverse order. The returned results is the result from the
last undone command, i.e. the command at index zero (which
may be composite as well).
The value returned from execute()
and undo()
is the result from the last command in this composite
sequence! For execute
, this is the last command executed,
while for undo
is it the value of the first undone command.
The result of executing a failed composite command again without having undo the previous error is undefined.
Composite commands compares based on identity (==).
Field Summary | |
---|---|
private List<Command<E>> |
commands
The commands associated with this command, in order. |
private int |
index
The index identifying the current command being executed or undone. |
Constructor Summary | |
---|---|
CompositeCommand()
No-arg constructor. |
|
CompositeCommand(Command<E>... commands)
Constructor, which adds each copies each command
in commands and adds it to this composite command. |
|
CompositeCommand(CompositeCommand<E> command)
Copy constructor, which copies each
associated command in command and adds it
to this composite command. |
Method Summary | ||
---|---|---|
boolean |
addCommand(Command<E> command)
Associates the command supplied
as command to this composite command, if
not already. |
|
CompositeCommand<E> |
copy()
Creates a copy of this command, excluding internal state information required for execution and undo. |
|
List<Command<E>> |
execute()
Performs the execution of this command. |
|
List<Command<E>> |
getCommands()
Returns the commands currently
associated with this composite command, in order. |
|
|
getCommands(Class<V> type)
Returns the commands currently
associated with this composite command, in order,
assignable to the type supplied as type . |
|
E |
getResult()
Returns the result of the last executed command contained in this composite command. |
|
boolean |
isUndoable()
Returns true if this command has been executed and is undoable, false if not. |
|
Iterator<Command<E>> |
iterator()
Returns an iterator to iterate through this composite command, which is backed by this composite commands, i.e. |
|
boolean |
removeCommand(Command<?> command)
Removes the command supplied
as command from this composite command, if
associated. |
|
int |
size()
Returns the number of associated commands to this composite command. |
|
String |
toString()
Returns the string representation if this composite command. |
|
E |
undo()
Perform undo of this command, if possible. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private final List<Command<E>> commands
Never null, but can be null.
private int index
Range: 0 <= index <= commands
.size()
Constructor Detail |
---|
public CompositeCommand()
public CompositeCommand(Command<E>... commands)
copies
each command
in commands
and adds it to this composite command. Duplicate commands are ignored.
commands
- The (copied) commands to add; cannot contain null
entries.
NullPointerException
- If commands
contain a null
entry.public CompositeCommand(CompositeCommand<E> command)
copies
each
associated command in command
and adds it
to this composite command.
command
- The composite command to copy; cannot be null.
NullPointerException
- If command
is null.Method Detail |
---|
public boolean addCommand(Command<E> command)
command
supplied
as command
to this composite command, if
not already. If already added, this method does nothing.
command
- The command to add; cannot be null.
command
was added, false if not,
i.e. if already added.
NullPointerException
- If command
is null.
IllegalArgumentException
- If this composite command is
tried added to it self.removeCommand(Command)
public CompositeCommand<E> copy()
Command
copy
in interface Command<E>
copy
in interface Copyable<Command<E>>
public List<Command<E>> execute() throws CommandException
Command
After successful execution, the result of the
execution can be obtained via the Command.getResult()
method.
execute
in interface Command<E>
CommandException
- If the execution fails.Command.undo()
public List<Command<E>> getCommands()
commands
currently
associated with this composite command, in order. Modifying the returned collection will not affect this command.
size()
public <V extends Command<E>> List<V> getCommands(Class<V> type)
commands
currently
associated with this composite command, in order,
assignable to the type supplied as type
.
Invoking this method with the Command
interface
corresponds to invoking getCommands()
.
Modifying the returned collection will not affect this command.
V
- The type of applicable commands.type
- The (super-)class of commands to include; cannot
be null.
type
; never null, but can be
empty.
NullPointerException
- If type
is null.public E getResult()
getResult
in interface Command<E>
public boolean isUndoable()
Command
If undoable, the effect of the execution
can be undone by invoking Command.undo()
.
isUndoable
in interface Command<E>
public Iterator<Command<E>> iterator()
iterator
in interface Iterable<Command<E>>
public boolean removeCommand(Command<?> command)
command
supplied
as command
from this composite command, if
associated. If not associated, this method does nothing.
command
- The command to remove; cannot be null.
command
was removed, false if not,
i.e. if not previously added.
NullPointerException
- If command
is null.addCommand(Command)
public int size()
public String toString()
toString
in class Object
public E undo() throws CommandException
Command
If this method is invoked and this command is not
undoable
, an exception will
be thrown.
The result of invoking undo on a command that has
not been executed
is undefined.
undo
in interface Command<E>
CommandException
- If the undo fails.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |