18
18
import java .util .Collection ;
19
19
import java .util .concurrent .ConcurrentHashMap ;
20
20
21
- /**
22
- * Allows execution of functions from multiple different languages.
23
- * <p>
24
- * Language support is provided via implementations of {@link FunctionLanguageAdaptor}.
25
- * <p>
26
- * This class will dynamically look for known language adaptors on the classpath at startup or new ones can be registered using {@link #registerLanguageAdaptor(Class[], FunctionLanguageAdaptor)}.
27
- */
28
21
public class Functions {
29
22
30
- private final static ConcurrentHashMap <Class <?>, FunctionLanguageAdaptor > languageAdaptors = new ConcurrentHashMap <Class <?>, FunctionLanguageAdaptor >();
31
-
32
- static {
33
- /* optimistically look for supported languages if they are in the classpath */
34
- loadLanguageAdaptor ("Groovy" );
35
- loadLanguageAdaptor ("JRuby" );
36
- loadLanguageAdaptor ("Clojure" );
37
- loadLanguageAdaptor ("Scala" );
38
- // as new languages arise we can add them here but this does not prevent someone from using 'registerLanguageAdaptor' directly
39
- }
40
-
41
- private static boolean loadLanguageAdaptor (String name ) {
42
- String className = "rx.lang." + name .toLowerCase () + "." + name + "Adaptor" ;
43
- try {
44
- Class <?> c = Class .forName (className );
45
- FunctionLanguageAdaptor a = (FunctionLanguageAdaptor ) c .newInstance ();
46
- registerLanguageAdaptor (a .getFunctionClass (), a );
47
- /*
48
- * Using System.err/System.out as this is the only place in the library where we do logging and it's only at startup.
49
- * I don't want to include SL4J/Log4j just for this and no one uses Java Logging.
50
- */
51
- System .out .println ("RxJava => Successfully loaded function language adaptor: " + name + " with path: " + className );
52
- } catch (ClassNotFoundException e ) {
53
- System .err .println ("RxJava => Could not find function language adaptor: " + name + " with path: " + className );
54
- return false ;
55
- } catch (Throwable e ) {
56
- System .err .println ("RxJava => Failed trying to initialize function language adaptor: " + className );
57
- e .printStackTrace ();
58
- return false ;
59
- }
60
- return true ;
61
- }
62
-
63
- public static void registerLanguageAdaptor (Class <?>[] functionClasses , FunctionLanguageAdaptor adaptor ) {
64
- for (Class <?> functionClass : functionClasses ) {
65
- if (functionClass .getPackage ().getName ().startsWith ("java." )) {
66
- throw new IllegalArgumentException ("FunctionLanguageAdaptor implementations can not specify java.lang.* classes." );
67
- }
68
- languageAdaptors .put (functionClass , adaptor );
69
- }
70
- }
71
-
72
- public static void removeLanguageAdaptor (Class <?> functionClass ) {
73
- languageAdaptors .remove (functionClass );
74
- }
75
-
76
- public static Collection <FunctionLanguageAdaptor > getRegisteredLanguageAdaptors () {
77
- return languageAdaptors .values ();
78
- }
79
-
80
23
/**
81
24
* Utility method for determining the type of closure/function and executing it.
82
25
*
@@ -91,131 +34,11 @@ public static FuncN from(final Object function) {
91
34
/* check for typed Rx Function implementation first */
92
35
if (function instanceof Function ) {
93
36
return fromFunction ((Function ) function );
94
- } else {
95
- /* not an Rx Function so try language adaptors */
96
-
97
- // check for language adaptor
98
- for (final Class c : languageAdaptors .keySet ()) {
99
- if (c .isInstance (function )) {
100
- final FunctionLanguageAdaptor la = languageAdaptors .get (c );
101
- // found the language adaptor so wrap in FuncN and return
102
- return new FuncN () {
103
-
104
- @ Override
105
- public Object call (Object ... args ) {
106
- return la .call (function , args );
107
- }
108
-
109
- };
110
- }
111
- }
112
- // no language adaptor found
113
37
}
114
-
115
38
// no support found
116
39
throw new RuntimeException ("Unsupported closure type: " + function .getClass ().getSimpleName ());
117
40
}
118
41
119
- //
120
- // @SuppressWarnings("unchecked")
121
- // private static <R> R executionRxFunction(Function function, Object... args) {
122
- // // check Func* classes
123
- // if (function instanceof Func0) {
124
- // Func0<R> f = (Func0<R>) function;
125
- // if (args.length != 0) {
126
- // throw new RuntimeException("The closure was Func0 and expected no arguments, but we received: " + args.length);
127
- // }
128
- // return (R) f.call();
129
- // } else if (function instanceof Func1) {
130
- // Func1<Object, R> f = (Func1<Object, R>) function;
131
- // if (args.length != 1) {
132
- // throw new RuntimeException("The closure was Func1 and expected 1 argument, but we received: " + args.length);
133
- // }
134
- // return f.call(args[0]);
135
- // } else if (function instanceof Func2) {
136
- // Func2<Object, Object, R> f = (Func2<Object, Object, R>) function;
137
- // if (args.length != 2) {
138
- // throw new RuntimeException("The closure was Func2 and expected 2 arguments, but we received: " + args.length);
139
- // }
140
- // return f.call(args[0], args[1]);
141
- // } else if (function instanceof Func3) {
142
- // Func3<Object, Object, Object, R> f = (Func3<Object, Object, Object, R>) function;
143
- // if (args.length != 3) {
144
- // throw new RuntimeException("The closure was Func3 and expected 3 arguments, but we received: " + args.length);
145
- // }
146
- // return (R) f.call(args[0], args[1], args[2]);
147
- // } else if (function instanceof Func4) {
148
- // Func4<Object, Object, Object, Object, R> f = (Func4<Object, Object, Object, Object, R>) function;
149
- // if (args.length != 1) {
150
- // throw new RuntimeException("The closure was Func4 and expected 4 arguments, but we received: " + args.length);
151
- // }
152
- // return f.call(args[0], args[1], args[2], args[3]);
153
- // } else if (function instanceof Func5) {
154
- // Func5<Object, Object, Object, Object, Object, R> f = (Func5<Object, Object, Object, Object, Object, R>) function;
155
- // if (args.length != 1) {
156
- // throw new RuntimeException("The closure was Func5 and expected 5 arguments, but we received: " + args.length);
157
- // }
158
- // return f.call(args[0], args[1], args[2], args[3], args[4]);
159
- // } else if (function instanceof Func6) {
160
- // Func6<Object, Object, Object, Object, Object, Object, R> f = (Func6<Object, Object, Object, Object, Object, Object, R>) function;
161
- // if (args.length != 1) {
162
- // throw new RuntimeException("The closure was Func6 and expected 6 arguments, but we received: " + args.length);
163
- // }
164
- // return f.call(args[0], args[1], args[2], args[3], args[4], args[5]);
165
- // } else if (function instanceof Func7) {
166
- // Func7<Object, Object, Object, Object, Object, Object, Object, R> f = (Func7<Object, Object, Object, Object, Object, Object, Object, R>) function;
167
- // if (args.length != 1) {
168
- // throw new RuntimeException("The closure was Func7 and expected 7 arguments, but we received: " + args.length);
169
- // }
170
- // return f.call(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
171
- // } else if (function instanceof Func8) {
172
- // Func8<Object, Object, Object, Object, Object, Object, Object, Object, R> f = (Func8<Object, Object, Object, Object, Object, Object, Object, Object, R>) function;
173
- // if (args.length != 1) {
174
- // throw new RuntimeException("The closure was Func8 and expected 8 arguments, but we received: " + args.length);
175
- // }
176
- // return f.call(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
177
- // } else if (function instanceof Func9) {
178
- // Func9<Object, Object, Object, Object, Object, Object, Object, Object, Object, R> f = (Func9<Object, Object, Object, Object, Object, Object, Object, Object, Object, R>) function;
179
- // if (args.length != 1) {
180
- // throw new RuntimeException("The closure was Func9 and expected 9 arguments, but we received: " + args.length);
181
- // }
182
- // return f.call(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
183
- // } else if (function instanceof FuncN) {
184
- // FuncN<R> f = (FuncN<R>) function;
185
- // return f.call(args);
186
- // } else if (function instanceof Action0) {
187
- // Action0 f = (Action0) function;
188
- // if (args.length != 1) {
189
- // throw new RuntimeException("The closure was Action0 and expected 0 arguments, but we received: " + args.length);
190
- // }
191
- // f.call();
192
- // return null;
193
- // } else if (function instanceof Action1) {
194
- // Action1<Object> f = (Action1<Object>) function;
195
- // if (args.length != 1) {
196
- // throw new RuntimeException("The closure was Action1 and expected 1 argument, but we received: " + args.length);
197
- // }
198
- // f.call(args[0]);
199
- // return null;
200
- // } else if (function instanceof Action2) {
201
- // Action2<Object, Object> f = (Action2<Object, Object>) function;
202
- // if (args.length != 1) {
203
- // throw new RuntimeException("The closure was Action2 and expected 2 argument, but we received: " + args.length);
204
- // }
205
- // f.call(args[0], args[1]);
206
- // return null;
207
- // } else if (function instanceof Action3) {
208
- // Action3<Object, Object, Object> f = (Action3<Object, Object, Object>) function;
209
- // if (args.length != 1) {
210
- // throw new RuntimeException("The closure was Action1 and expected 1 argument, but we received: " + args.length);
211
- // }
212
- // f.call(args[0], args[1], args[2]);
213
- // return null;
214
- // }
215
- //
216
- // throw new RuntimeException("Unknown implementation of Function: " + function.getClass().getSimpleName());
217
- // }
218
-
219
42
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
220
43
private static FuncN fromFunction (Function function ) {
221
44
// check Func* classes
0 commit comments