info.javelot.functionalj
Class Function

java.lang.Object
  extended byinfo.javelot.functionalj.Function
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
FunctionN, ReflectionFunction

public abstract class Function
extends Object
implements Serializable

Base class for working with functions, which target constructors, static methods and instance methods.

Author:
Copyright © 2006 Frederic Daoud
See Also:
Serialized Form

Field Summary
protected  Class m_class
          The class of the target of this function.
protected  String m_name
          The name of the target, if applicable.
protected  Object m_object
          The object on which to invoke the target, if applicable.
protected  Object[] m_parameters
          The list of supplied parameters.
protected  List m_targets
          The list of possible targets.
 
Constructor Summary
protected Function()
          Default constructor.
protected Function(Class p_class, String p_name, Object p_object, Object[] p_parameters)
          Constructor which accepts the target class, function name, target object, and parameters of the function.
 
Method Summary
 Function addParameter(Object p_parameter)
          Convenience method that calls addParameters(Object[]), creating an array containing the specified parameter.
 Function addParameters(List p_parameters)
          Creates a new function object that contains the previously added parameters, if any, and the given parameters appended to the list, by converting the list to an array and calling addParameters(Object[]).
 Function addParameters(Object[] p_parameters)
          Creates a new function object that contains the previously added parameters, if any, and the given parameters appended to the list.
 Function addParameters(Object p_parameter1, Object p_parameter2)
          Convenience method that calls addParameters(Object[]), creating an array containing the specified parameters.
 Function addParameters(Object p_parameter1, Object p_parameter2, Object p_parameter3)
          Convenience method that calls addParameters(Object[]), creating an array containing the specified parameters.
 Function addParameters(Object p_parameter1, Object p_parameter2, Object p_parameter3, Object p_parameter4)
          Convenience method that calls addParameters(Object[]), creating an array containing the specified parameters.
abstract  Object call()
          Calls the function by invoking the target using the parameters that have been previously supplied (if any) and returns the result.
 String getName()
          Returns the name of the target of this function.
 Object getObject()
          Returns the object on which to invoke the target of this function.
 Object[] getParameters()
          Returns the list of parameters that have been supplied, if any.
 Class getTargetClass()
          Returns the class of the target of this function.
 List getTargets()
          Returns the list of possible targets of this function.
 boolean isObjectPresent()
          Indicates if an object has been supplied to the function.
abstract  boolean isObjectRequired()
          Indicates whether or not an object is required to invoke the target.
protected abstract  Function newInstance(Class p_class, Object[] p_parameters)
          Used to ask subclasses to create an appropriate new instance.
abstract  Function object(Object p_object)
          Supplies the object to use for instance methods.
 Function objectOrParameter(Object p_object)
          Accepts the specified object and uses it as the object on which to invoke the target, if one is required and has not yet been specified; otherwise, the object is used as a parameter supplied to the function.
 String toString()
          Returns a String representation of this Function object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

m_class

protected Class m_class
The class of the target of this function.


m_name

protected String m_name
The name of the target, if applicable.


m_object

protected Object m_object
The object on which to invoke the target, if applicable.


m_parameters

protected Object[] m_parameters
The list of supplied parameters. This list may be null or empty.


m_targets

protected List m_targets
The list of possible targets. Must contain at least one target.

Constructor Detail

Function

protected Function()
Default constructor.


Function

protected Function(Class p_class,
                   String p_name,
                   Object p_object,
                   Object[] p_parameters)
            throws FunctionException
Constructor which accepts the target class, function name, target object, and parameters of the function.

Parameters:
p_class - the class of the target of this function.
p_name - the name of the target. When the target is a method, this name is required. When the target is a constructor, this name is null.
p_object - if the target is an instance method, this is the object on which to invoke the method.
p_parameters - the parameters, if any, to use when invoking the target.
Throws:
FunctionException - thrown if the class parameter is null, or if no possibly matching targets are found.
Method Detail

isObjectRequired

public abstract boolean isObjectRequired()
Indicates whether or not an object is required to invoke the target.

Returns:
true if an object is required to invoke the target, false otherwise.

newInstance

protected abstract Function newInstance(Class p_class,
                                        Object[] p_parameters)
