jigcell.compare.data
Class MethodDispatcher

java.lang.Object
  extended by jigcell.compare.data.MethodDispatcher

public class MethodDispatcher
extends java.lang.Object

Performs automated method dispatching based on a tuneable heuristic.

This code is licensed under the DARPA BioCOMP Open Source License. See LICENSE for more details.

Author:
Nicholas Allen

Nested Class Summary
protected static class MethodDispatcher.MethodScorer
          Intermediate results for computing a method score.
protected static class MethodDispatcher.NoDispatchableMethodException
          Indicates that method dispatching failed because no suitable methods were found.
 
Field Summary
protected  boolean allowWildcardMatches
          Whether wildcard matches of the method name are allowed
protected static char CLASS_ARRAY
          Character representing an array reference
protected static java.lang.String CLASS_BOOLEAN
          Character representing a boolean reference
protected static java.lang.String CLASS_CHARACTER
          Character representing a character reference
protected static java.lang.String CLASS_DOUBLE
          Character representing a double reference
protected static java.lang.String CLASS_FLOAT
          Character representing a float reference
protected static java.lang.String CLASS_INTEGER
          Character representing an integer reference
protected static java.lang.String CLASS_LONG
          Character representing a long reference
protected static java.lang.String CLASS_OBJECT_END
          Character representing an object reference
protected static java.lang.String CLASS_OBJECT_START
          Character representing an object reference
protected static java.lang.String CLASS_SHORT
          Character representing a short reference
protected static java.lang.reflect.Method CONVERTER_LONGDOUBLE
          Converter from Long to Double
static long DEFAULT_PENALTYLENGTH
          Default penalty value for length mismatches
static long DEFAULT_PENALTYLOOSEMATCH
          Default penalty value for loose matches.
static long DEFAULT_REWARDMATCH
          Default value for a match.
static long DEFAULT_REWARDSUBCLASS
          Default value for having a specialized match.
protected static long ERROR
          Maximally bad score
protected static java.util.Map MAP_CLASSNAMETYPE
          Map from primitive class names to formalTypes
protected static java.util.Map MAP_CLASSTYPENAME
          Map from primitive class formalTypes to names
protected static java.lang.String MESSAGE_METHOD
          Beginning of a method error message
protected static java.lang.String MESSAGE_NOTIMPLEMENTED
          Error message when an unimplemented function is invoked
protected static java.lang.String MESSAGE_PARAMETERERROR
          Error message when the parameter formalType of a method is illegal
protected static java.lang.String MESSAGE_RETURNERROR
          Error message when the return formalType of a method is illegal
protected static java.lang.String MESSAGE_TOOBIGERROR
          Error message when an element is larger than can be reflected
protected static java.lang.String METHOD_LONGDOUBLE
          Name of the method to convert a Long to a Double
protected  java.lang.String methodName
          Name of the method to invoke
protected  java.util.HashMap parameterTypeCache
          Map between the known methods and their formalType signature
protected  long penaltyLength
          Penalty value for length mismatches
protected  boolean penaltyLengthChecked
          Whether length mismatches are allowed
protected  long penaltyLooseMatch
          Penalty value for loose matches
protected  boolean penaltyLooseMatchChecked
          Whether loose formalType matches are allowed
protected  long rewardMatch
          Reward value for a match
protected  long rewardSubclass
          Reward value for having a specialized match
protected  boolean rewardSubclassChecked
          Whether specialized matches are rewarded
protected  java.lang.Object target
          Target of method invocations
 
Constructor Summary
MethodDispatcher(java.lang.Object target, java.lang.String methodName)
          Creates a new method dispatcher.
 
Method Summary
static java.lang.Class computePrimitiveBox(java.lang.Class clazz)
          Boxes a primitive formalType or returns the formalType unchanged if it is not a primitive formalType.
static java.lang.Object convertArrayClass(java.lang.Object array, java.lang.Class clazz, java.lang.reflect.Method converter)
          Converts an array from one formalType to another.
static java.lang.Object convertFromElement(IDataElement element)
          Converts a DataElement to a collection of objects.
static java.lang.Double convertLongDouble(java.lang.Long l)
          Converts a Long to a Double.
static IDataElement convertToElement(java.lang.Object o)
          Converts a collection of objects to a DataElement.
static java.lang.Class createArrayClass(java.lang.Class clazz, int dimension)
          Creates a multidimensional array formalType.
protected  void createTypeCache()
          Fills in the entries of the parameter formalType cache.
 IDataElement dispatch(IDataElement element)
          Performs method dispatching on the target with a DataElement.
 java.lang.Object dispatch(java.lang.Object o)
          Performs method dispatching on the target with a collection of objects.
