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

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

java.lang.Object
  extended by dk.rode.thesis.command.EvilCommand<E>
Type Parameters:
E - The type of mimicked result obtained by executing this command. This command type always return null as the result.
All Implemented Interfaces:
Command<E>, Copyable<Command<E>>, StrictCopyable<Command<E>>

@Participant(value="ConcreteCommand")
public class EvilCommand<E>
extends Object
implements Command<E>

An evil command is a test command that will always fail during execution and perhaps on undo as well by specifying that it is undoable while in reality it is not.

The same instance of a EvilCommand.EvilCommandException is always thrown to be easily identifiable in testing scenarios.

It still utilises a type parameter, E, so it can mimic commands delivering a certain type of value.

Evil commands are thread-safe and can be shared.

Author:
Gunni Rode / rode.dk

Nested Class Summary
static class EvilCommand.EvilCommandException
          The exception type thrown during execution and perhaps on undo of the an evil command.
 
Field Summary
static EvilCommand.EvilCommandException ERROR
          This command exception instance always thrown during the execution add perhaps during undo as well.
private  boolean failOnUndo
          True if this command will fail on undo, false if it only fails on execution.
 
Constructor Summary
EvilCommand()
          Constructor.
EvilCommand(boolean failOnUndo)
          Constructor.
 
Method Summary
 EvilCommand<E> copy()
          Creates a copy of this command, excluding internal state information required for execution and undo.
 boolean equals(Object object)
          Returns true if object is an evil command set to fail in the same way as this command, otherwise fails.
 List<Command<E>> execute()
          Always throws ERROR.
 E getResult()
          Returns the result of the execution of this command.
 int hashCode()
          Returns the hash code of this evil command.
 boolean isUndoable()
          Returns true.
 String toString()
          Returns the string representation of this evil command.
 E undo()
          If this command is set to fail on undo, ERROR is thrown.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ERROR

public static final EvilCommand.EvilCommandException ERROR
This command exception instance always thrown during the execution add perhaps during undo as well.

Never null.


failOnUndo

private final boolean failOnUndo
True if this command will fail on undo, false if it only fails on execution.

Constructor Detail

EvilCommand

public EvilCommand()
Constructor.

This evil command will not fail on undo.


EvilCommand

public EvilCommand(boolean failOnUndo)
Constructor.

Parameters:
failOnUndo - True to fail on undo, false to only fail during execution.
Method Detail

copy

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

Specified by:
copy in interface Command<E>
Specified by:
copy in interface Copyable<Command<E>>
Returns:
A new instance of this command; never null.

equals

public boolean equals(Object object)
Returns true if object is an evil command set to fail in the same way as this command, otherwise fails.

Overrides:
equals in class Object
Parameters:
object - The object to test; can be null.
Returns:
True if equal, false if not.

execute

public List<Command<E>> execute()
                         throws EvilCommand.EvilCommandException
Always throws ERROR.

Specified by:
execute in interface Command<E>
Returns:
Never returns.
Throws:
EvilCommand.EvilCommandException - Always thrown.
See Also:
Command.undo()

getResult

public E getResult()
Description copied from interface: Command
Returns the result of the execution of this command.

Invoking this method before execution will cause an illegal state exception to be thrown. If undone, this command must be re-executed before the result can be fetched again.

Specified by:
getResult in interface Command<E>
Returns:
Always null.

hashCode

public int hashCode()
Returns the hash code of this evil command.

Overrides:
hashCode in class Object
Returns:
The hash code.

isUndoable

public boolean isUndoable()
Returns true.

Specified by:
isUndoable in interface Command<E>
Returns:
True.

toString

public String toString()
Returns the string representation of this evil command.

Overrides:
toString in class Object
Returns:
The string representation; never null.

undo

public E undo()
       throws EvilCommand.EvilCommandException
If this command is set to fail on undo, ERROR is thrown.

Otherwise null is returned.

Specified by:
undo in interface Command<E>
Returns:
null if this command is not set to fail on undo.
Throws:
EvilCommand.EvilCommandException - If this command is set to fail on undo.

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.