KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.mmbase.util.logging.Logger;
15 import org.mmbase.util.logging.Logging;
16
17 /**
18  * A function provider maintains a set of {@link Function} objects.
19  *
20  * @since MMBase-1.8
21  * @author Pierre van Rooden
22  * @version $Id: FunctionProvider.java,v 1.11 2005/07/28 17:07:55 michiel Exp $
23  */

24 public abstract class FunctionProvider {
25     private static final Logger log = Logging.getLoggerInstance(FunctionProvider.class);
26
27     protected Map functions = Collections.synchronizedMap(new HashMap());
28     /**
29      * Every Function Provider provides least the 'getFunctions' function, which returns a Set of all functions which it provides.
30      */

31     protected Function getFunctions = new AbstractFunction("getFunctions", Parameter.EMPTY, ReturnType.COLLECTION) {
32             {
33                 setDescription("The 'getFunctions' returns the collections of al Function object which are available on this FunctionProvider");
34             }
35             public Object JavaDoc getFunctionValue(Parameters arguments) {
36                 return getFunctions();
37             }
38         };
39     {
40         addFunction(getFunctions);
41     }
42
43
44     /**
45      * The constructor of an FunctionProvider two things. It adds the 'getFunction' function, and it
46      * guesses other function using reflection.
47      * @todo Should this last thing not only be done on MMObjectBuilders?
48      */

49     public FunctionProvider() {
50         // determine parameters through reflection
51
Map parameterDefinitions = Functions.getParameterDefinitonsByReflection(this.getClass(), new HashMap());
52         try {
53             for (Iterator i = parameterDefinitions.entrySet().iterator(); i.hasNext(); ) {
54                 Map.Entry entry = (Map.Entry) i.next();
55                 Function fun = newFunctionInstance((String JavaDoc)entry.getKey(), (Parameter[])entry.getValue(), ReturnType.UNKNOWN);
56                 fun.setDescription("Function automaticly found by reflection on public Parameter[] members");
57                 addFunction(fun);
58             }
59         } catch (UnsupportedOperationException JavaDoc uoo) {
60             log.warn("Found parameter definition array in " + this.getClass() + " but newFunctionInstance was not implemented for that");
61         }
62     }
63
64     protected Function newFunctionInstance(String JavaDoc name, Parameter[] parameters, ReturnType returnType) {
65         throw new UnsupportedOperationException JavaDoc("This class is not a fully implemented function-provider");
66     }
67
68
69     /**
70      * Adds a function to the FunctionProvider. So, you can implement any function and add it to the
71      * provider, to make it provide this function too.
72      */

73     public void addFunction(Function function) {
74         Object JavaDoc oldValue = functions.put(function.getName(), function);
75         if (oldValue != null) {
76             log.debug("Replaced " + oldValue + " by " + function);
77         }
78     }
79
80     /**
81      * Creates a new empty Parameters object for given function.
82      * @return A new empty Parameters object, or <code>null</code> if no such function.
83      */

84     public Parameters createParameters(String JavaDoc functionName) {
85         Function function = getFunction(functionName);
86         if (function != null) {
87             return function.createParameters();
88         } else {
89             return null;
90         }
91     }
92
93     /**
94      * Executes a function, and returns the function value.
95      * @return The function value or <code>null</code> if no such function.
96      */

97     public Object JavaDoc getFunctionValue(String JavaDoc functionName, List parameters) {
98         Function function = getFunction(functionName);
99         if (function != null) {
100             return function.getFunctionValueWithList(parameters);
101         } else {
102             return null;
103         }
104     }
105
106     /**
107      * Returns the Function object with given name.
108      * @return Function object or <code>null</code> if no such function is provided.
109      */

110     public Function getFunction(String JavaDoc functionName) {
111         return (Function)functions.get(functionName);
112     }
113
114     /**
115      * Returns a Collection of all functions currently provided by the FunctionProvider.
116      */

117     public Collection getFunctions() {
118         return Collections.unmodifiableCollection(functions.values());
119     }
120
121 }
122
Popular Tags