extvisitor
Class AExtVisitor<R,I,P,H extends IExtVisitorHost<I,? super H>>

java.lang.Object
  extended by extvisitor.AExtVisitor<R,I,P,H>
Type Parameters:
R - The type of the return value
I - The type of the index value
P - The type of the input parameters
H - The type of the host, restricted to being a subclass of IExtVisitorHost<I, ? super H>
All Implemented Interfaces:
IExtVisitor<R,I,P,H>, java.io.Serializable
Direct Known Subclasses:
DataPacketAlgo, StatusAlgo

public abstract class AExtVisitor<R,I,P,H extends IExtVisitorHost<I,? super H>>
extends java.lang.Object
implements IExtVisitor<R,I,P,H>

Abstract implementation of IExtVisitor that adds an invariant implementation of storing commands associated with each case in a dictionary indexed by the case's associated index value. When a particular case is called, the associated command is retrieved and executed. The return value is the return value from the command. If no associated command is found, then a default command is executed. In general, command-based implementations of IExtVisitor will be concrete subclasses of this class.
Usage:

  
 public class MyExtVisitor extends AExtVisitor<MyReturn, MyIndex, MyReturn, MyExtVisitorHost> {...}
 

Author:
Stephen Wong (c) 2010
See Also:
Serialized Form

Field Summary
private  java.util.Map<I,IExtVisitorCmd<R,I,P,H>> cmds
          The dictionary used to store the commands
private  IExtVisitorCmd<R,I,P,H> defaultCmd
          The default command to use if no command is associated with a case index value.
private static long serialVersionUID
           
 
Constructor Summary
AExtVisitor(IExtVisitorCmd<R,I,P,H> defaultCmd)
          Constructor that takes a default command to use.
AExtVisitor(R noOpResult)
          A convenience constructor that takes a value that the default command will return.
 
Method Summary
<T extends IExtVisitorHost<I,? super H>>
R
caseAt(I idx, T host, P... params)
          Concrete implementation of the parameterized case method that takes the index value, retrieves an associated IExtVisitor command and executes the command with the given host and input parameters.
 IExtVisitorCmd<R,I,P,H> getCmd(I idx)
          Retrieve the command associated with given index value.
 IExtVisitorCmd<R,I,P,H> getDefaultCmd()
          Retrieve the current default command
 void setCmd(I idx, IExtVisitorCmd<R,I,P,H> cmd)
          Associates the given index value with the given command
 void setDefaultCmd(IExtVisitorCmd<R,I,P,H> defaultCmd)
          Set the default command to a new value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

cmds

private java.util.Map<I,IExtVisitorCmd<R,I,P,H extends IExtVisitorHost<I,? super H>>> cmds
The dictionary used to store the commands


defaultCmd

private IExtVisitorCmd<R,I,P,H extends IExtVisitorHost<I,? super H>> defaultCmd
The default command to use if no command is associated with a case index value.

Constructor Detail

AExtVisitor

public AExtVisitor(IExtVisitorCmd<R,I,P,H> defaultCmd)
Constructor that takes a default command to use.

Parameters:
defaultCmd - The default command to use.

AExtVisitor

public AExtVisitor(R noOpResult)
A convenience constructor that takes a value that the default command will return. A default command is created will return the given value and do nothing else.

Parameters:
noOpResult - The value for the default command to return.
Method Detail

setCmd

public void setCmd(I idx,
                   IExtVisitorCmd<R,I,P,H> cmd)
Associates the given index value with the given command

Parameters:
idx - The index value to use associate with the command.
cmd - The command associated with the index value

getCmd

public IExtVisitorCmd<R,I,P,H> getCmd(I idx)
Retrieve the command associated with given index value. null is returned if no command is associated with the index value.

Parameters:
idx - An index value
Returns:
The IExtVisitorCmd associated with the index value or null

getDefaultCmd

public IExtVisitorCmd<R,I,P,H> getDefaultCmd()
Retrieve the current default command

Returns:
The current default command

setDefaultCmd

public void setDefaultCmd(IExtVisitorCmd<R,I,P,H> defaultCmd)
Set the default command to a new value.

Parameters:
defaultCmd - The new default command

caseAt

public <T extends IExtVisitorHost<I,? super H>> R caseAt(I idx,
                                                         T host,
                                                         P... params)
Concrete implementation of the parameterized case method that takes the index value, retrieves an associated IExtVisitor command and executes the command with the given host and input parameters. The result from the command execution is returned. If not associated command is found, then the current default command is executed. Any given host's execute method will call this method, passing the index value associated with that host, a reference to the host itself and pass along any input parameters that were supplied.

Specified by:
caseAt in interface IExtVisitor<R,I,P,H extends IExtVisitorHost<I,? super H>>
Type Parameters:
T - The type of the host the is expected to call this method. T is restricted to be a subclass of IExtVisitorHost<I, ? super H>
Parameters:
idx - The index value for the case
host - The visitor's host.
params - Vararg input parameters for the case
Returns:
The result from executing the associated or default command