KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > functions > AbstractFunction


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.functions;
11
12 import java.util.*;
13
14 /**
15  * An abstract representation of a piece of functionality (a 'function'). A function has a name, a
16  * return type, and a parameter-definition (which is a {@link Parameter} array).
17  *
18  * The goal of a Function object is to call its {@link #getFunctionValue(Parameters)} method, which
19  * executes it, given the specified parameters.
20  *
21  *
22  * @author Daniel Ockeloen
23  * @author Michiel Meeuwissen
24  * @version $Id: AbstractFunction.java,v 1.11 2006/02/14 22:52:33 michiel Exp $
25  * @since MMBase-1.8
26  * @see Parameter
27  * @see Parameters
28  */

29 abstract public class AbstractFunction implements Function, Comparable JavaDoc, java.io.Serializable JavaDoc {
30
31     protected String JavaDoc name;
32     protected ReturnType returnType;
33
34     private Parameter[] parameterDefinition;
35     private String JavaDoc description;
36
37     /**
38      * Constructor for Function objects.
39      * @param name Every function must have a name
40      * @param def Every function must have a parameter definition. It can be left <code>null</code> and then filled later by {@link #setParameterDefinition}
41      * @param returnType Every function must also specify its return type. It can be left <code>null</code> and then filled later by {@link #setReturnType}
42      */

43     public AbstractFunction(String JavaDoc name, Parameter[] def, ReturnType returnType) {
44         this.name = name;
45         if (def != null){
46             this.parameterDefinition = (Parameter[]) Functions.define(def, new ArrayList()).toArray(Parameter.EMPTY);
47         }
48         this.returnType = returnType;
49     }
50
51     /**
52      * Creates an empty 'Parameters' object for you, which you have to fill and feed back to getFunctionValue
53      * @see #getFunctionValue(Parameters)
54      */

55     public Parameters createParameters() {
56         if (parameterDefinition == null) {
57             throw new IllegalStateException JavaDoc("Definition is not set yet");
58         }
59         return new Parameters(parameterDefinition);
60     }
61
62     /**
63      * Executes the defined function supplying the given arguments.
64      * @see #createParameters
65      * @param parameters The parameters for the function. To specify an empty parameter list use {@link Parameters#VOID}.
66      * Implementors are encouraged to support <code>null</code> too.
67      * @return The function value, which can be of any type compatible to {@link #getReturnType}
68      */

69     abstract public Object JavaDoc getFunctionValue(Parameters parameters);
70
71     /**
72      * Executes the defined function supplying the given List of arguments.
73      * This is a convenience method, as the List is mapped to a Parameters type and passed to {@link #getFunctionValue(Parameters)}.
74      * @param parameters The parameters for the function. To specify an empty parameter list use {@link Parameters#VOID}.
75      *
76      * @return The function value, which can be of any type compatible to {@link #getReturnType}
77      */

78      public final Object JavaDoc getFunctionValueWithList(List parameters) {
79          if (parameters instanceof Parameters) {
80              return getFunctionValue((Parameters)parameters);
81          } else {
82              return getFunctionValue(new Parameters(parameterDefinition, parameters));
83          }
84      }
85
86     /**
87      * For documentational purposes a function object needs a description too.
88      */

89     public void setDescription(String JavaDoc description) {
90         this.description = description;
91     }
92
93     /**
94      * @see #setDescription(String)
95      */

96     public String JavaDoc getDescription() {
97         return description;
98     }
99
100     /**
101      * A function <em>must</em> have a name. This is the name which was used to aquire the function object.
102      * @return The function's name, never <code>null</code>
103      */

104     public String JavaDoc getName() {
105         return name;
106     }
107
108     /**
109      * @return The currently set Parameter definition array, or <code>null</code> if not set already.
110      */

111     public Parameter[] getParameterDefinition() {
112         return parameterDefinition;
113     }
114
115     /**
116      * A function object is of no use, as long as it lacks a definition.
117      * @param params An array of Parameter objects.
118      * @throws IllegalStateException if there was already set a parameter defintion for this function object.
119      */

120     public void setParameterDefinition(Parameter[] params) {
121         if (parameterDefinition != null) {
122             throw new IllegalStateException JavaDoc("Definition is set already");
123         }
124         parameterDefinition = (Parameter[]) Functions.define(params, new ArrayList()).toArray(Parameter.EMPTY);
125     }
126
127
128     /**
129      * @return The currently set ReturnType, or <code>null</code> if not set already.
130      */

131     public ReturnType getReturnType() {
132         return returnType;
133     }
134     /**
135      * Sets the ReturnType for this function if not set already.
136      * @param type A ReturnType object. For void functions that could be {@link ReturnType#VOID}.
137      * @throws IllegalStateException if there was already set a return type for this function object.
138      */

139     public void setReturnType(ReturnType type) {
140         if (returnType != null) {
141             throw new IllegalStateException JavaDoc("Returntype is set already");
142         }
143         returnType = type;
144     }
145
146     public int compareTo(Object JavaDoc o) {
147         Function fun = (Function) o;
148         return name.compareTo(fun.getName());
149     }
150
151     public boolean equals(Object JavaDoc o) {
152         if (o == this) return true;
153         if (o == null) return false;
154         return (o instanceof Function) && ((Function)o).getName().equals(name);
155     }
156
157
158     /**
159      * @see java.lang.Object#hashCode()
160      */

161     public int hashCode() {
162         return name.hashCode();
163     }
164
165     public String JavaDoc toString() {
166         return "" + returnType + " " + getName() + Arrays.asList(parameterDefinition);
167     }
168
169 }
170
Popular Tags