Used to ask subclasses to create an appropriate new instance.

Parameters:
p_class - the class of the target.
p_parameters - the parameters that have been supplied.
Returns:
a new instance of a subclass using the specified class and parameters.

object

public abstract Function object(Object p_object)
                         throws FunctionException
Supplies the object to use for instance methods. This is handled by the InstanceFunction class. For other subclasses, a FunctionException is thrown.

Throws:
FunctionException

addParameter

public Function addParameter(Object p_parameter)
                      throws FunctionException
Convenience method that calls addParameters(Object[]), creating an array containing the specified parameter.

Throws:
FunctionException
See Also:
addParameters(Object[]).

addParameters

public Function addParameters(Object p_parameter1,
                              Object p_parameter2)
                       throws FunctionException
Convenience method that calls addParameters(Object[]), creating an array containing the specified parameters.

Throws:
FunctionException
See Also:
addParameters(Object[]).

addParameters

public Function addParameters(Object p_parameter1,
                              Object p_parameter2,
                              Object p_parameter3)
                       throws FunctionException
Convenience method that calls addParameters(Object[]), creating an array containing the specified parameters.

Throws:
FunctionException
See Also:
addParameters(Object[]).

addParameters

public Function addParameters(Object p_parameter1,
                              Object p_parameter2,
                              Object p_parameter3,
                              Object p_parameter4)
                       throws FunctionException
Convenience method that calls addParameters(Object[]), creating an array containing the specified parameters.

Throws:
FunctionException
See Also:
addParameters(Object[]).

addParameters

public Function addParameters(Object[] p_parameters)
                       throws FunctionException
Creates a new function object that contains the previously added parameters, if any, and the given parameters appended to the list.

Parameters:
p_parameters - the parameters to be added to the list of parameters that are supplied to the function.
Returns:
a new Function object with the parameters added to the list of supplied parameters.
Throws:
FunctionException - thrown if the additional parameters cause the list of supplied parameters not to match any of the possible targets.

addParameters

public Function addParameters(List p_parameters)
                       throws FunctionException
Creates a new function object that contains the previously added parameters, if any, and the given parameters appended to the list, by converting the list to an array and calling addParameters(Object[]). Note that to add a single parameter of type List, addParameter(Object) should be used.

Throws:
FunctionException
See Also:
addParameters(Object[]).

call

public abstract Object call()
                     throws FunctionException
Calls the function by invoking the target using the parameters that have been previously supplied (if any) and returns the result. If an exception is thrown by the target call, it is wrapped into a FunctionException.

Throws:
FunctionException - thrown if an exception occurs when invoking the target, if an object on which to invoke the target is required but none has been supplied, or if all of the potential targets expect more parameters than have been supplied.

getTargetClass

public Class getTargetClass()
Returns the class of the target of this function.

Returns:
the class of the target of this function.

getName

public String getName()
Returns the name of the target of this function.

Returns:
the name of the target of this function, or null if not applicable.

getObject

public Object getObject()
Returns the object on which to invoke the target of this function.

Returns:
the object on which to invoke the target of this function, or null if not applicable.

getParameters

public Object[] getParameters()
Returns the list of parameters that have been supplied, if any.

Returns:
the list of parameters that have been supplied, if any. If none have been supplied, the method returns null.

getTargets

public List getTargets()
Returns the list of possible targets of this function.

Returns:
the list of possible targets of this function. This list contains at least one element, because a Function object cannot exist without at least one possible target.

isObjectPresent

public boolean isObjectPresent()
Indicates if an object has been supplied to the function. This is applicable for instances of InstanceFunction only.

Returns:
true if this function requires an object on which to invoke the target and that object has been supplied, false otherwise.

objectOrParameter

public Function objectOrParameter(Object p_object)
Accepts the specified object and uses it as the object on which to invoke the target, if one is required and has not yet been specified; otherwise, the object is used as a parameter supplied to the function.

Parameters:
p_object - the object to be used as an object on which to invoke the target or as a parameter.
Returns:
a new Function that results from adding the specified object.

toString

public String toString()
Returns a String representation of this Function object.

Returns:
a String representation of this Function object, containing all pertinent information (class, name, object, parameters, and list of possible targets).