net.java.joglutils.msg.misc
Class ActionTable

java.lang.Object
  extended by net.java.joglutils.msg.misc.ActionTable

public class ActionTable
extends Object

Keeps track of methods which are attached to a particular Action type for a given set of Node types. This mechanism is different than using the typical Visitor design pattern because Actions, even built-in Actions, need to easily support the addition of newly added Node types. Dispatch occurs via reflection on a per-Node-type basis.


Constructor Summary
ActionTable(Class<? extends Action> actionClass)
          Creates an ActionTable intended for use with a particular Action subclass.
 
Method Summary
 void addActionMethod(Class<? extends Node> nodeType, Method actionMethod)
          Adds an action method for a particular node type to this table.
 Method lookupActionMethod(Node node)
          Looks up the appropriate action method for the given node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActionTable

public ActionTable(Class<? extends Action> actionClass)
Creates an ActionTable intended for use with a particular Action subclass.

Method Detail

addActionMethod

public void addActionMethod(Class<? extends Node> nodeType,
                            Method actionMethod)
                     throws IllegalArgumentException
Adds an action method for a particular node type to this table. This method will apply to this type and all subtypes unless one is added for a more specific subtype. An action method is defined as a public static method with the following signature:

public static void [method name]([Action subclass] action, [Node subclass] node)

The action subclass may be the action class this ActionTable was created with, or any supertype up to and including the Action class. This avoids unnecessary downcasting of the Action in action methods.

The node subclass may be the node type which is passed in to the addActionMethod call, or any supertype up to and including the Node class. This allows convenient overloading of action methods.

Note that it does not matter which type the action method is attached to. This is a key mechanism for extensibility; new action methods can be attached to existing action types to handle new node types which are added to the system.

An action uses reflection to dispatch to these methods at run time, caching lookup results for node types which have not been encountered yet.

Throws:
IllegalArgumentException

lookupActionMethod

public Method lookupActionMethod(Node node)
Looks up the appropriate action method for the given node. Performs caching internally to avoid excessive reflective lookups. Returns null if no action method was registered. This should only happen if an action method was not registered for the base Node type.