Skip to content

Commit b729f8f

Browse files
Merge pull request ReactiveX#3 from benjchristensen/clojure-adaptor
Clojure adaptor
2 parents 734c728 + 1030479 commit b729f8f

File tree

4 files changed

+148
-10
lines changed

4 files changed

+148
-10
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apply plugin: 'java'
2+
dependencies {
3+
compile project(':rxjava-core')
4+
compile 'org.clojure:clojure:1.5.+'
5+
provided 'junit:junit:4.10'
6+
provided 'org.mockito:mockito-core:1.9.5'
7+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* Copyright 2013 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.rx.lang.clojure;
17+
18+
import static org.mockito.Mockito.*;
19+
20+
import java.util.Arrays;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.mockito.Mock;
25+
import org.mockito.MockitoAnnotations;
26+
import org.rx.functions.FunctionLanguageAdaptor;
27+
import org.rx.reactive.Observer;
28+
29+
import clojure.lang.IFn;
30+
import clojure.lang.RT;
31+
import clojure.lang.Var;
32+
33+
public class ClojureAdaptor implements FunctionLanguageAdaptor {
34+
35+
@Override
36+
public Object call(Object function, Object[] args) {
37+
if (args.length == 0) {
38+
return ((IFn) function).invoke();
39+
} else if (args.length == 1) {
40+
return ((IFn) function).invoke(args[0]);
41+
} else if (args.length == 2) {
42+
return ((IFn) function).invoke(args[0], args[1]);
43+
} else if (args.length == 3) {
44+
return ((IFn) function).invoke(args[0], args[1], args[2]);
45+
} else if (args.length == 4) {
46+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3]);
47+
} else if (args.length == 5) {
48+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4]);
49+
} else if (args.length == 6) {
50+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5]);
51+
} else if (args.length == 7) {
52+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
53+
} else if (args.length == 8) {
54+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
55+
} else if (args.length == 9) {
56+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
57+
} else if (args.length == 10) {
58+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
59+
} else if (args.length == 11) {
60+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
61+
} else if (args.length == 12) {
62+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11]);
63+
} else if (args.length == 13) {
64+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12]);
65+
} else if (args.length == 14) {
66+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13]);
67+
} else if (args.length == 15) {
68+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14]);
69+
} else if (args.length == 16) {
70+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14], args[15]);
71+
} else if (args.length == 17) {
72+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14], args[15], args[16]);
73+
} else if (args.length == 18) {
74+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14], args[15], args[16], args[17]);
75+
} else if (args.length == 19) {
76+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14], args[15], args[16], args[17], args[18]);
77+
} else if (args.length == 20) {
78+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14], args[15], args[16], args[17], args[18], args[19]);
79+
} else {
80+
return ((IFn) function).invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11], args[12], args[13], args[14], args[15], args[16], args[17], args[18], args[19], Arrays.copyOfRange(args, 20, args.length));
81+
}
82+
}
83+
84+
@Override
85+
public Class<?> getFunctionClass() {
86+
return IFn.class;
87+
}
88+
89+
public static class UnitTest {
90+
91+
@Mock
92+
ScriptAssertion assertion;
93+
94+
@Mock
95+
Observer<Integer> w;
96+
97+
@Before
98+
public void before() {
99+
MockitoAnnotations.initMocks(this);
100+
}
101+
102+
@Test
103+
public void testTake() {
104+
runClojureScript("(-> (org.rx.reactive.Observable/toObservable [\"one\" \"two\" \"three\"]) (.take 2) (.subscribe (fn [arg] (println arg))))");
105+
}
106+
107+
// commented out for now as I can't figure out how to set the var 'a' with the 'assertion' instance when running the code from java
108+
// @Test
109+
// public void testFilter() {
110+
// runClojureScript("(-> (org.rx.reactive.Observable/toObservable [1 2 3]) (.filter (fn [v] (>= v 2))) (.subscribe (fn [result] (a.received(result)))))");
111+
// verify(assertion, times(0)).received(1);
112+
// verify(assertion, times(1)).received(2);
113+
// verify(assertion, times(1)).received(3);
114+
// }
115+
116+
private static interface ScriptAssertion {
117+
public void error(Exception o);
118+
119+
public void received(Object o);
120+
}
121+
122+
private void runClojureScript(String script) {
123+
Object code = RT.var("clojure.core", "read-string").invoke(script);
124+
Var eval = RT.var("clojure.core", "eval");
125+
Object result = eval.invoke(code);
126+
System.out.println("Result: " + result);
127+
}
128+
}
129+
}

rxjava-core/src/main/java/org/rx/reactive/Observable.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2013 Netflix, Inc.
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -136,9 +136,10 @@ public void onNext(Object args) {
136136
}
137137

138138
@SuppressWarnings({ "rawtypes", "unchecked" })
139-
public Subscription subscribe(final Object onNext) {
140-
if (onNext instanceof Observer) {
141-
throw new RuntimeException("Observers are not intended to be passed to this generic method. Your generic type is most likely wrong. This method is for dynamic code to send in closures.");
139+
public Subscription subscribe(final Object o) {
140+
if (o instanceof Observer) {
141+
// in case a dynamic language is not correctly handling the overloaded methods and we receive an Observer just forward to the correct method.
142+
return subscribe((Observer) o);
142143
}
143144
return subscribe(new Observer() {
144145

@@ -152,10 +153,10 @@ public void onError(Exception e) {
152153
}
153154

154155
public void onNext(Object args) {
155-
if (onNext == null) {
156+
if (o == null) {
156157
throw new RuntimeException("onNext must be implemented");
157158
}
158-
executeCallback(onNext, args);
159+
executeCallback(o, args);
159160
}
160161

161162
});

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rootProject.name='rxjava'
22
include 'rxjava-core', \
33
'language-adaptors:rxjava-groovy', \
4-
'language-adaptors:rxjava-jruby'
4+
'language-adaptors:rxjava-jruby', \
5+
'language-adaptors:rxjava-clojure'

0 commit comments

Comments
 (0)