protected  java.lang.Object dispatchExact(MethodDispatcher.MethodScorer scorer, java.lang.reflect.Method method)
          Dispatches an exact match.
protected  java.lang.Object dispatchLoose(MethodDispatcher.MethodScorer scorer, java.lang.reflect.Method method)
          Dispatches a loose match.
static java.lang.Class findCommonSuperclass(java.lang.Object[] objects)
          The common superclass for a collection of objects.
 boolean getAllowWildcardMatches()
          Whether wildcard matches are allowed.
static int getDimension(java.lang.Class clazz)
          The dimension of a formalType.
 java.lang.String getMethodName()
          Name of the method to invoke.
 long getPenaltyLength()
          The penalty for length mismatches.
 boolean getPenaltyLengthChecked()
          Whether length mismatches are allowed.
 long getPenaltyLooseMatch()
          The penalty for loose formalType matches.
 boolean getPenaltyLooseMatchChecked()
          Whether loose formalType matches are allowed.
 long getRewardMatch()
          The reward for matches.
 long getRewardSubclass()
          The reward for specialized matches.
 boolean getRewardSubclassChecked()
          Whether specialized matches are rewarded.
 java.lang.Object getTarget()
          The target to invoke.
static java.lang.Class getUnderlyingClass(java.lang.Class clazz)
          The non-array formalType of a formalType.
static boolean isConvertibleClass(java.lang.Class clazz)
          Whether a class is of a convertible formalType.
protected  long scoreExact(MethodDispatcher.MethodScorer scorer)
          Scores for exact matches.
protected  long scoreLoose(MethodDispatcher.MethodScorer scorer)
          Scores for loose matches.
 void setAllowWildcardMatches(boolean allowWildcardMatches)
          Sets whether wildcard matches are allowed.
 void setMethodName(java.lang.String methodName)
          Sets the name of the method to invoke.
 void setPenaltyLength(long penaltyLength)
          Sets the penalty for length mismatches.
 void setPenaltyLengthChecked(boolean penaltyLengthChecked)
          Sets whether length mismatches are allowed.
 void setPenaltyLooseMatch(long penaltyLooseMatch)
          Sets the penalty for loose formalType matches.
 void setPenaltyLooseMatchChecked(boolean penaltyLooseMatchChecked)
          Sets whether loose formalType matches are allowed.
 void setRewardMatch(long rewardMatch)
          Sets the reward for matches.
 void setRewardSubclass(long rewardSubclass)
          Sets the reward for specialized matches.
 void setRewardSubclassChecked(boolean rewardSubclassChecked)
          Sets whether specialized matches are rewarded.
 void setTarget(java.lang.Object target)
          Sets the target to invoke.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_REWARDMATCH

public static final long DEFAULT_REWARDMATCH
Default value for a match. A match is a widening conversion, boxing operation, unboxing operation, or uniform array aggregation.

See Also:
Constant Field Values

DEFAULT_REWARDSUBCLASS

public static final long DEFAULT_REWARDSUBCLASS
Default value for having a specialized match. A specialized match is a match to a DataElement subclass

See Also:
Constant Field Values

DEFAULT_PENALTYLENGTH

public static final long DEFAULT_PENALTYLENGTH
Default penalty value for length mismatches

See Also:
Constant Field Values

DEFAULT_PENALTYLOOSEMATCH

public static final long DEFAULT_PENALTYLOOSEMATCH
Default penalty value for loose matches. A loose match is a conversion between literal and primitive formalTypes, cast from integer to double, or exact array disaggregation

See Also:
Constant Field Values

CLASS_ARRAY

protected static final char CLASS_ARRAY
Character representing an array reference

See Also:
Constant Field Values

MAP_CLASSTYPENAME

protected static final java.util.Map MAP_CLASSTYPENAME
Map from primitive class formalTypes to names


MAP_CLASSNAMETYPE

protected static final java.util.Map MAP_CLASSNAMETYPE
Map from primitive class names to formalTypes


CLASS_BOOLEAN

protected static final java.lang.String CLASS_BOOLEAN
Character representing a boolean reference

See Also:
Constant Field Values

CLASS_CHARACTER

protected static final java.lang.String CLASS_CHARACTER
Character representing a character reference

See Also:
Constant Field Values

CLASS_DOUBLE

protected static final java.lang.String CLASS_DOUBLE
Character representing a double reference

See Also:
Constant Field Values

CLASS_FLOAT

protected static final java.lang.String CLASS_FLOAT
Character representing a float reference

See Also:
Constant Field Values

CLASS_INTEGER

protected static final java.lang.String CLASS_INTEGER
Character representing an integer reference

See Also:
Constant Field Values

CLASS_LONG

protected static final java.lang.String CLASS_LONG
Character representing a long reference

