1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.onemind.commons.invoke;
22
23 import java.util.*;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.onemind.commons.java.lang.reflect.ReflectUtils;
27 /***
28 * An invocable that contain a list of invocable functions to be invoke upon. In current implementation, it can only contains on
29 * function with the specific name (regardless of arg types)
30 * TODO: improve to allow multiple functions with same name (different args)
31 * @todo improve to allow multiple functions with same name (different args)
32 * @author TiongHiang Lee (thlee@onemindsoft.org)
33 * @version $Id: AbstractInvocable.java,v 1.1 2005/01/24 05:51:36 thlee Exp $ $Name: $
34 * @todo extend to allow multiple method with same name but different arg types
35 * TODO extend to allow multiple method with same name but different arg types
36 */
37 public abstract class AbstractInvocable implements Invocable
38 {
39
40 /*** the functions **/
41 private Map _functions = new HashMap();
42
43 /***
44 * Add an function
45 * @param functionName the function name
46 * @param function the function
47 */
48 public void addFunction(InvocableFunction function)
49 {
50 Object key = ReflectUtils.toMethodString(function.getName(), function.getArgTypes());
51 _functions.put(key, function);
52 }
53
54 /***
55 * {@inheritDoc}
56 */
57 public InvocableFunction getFunction(String functionName, Object[] args)
58 {
59 return getFunction(functionName, ReflectUtils.toArgTypes(args));
60 }
61
62 /***
63 * {@inheritDoc}
64 */
65 public InvocableFunction getFunction(String functionName, Class[] argTypes)
66 {
67 Object key = ReflectUtils.toMethodString(functionName, argTypes);
68 InvocableFunction func = (InvocableFunction) _functions.get(key);
69 if (func != null)
70 {
71 return func;
72 } else
73 {
74
75 Iterator it = _functions.values().iterator();
76 while (it.hasNext())
77 {
78 func = (InvocableFunction) it.next();
79 if (functionName.equals(func.getName()) && func.canInvokeOn(argTypes))
80 {
81 return func;
82 }
83 }
84 return null;
85 }
86 }
87
88 /***
89 * {@inheritDoc}
90 */
91 public boolean canInvoke(String functionName, Object[] args)
92 {
93 InvocableFunction f = getFunction(functionName, args);
94 return (f != null);
95 }
96
97 /***
98 * {@inheritDoc}
99 */
100 public Object invoke(String functionName, Object[] args) throws Exception
101 {
102 InvocableFunction function = getFunction(functionName, args);
103 if (function != null)
104 {
105 return function.invoke(this, args);
106 } else
107 {
108 throw new NoSuchMethodException("Method " + ReflectUtils.toMethodString(functionName, args) + " not found for " + this);
109 }
110 }
111
112 /***
113 * Get all the functions
114 * @return the functions
115 */
116 public Collection getFunctions()
117 {
118 return Collections.unmodifiableCollection(_functions.values());
119 }
120 }