org.mmbase.util.functions
Class AbstractFunction<R>

java.lang.Object
  extended by org.mmbase.util.functions.AbstractFunction<R>
All Implemented Interfaces:
Serializable, Comparable<Function<R>>, Function<R>
Direct Known Subclasses:
BeanFunction, FileServlet.DirectoryFunction, MethodFunction, NodeFunction, ProcessorModule.GetNodeListFunction, ProcessorModule.ProcessFunction, ProcessorModule.ReplaceFunction, SetFunction

public abstract class AbstractFunction<R>
extends Object
implements Function<R>, Comparable<Function<R>>, Serializable

An abstract representation of a piece of functionality (a 'function'). A function has a name, a return type, and a parameter-definition (which is a Parameter array). The goal of a Function object is to call its getFunctionValue(Parameters) method, which executes it, given the specified parameters. The Parameter definition and ReturnType can be passed into your implementation using the constructor. The implementation must be provided by implementing getFunctionValue(org.mmbase.util.functions.Parameters).

Since:
MMBase-1.8
Version:
$Id: AbstractFunction.java 41943 2010-04-16 17:18:19Z michiel $
Author:
Daniel Ockeloen, Michiel Meeuwissen
See Also:
Parameter, Parameters, Serialized Form

Field Summary
protected  boolean autoReturnType
           
protected  String name
           
protected  ReturnType<R> returnType
           
 
Constructor Summary
AbstractFunction(String name, Parameter<?>... def)
          Determines the ReturnType automaticly using the return type of getFunctionValue(Parameters).
AbstractFunction(String name, Parameter<?>[] def, ReturnType<R> returnType)
          Constructor for Function objects.
 
Method Summary
 int compareTo(Function<R> fun)
           
 Parameters createParameters()
          Creates an empty 'Parameters' object for you, which you have to fill and feed back to getFunctionValue
 boolean equals(Object o)
           
 String getDescription()
           
abstract  R getFunctionValue(Parameters parameters)
          Executes the defined function supplying the given arguments.
 R getFunctionValueWithArgs(Object... parameters)
          A 'varargs' version of getFunctionValueWithList.
 R getFunctionValueWithList(List<?> parameters)
          Executes the defined function supplying the given List of arguments.
 String getName()
          A function must have a name.
 Parameter<?>[] getParameterDefinition()
           
 ReturnType<R> getReturnType()
           
 int hashCode()
           
 void setDescription(String description)
          For documentational purposes a function object needs a description too.
 void setParameterDefinition(Parameter<?>[] params)
          A function object is of no use, as long as it lacks a definition.
 void setReturnType(ReturnType<R> type)
          Sets the ReturnType for this function if not set already.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected final String name

returnType

protected ReturnType<R> returnType

autoReturnType

protected boolean autoReturnType
Constructor Detail

AbstractFunction

public AbstractFunction(String name,
                        Parameter<?>[] def,
                        ReturnType<R> returnType)
Constructor for Function objects.

Parameters:
name - Every function must have a name
def - Every function must have a parameter definition. It can be left null and then filled later by setParameterDefinition(org.mmbase.util.functions.Parameter[])
returnType - Every function must also specify its return type. It can be left null and then filled later by setReturnType(org.mmbase.util.functions.ReturnType)

AbstractFunction

public AbstractFunction(String name,
                        Parameter<?>... def)
Determines the ReturnType automaticly using the return type of getFunctionValue(Parameters).

Since:
MMBase-1.9
Method Detail

createParameters

public Parameters createParameters()
Creates an empty 'Parameters' object for you, which you have to fill and feed back to getFunctionValue

Specified by:
createParameters in interface Function<R>
See Also:
getFunctionValue(Parameters)

getFunctionValue

public abstract R getFunctionValue(Parameters parameters)
Executes the defined function supplying the given arguments.

Specified by:
getFunctionValue in interface Function<R>
Parameters:
parameters - The parameters for the function. To specify an empty parameter list use Parameters.VOID. Implementors are encouraged to support null too.
Returns:
The function value, which can be of any type compatible to getReturnType()
See Also:
createParameters()

getFunctionValueWithList

public final R getFunctionValueWithList(List<?> parameters)
Executes the defined function supplying the given List of arguments. This is a convenience method, as the List is mapped to a Parameters type and passed to getFunctionValue(Parameters).

Specified by:
getFunctionValueWithList in interface Function<R>
Parameters:
parameters - The parameters for the function. To specify an empty parameter list use Parameters.VOID.
Returns:
The function value, which can be of any type compatible to getReturnType()

getFunctionValueWithArgs

public final R getFunctionValueWithArgs(Object... parameters)
Description copied from interface: Function
A 'varargs' version of getFunctionValueWithList.

Specified by:
getFunctionValueWithArgs in interface Function<R>
Since:
MMBase-1.9

setDescription

public void setDescription(String description)
For documentational purposes a function object needs a description too.

Specified by:
setDescription in interface Function<R>

getDescription

public String getDescription()
Specified by:
getDescription in interface Function<R>
See Also:
setDescription(String)

getName

public String getName()
A function must have a name. This is the name which was used to aquire the function object.

Specified by:
getName in interface Function<R>
Returns:
The function's name, never null

getParameterDefinition

public Parameter<?>[] getParameterDefinition()
Specified by:
getParameterDefinition in interface Function<R>
Returns:
The currently set Parameter definition array, or null if not set already.

setParameterDefinition

public void setParameterDefinition(Parameter<?>[] params)
A function object is of no use, as long as it lacks a definition.

Specified by:
setParameterDefinition in interface Function<R>
Parameters:
params - An array of Parameter objects.
Throws:
IllegalStateException - if there was already set a parameter defintion for this function object.

getReturnType

public ReturnType<R> getReturnType()
Specified by:
getReturnType in interface Function<R>
Returns:
The currently set ReturnType, or null if not set already.

setReturnType

public void setReturnType(ReturnType<R> type)
Sets the ReturnType for this function if not set already.

Specified by:
setReturnType in interface Function<R>
Parameters:
type - A ReturnType object. For void functions that could be ReturnType.VOID.
Throws:
IllegalStateException - if there was already set a return type for this function object.

compareTo

public int compareTo(Function<R> fun)
Specified by:
compareTo in interface Comparable<Function<R>>

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object
See Also:
Object.hashCode()

toString

public String toString()
Overrides:
toString in class Object


MMBase 2.0-SNAPSHOT - null