See Also:
Constant Field Values

CLASS_OBJECT_START

protected static final java.lang.String CLASS_OBJECT_START
Character representing an object reference

See Also:
Constant Field Values

CLASS_OBJECT_END

protected static final java.lang.String CLASS_OBJECT_END
Character representing an object reference

See Also:
Constant Field Values

CLASS_SHORT

protected static final java.lang.String CLASS_SHORT
Character representing a short reference

See Also:
Constant Field Values

METHOD_LONGDOUBLE

protected static final java.lang.String METHOD_LONGDOUBLE
Name of the method to convert a Long to a Double

See Also:
Constant Field Values

MESSAGE_METHOD

protected static final java.lang.String MESSAGE_METHOD
Beginning of a method error message

See Also:
Constant Field Values

MESSAGE_NOTIMPLEMENTED

protected static final java.lang.String MESSAGE_NOTIMPLEMENTED
Error message when an unimplemented function is invoked

See Also:
Constant Field Values

MESSAGE_PARAMETERERROR

protected static final java.lang.String MESSAGE_PARAMETERERROR
Error message when the parameter formalType of a method is illegal

See Also:
Constant Field Values

MESSAGE_RETURNERROR

protected static final java.lang.String MESSAGE_RETURNERROR
Error message when the return formalType of a method is illegal

See Also:
Constant Field Values

MESSAGE_TOOBIGERROR

protected static final java.lang.String MESSAGE_TOOBIGERROR
Error message when an element is larger than can be reflected

See Also:
Constant Field Values

ERROR

protected static final long ERROR
Maximally bad score

See Also:
Constant Field Values

CONVERTER_LONGDOUBLE

protected static java.lang.reflect.Method CONVERTER_LONGDOUBLE
Converter from Long to Double


allowWildcardMatches

protected boolean allowWildcardMatches
Whether wildcard matches of the method name are allowed


penaltyLengthChecked

protected boolean penaltyLengthChecked
Whether length mismatches are allowed


penaltyLooseMatchChecked

protected boolean penaltyLooseMatchChecked
Whether loose formalType matches are allowed


rewardSubclassChecked

protected boolean rewardSubclassChecked
Whether specialized matches are rewarded


parameterTypeCache

protected transient java.util.HashMap parameterTypeCache
Map between the known methods and their formalType signature


penaltyLength

protected long penaltyLength
Penalty value for length mismatches


penaltyLooseMatch

protected long penaltyLooseMatch
Penalty value for loose matches


rewardMatch

protected long rewardMatch
Reward value for a match


rewardSubclass

protected long rewardSubclass
Reward value for having a specialized match


target

protected java.lang.Object target
Target of method invocations


methodName

protected java.lang.String methodName
Name of the method to invoke

Constructor Detail

MethodDispatcher

public MethodDispatcher(java.lang.Object target,
                        java.lang.String methodName)
Creates a new method dispatcher.

Parameters:
target - Object to dispatch to
methodName - Name of method to invoke
Method Detail

computePrimitiveBox

public static java.lang.Class computePrimitiveBox(java.lang.Class clazz)
Boxes a primitive formalType or returns the formalType unchanged if it is not a primitive formalType.

Parameters:
clazz - Class

convertArrayClass

public static java.lang.Object convertArrayClass(java.lang.Object array,
                                                 java.lang.Class clazz,
                                                 java.lang.reflect.Method converter)
Converts an array from one formalType to another.

Parameters:
array - Array
clazz - New formalType
converter - Method that takes the objectified formalType of the underlying class of array as a parameter and returns an object of the objectified formalType of formalType clazz

convertFromElement

public static java.lang.Object convertFromElement(IDataElement element)
Converts a DataElement to a collection of objects.

Parameters:
element - Element

convertLongDouble

public static java.lang.Double convertLongDouble(java.lang.Long l)
Converts a Long to a Double.

Parameters:
l - Long

convertToElement

public static IDataElement convertToElement(java.lang.Object o)
Converts a collection of objects to a DataElement.

Parameters:
o - Objects

createArrayClass

public static java.lang.Class createArrayClass(java.lang.Class clazz,
                                               int dimension)
Creates a multidimensional array formalType.

Parameters:
clazz - Type class
dimension - Dimension

findCommonSuperclass

public static java.lang.Class findCommonSuperclass(java.lang.Object[] objects)
The common superclass for a collection of objects.

Parameters:
objects - Objects

getDimension

public static int getDimension(java.lang.Class clazz)
The dimension of a formalType.

Parameters:
clazz - Class

getUnderlyingClass

public static java.lang.Class getUnderlyingClass(java.lang.Class clazz)
The non-array formalType of a formalType.

Parameters:
clazz - Class

isConvertibleClass

