KickJava   Java API By Example, From Geeks To Geeks.

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


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
11 package org.mmbase.util.functions;
12
13 import org.mmbase.bridge.*;
14 import org.mmbase.core.AbstractDescriptor;
15 import org.mmbase.datatypes.*;
16 import org.mmbase.util.*;
17 import org.mmbase.util.logging.*;
18 import java.util.*;
19 import java.io.*;
20
21 /**
22  * Each (function) argument is specified by a Parameter object.
23  * A Parameter contains a name and type (it does <em>not</em> contain a value). An array of this is returned by
24  * {@link Function#getParameterDefinition}, and this same array is used to create new empty {@link Parameters}
25  * object (by {@link Function#createParameters}), which can contain actual values for each Parameter.
26  *
27  * @author Daniel Ockeloen (MMFunctionParam)
28  * @author Michiel Meeuwissen
29  * @since MMBase-1.7
30  * @version $Id: Parameter.java,v 1.30 2006/06/20 20:13:55 michiel Exp $
31  * @see Parameters
32  */

33
34 public class Parameter extends AbstractDescriptor implements java.io.Serializable JavaDoc {
35     private static final Logger log = Logging.getLoggerInstance(Parameter.class);
36
37     private static final long serialVersionUID = 1L;
38     /**
39      * Parameter which might be needed in lots of Parameter definitions. These parameters are
40      * 'standard' parameters, which can be filled in by the system. E.g. the mmbase taglib uses
41      * these constants, and if it has a cloud ('mm:cloud is used'), then cloud-parameters are filled
42      * automaticly.
43      */

44     public static final Parameter LANGUAGE = new Parameter("language", String JavaDoc.class);
45     public static final Parameter LOCALE = new Parameter("locale", Locale.class);
46     public static final Parameter USER = new Parameter("user", org.mmbase.security.UserContext.class);
47     public static final Parameter RESPONSE = new Parameter("response", javax.servlet.http.HttpServletResponse JavaDoc.class);
48     public static final Parameter REQUEST = new Parameter("request", javax.servlet.http.HttpServletRequest JavaDoc.class);
49     public static final Parameter CLOUD = new Parameter("cloud", org.mmbase.bridge.Cloud.class);
50
51     /**
52      * 'system' parameter set for nodefunctions.
53      * @since MMBaes-1.8
54      */

55     public static final Parameter NODE = new Parameter("_node", org.mmbase.bridge.Node.class);
56     public final static Parameter CORENODE = new Parameter("_corenode", Object JavaDoc.class); // object because otherwise problems with RMMCI which doesn't have MMObjectNode.
57

58
59
60     public static final Parameter FIELD = new Parameter("field", String JavaDoc.class);
61
62     /**
63      * An empty Parameter array.
64      */

65     public static final Parameter[] EMPTY = new Parameter[0];
66
67
68     // implementation of serializable, I hate java. Cannot make AbstractDescriptor Serializable, so doing it here.... sigh sigh.
69
// If you would make AbstractDescriptor Serializable, CoreField will become Serializable and MMObjectBuilder needs to be serializable then (because it is a member of CoreField).
70
private void writeObject(ObjectOutputStream out) throws IOException {
71         out.writeUTF(key);
72         out.writeObject(description);
73         out.writeObject(guiName);
74         out.writeObject(dataType);
75     }
76     // implementation of serializable
77
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException JavaDoc {
78         key = in.readUTF();
79         description = (LocalizedString) in.readObject();
80         guiName = (LocalizedString) in.readObject();
81         dataType = (DataType) in.readObject();
82     }
83
84     /**
85      * The parameter's data type
86      * @since MMBase-1.8
87      */

88     protected DataType dataType;
89
90     /**
91      * Create a Parameter object
92      * @param name the name of the parameter
93      * @param dataType the datatype of the parameter to copy
94      * @since MMBase-1.8
95      */

96     public Parameter(String JavaDoc name, DataType dataType) {
97         this(name, dataType, true);
98     }
99
100     /**
101      * Create a Parameter object
102      * @param name the name of the parameter
103      * @param dataType the datatype of the parameter to assign or copy
104      * @param copy if <code>true</code>, teh datatype is copied. if not, it is assigned directly,
105      * that is, changing condfiitons on the parameter changes the passed datatype instance.
106      * @since MMBase-1.8
107      */

108     public Parameter(String JavaDoc name, DataType dataType, boolean copy) {
109         super(name);
110         if (copy) {
111             this.dataType = (DataType)dataType.clone(name);
112         } else {
113             this.dataType = dataType;
114         }
115     }
116
117     /**
118      * Create a Parameter object
119      * @param name the name of the parameter
120      * @param type the class of the parameter's possible value
121      */

122     public Parameter(String JavaDoc name, Class JavaDoc type) {
123         super(name);
124         dataType = DataTypes.createDataType(name, type);
125     }
126
127     /**
128      * Create a Parameter object
129      * @param name the name of the parameter
130      * @param type the class of the parameter's possible value
131      * @param required whether the parameter requires a value
132      */

133     public Parameter(String JavaDoc name, Class JavaDoc type, boolean required) {
134         this(name,type);
135         dataType.setRequired(required);
136     }
137
138     /**
139      * Create a Parameter object
140      * @param name the name of the parameter
141      * @param type the class of the parameter's possible value
142      * @param defaultValue the value to use if the parameter has no value set
143      */

144     public Parameter(String JavaDoc name, Class JavaDoc type, Object JavaDoc defaultValue) {
145         this(name,type);
146         dataType.setDefaultValue(defaultValue);
147     }
148
149     /**
150      * Copy-constructor, just to copy it with different requiredness
151      */

152     public Parameter(Parameter p, boolean required) {
153         this(p.key, p.getDataType());
154         dataType.setRequired(required);
155     }
156
157     /**
158      * Copy-constructor, just to copy it with different defaultValue (which implies that it is not required now)
159      */

160     public Parameter(Parameter p, Object JavaDoc defaultValue) {
161         this(p.key, p.getDataType());
162         dataType.setDefaultValue(defaultValue);
163     }
164
165     /**
166      * Returns the default value of this parameter (derived from the datatype).
167      * @return the default value
168      */

169     public Object JavaDoc getDefaultValue() {
170         return dataType.getDefaultValue();
171     }
172
173     /**
174      * Sets the default value of this parameter.
175      * @param defaultValue the default value
176      */

177     public void setDefaultValue(Object JavaDoc defaultValue) {
178         dataType.setDefaultValue(defaultValue);
179     }
180
181     /**
182      * Returns the data type of this parameter.
183      * @return the datatype
184      * @since MMBase-1.8
185      */

186     public DataType getDataType() {
187         return dataType;
188     }
189
190     /**
191      * Returns the type of values that this parameter accepts.
192      * @return the type as a Class
193      */

194     public Class JavaDoc getTypeAsClass() {
195         return dataType.getTypeAsClass();
196     }
197
198     /**
199      * Returns whether the parameter requires a value.
200      * @return <code>true</code> if a value is required
201      */

202     public boolean isRequired() {
203         return dataType.isRequired();
204     }
205
206     /**
207      * Checks if the passed object is of the correct class (compatible with the type of this Parameter),
208      * and throws an IllegalArgumentException if it doesn't.
209      * @param value teh value whose type (class) to check
210      * @throws IllegalArgumentException if the type is not compatible
211      */

212     public void checkType(Object JavaDoc value) {
213         dataType.checkType(value);
214     }
215
216
217     /**
218      * Tries to 'cast' an object for use with this parameter. E.g. if value is a String, but this
219      * parameter is of type Integer, then the string can be parsed to Integer.
220      * @param value The value to be filled in in this Parameter.
221      */

222     protected Object JavaDoc autoCast(Object JavaDoc value) {
223         return dataType.cast(value, null, null);
224     }
225
226     public int hashCode() {
227         return getName().hashCode() + 13 * getDataType().hashCode();
228     }
229
230     /**
231      * Whether parameter equals to other parameter. Only key and type are consided. DefaultValue and
232      * required propererties are only 'utilities'.
233      * @return true if o is Parameter of which key and type equal to this' key and type.
234      */

235     public boolean equals(Object JavaDoc o) {
236         if (o instanceof Parameter) {
237             Parameter a = (Parameter) o;
238             return a.getName().equals(getName()) && a.getDataType().equals(getDataType());
239         }
240         return false;
241     }
242
243     public String JavaDoc toString() {
244         return getTypeAsClass().getName() + " " + getName();
245     }
246
247     /**
248      * A Parameter.Wrapper wraps one Parameter around a Parameter[] (then you can put it in a
249      * Parameter[]). Parameters will recognize this. This can be used when you 'extend'
250      * functionality, and add more parameters. The Parameter array can contain such a
251      * Parameter.Wrapper object containing the original Parameter array.
252      */

253     public static class Wrapper extends Parameter {
254         Parameter[] arguments;
255
256         public Wrapper(Parameter[] arg) {
257             super("[ARRAYWRAPPER]", Parameter[].class);
258             arguments = arg;
259         }
260
261         // this toString makes the wrapping invisible in the toString of a wrapping Parameter[]
262
public String JavaDoc toString() {
263             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
264             for (int i = 0 ; i < arguments.length; i++) {
265                 if (i > 0) buf.append(", ");
266                 buf.append(arguments[i].toString());
267
268             }
269             return buf.toString();
270
271         }
272     }
273
274
275 }
276
Popular Tags