| 1 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 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 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 getFunctionValue(Parameters arguments) { 36 return getFunctions(); 37 } 38 }; 39 { 40 addFunction(getFunctions); 41 } 42 43 44 49 public FunctionProvider() { 50 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 )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 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 name, Parameter[] parameters, ReturnType returnType) { 65 throw new UnsupportedOperationException ("This class is not a fully implemented function-provider"); 66 } 67 68 69 73 public void addFunction(Function function) { 74 Object oldValue = functions.put(function.getName(), function); 75 if (oldValue != null) { 76 log.debug("Replaced " + oldValue + " by " + function); 77 } 78 } 79 80 84 public Parameters createParameters(String functionName) { 85 Function function = getFunction(functionName); 86 if (function != null) { 87 return function.createParameters(); 88 } else { 89 return null; 90 } 91 } 92 93 97 public Object getFunctionValue(String 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 110 public Function getFunction(String functionName) { 111 return (Function)functions.get(functionName); 112 } 113 114 117 public Collection getFunctions() { 118 return Collections.unmodifiableCollection(functions.values()); 119 } 120 121 } 122 | Popular Tags |