public static boolean isConvertibleClass(java.lang.Class clazz)
Whether a class is of a convertible formalType.

Parameters:
clazz - Class

dispatch

public IDataElement dispatch(IDataElement element)
                      throws MethodDispatcher.NoDispatchableMethodException
Performs method dispatching on the target with a DataElement.

Parameters:
element - Element
Throws:
MethodDispatcher.NoDispatchableMethodException

dispatch

public java.lang.Object dispatch(java.lang.Object o)
                          throws MethodDispatcher.NoDispatchableMethodException
Performs method dispatching on the target with a collection of objects.

Parameters:
o - Objects
Throws:
MethodDispatcher.NoDispatchableMethodException

getAllowWildcardMatches

public boolean getAllowWildcardMatches()
Whether wildcard matches are allowed.


getMethodName

public java.lang.String getMethodName()
Name of the method to invoke.


getPenaltyLength

public long getPenaltyLength()
The penalty for length mismatches.


getPenaltyLengthChecked

public boolean getPenaltyLengthChecked()
Whether length mismatches are allowed.


getPenaltyLooseMatch

public long getPenaltyLooseMatch()
The penalty for loose formalType matches.


getPenaltyLooseMatchChecked

public boolean getPenaltyLooseMatchChecked()
Whether loose formalType matches are allowed.


getRewardMatch

public long getRewardMatch()
The reward for matches.


getRewardSubclass

public long getRewardSubclass()
The reward for specialized matches.


getRewardSubclassChecked

public boolean getRewardSubclassChecked()
Whether specialized matches are rewarded.


getTarget

public java.lang.Object getTarget()
The target to invoke.


setAllowWildcardMatches

public void setAllowWildcardMatches(boolean allowWildcardMatches)
Sets whether wildcard matches are allowed.

Parameters:
allowWildcardMatches - Whether wildcard matches are allowed

setMethodName

public void setMethodName(java.lang.String methodName)
Sets the name of the method to invoke.

Parameters:
methodName - Name

setPenaltyLength

public void setPenaltyLength(long penaltyLength)
Sets the penalty for length mismatches.

Parameters:
penaltyLength - The penalty for length mismatches

setPenaltyLengthChecked

public void setPenaltyLengthChecked(boolean penaltyLengthChecked)
Sets whether length mismatches are allowed.

Parameters:
penaltyLengthChecked - Whether length mismatches are allowed

setPenaltyLooseMatch

public void setPenaltyLooseMatch(long penaltyLooseMatch)
Sets the penalty for loose formalType matches.

Parameters:
penaltyLooseMatch - The penalty for loose formalType matches

setPenaltyLooseMatchChecked

public void setPenaltyLooseMatchChecked(boolean penaltyLooseMatchChecked)
Sets whether loose formalType matches are allowed.

Parameters:
penaltyLooseMatchChecked - Whether loose formalType matches are allowed

setRewardMatch

public void setRewardMatch(long rewardMatch)
Sets the reward for matches.

Parameters:
rewardMatch - The rewarded for matches

setRewardSubclass

public void setRewardSubclass(long rewardSubclass)
Sets the reward for specialized matches.

Parameters:
rewardSubclass - The rewarded for specialized matches

setRewardSubclassChecked

public void setRewardSubclassChecked(boolean rewardSubclassChecked)
Sets whether specialized matches are rewarded.

Parameters:
rewardSubclassChecked - Whether specialized matches are rewarded

setTarget

public void setTarget(java.lang.Object target)
Sets the target to invoke.

Parameters:
target - Target

createTypeCache

protected void createTypeCache()
Fills in the entries of the parameter formalType cache.


dispatchExact

protected java.lang.Object dispatchExact(MethodDispatcher.MethodScorer scorer,
                                         java.lang.reflect.Method method)
                                  throws MethodDispatcher.NoDispatchableMethodException
Dispatches an exact match. The dispatch will fail if a non-exact conversion is required.

Parameters:
method - Method
Throws:
MethodDispatcher.NoDispatchableMethodException

dispatchLoose

protected java.lang.Object dispatchLoose(MethodDispatcher.MethodScorer scorer,
                                         java.lang.reflect.Method method)
                                  throws MethodDispatcher.NoDispatchableMethodException
Dispatches a loose match. The dispatch will fail if a non-exact or loose conversion is required.

Parameters:
method - Method
Throws:
MethodDispatcher.NoDispatchableMethodException

scoreExact

protected long scoreExact(MethodDispatcher.MethodScorer scorer)
Scores for exact matches. An error is reported if an exact formalType conversion is impossible.


scoreLoose

protected long scoreLoose(MethodDispatcher.MethodScorer scorer)
Scores for loose matches. An error is reported if an exact or loose formalType conversion is impossible.