Skip to content

Commit fc0a706

Browse files
author
Mike Ragalie
committed
First pass at jruby compatibility
1 parent fa72a27 commit fc0a706

File tree

4 files changed

+258
-0
lines changed

4 files changed

+258
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 rx.lang.jruby;
17+
18+
import org.jruby.RubyProc;
19+
import rx.util.functions.Action;
20+
import rx.util.functions.Action0;
21+
import rx.util.functions.Action1;
22+
import rx.util.functions.Action2;
23+
import rx.util.functions.Action3;
24+
25+
/**
26+
* Concrete wrapper that accepts a {@link RubyProc} and produces any needed Rx {@link Action}.
27+
*
28+
* @param <T1>
29+
* @param <T2>
30+
* @param <T3>
31+
* @param <T4>
32+
*/
33+
public class JRubyActionWrapper<T1, T2, T3, T4> implements Action, Action0, Action1<T1>, Action2<T1, T2>, Action3<T1, T2, T3> {
34+
35+
private final RubyProc<Void> proc;
36+
37+
public GroovyActionWrapper(RubyProc<Void> proc) {
38+
this.proc = proc;
39+
}
40+
41+
@Override
42+
public void call() {
43+
proc.call();
44+
}
45+
46+
@Override
47+
public void call(T1 t1) {
48+
proc.call(t1);
49+
}
50+
51+
@Override
52+
public void call(T1 t1, T2 t2) {
53+
proc.call(t1, t2);
54+
}
55+
56+
@Override
57+
public void call(T1 t1, T2 t2, T3 t3) {
58+
proc.call(t1, t2, t3);
59+
}
60+
61+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 rx.lang.jruby;
17+
18+
import org.jruby.RubyProc;
19+
import rx.util.functions.Func0;
20+
import rx.util.functions.Func1;
21+
import rx.util.functions.Func2;
22+
import rx.util.functions.Func3;
23+
import rx.util.functions.Func4;
24+
import rx.util.functions.Func5;
25+
import rx.util.functions.Func6;
26+
import rx.util.functions.Func7;
27+
import rx.util.functions.Func8;
28+
import rx.util.functions.Func9;
29+
import rx.util.functions.FuncN;
30+
import rx.util.functions.Function;
31+
32+
/**
33+
* Concrete wrapper that accepts a {@link RubyProc} and produces any needed Rx {@link Function}.
34+
*
35+
* @param <T1>
36+
* @param <T2>
37+
* @param <T3>
38+
* @param <T4>
39+
* @param <R>
40+
*/
41+
public class JRubyFunctionWrapper<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> implements
42+
Func0<R>,
43+
Func1<T1, R>,
44+
Func2<T1, T2, R>,
45+
Func3<T1, T2, T3, R>,
46+
Func4<T1, T2, T3, T4, R>,
47+
Func5<T1, T2, T3, T4, T5, R>,
48+
Func6<T1, T2, T3, T4, T5, T6, R>,
49+
Func7<T1, T2, T3, T4, T5, T6, T7, R>,
50+
Func8<T1, T2, T3, T4, T5, T6, T7, T8, R>,
51+
Func9<T1, T2, T3, T4, T5, T6, T7, T8, T9, R>,
52+
FuncN<R> {
53+
54+
private final RubyProc<R> proc;
55+
56+
public GroovyFunctionWrapper(RubyProc<R> proc) {
57+
this.proc = proc;
58+
}
59+
60+
@Override
61+
public R call() {
62+
return (R) proc.call();
63+
}
64+
65+
@Override
66+
public R call(T1 t1) {
67+
return (R) proc.call(t1);
68+
}
69+
70+
@Override
71+
public R call(T1 t1, T2 t2) {
72+
return (R) proc.call(t1, t2);
73+
}
74+
75+
@Override
76+
public R call(T1 t1, T2 t2, T3 t3) {
77+
return (R) proc.call(t1, t2, t3);
78+
}
79+
80+
@Override
81+
public R call(T1 t1, T2 t2, T3 t3, T4 t4) {
82+
return (R) proc.call(t1, t2, t3, t4);
83+
}
84+
85+
@Override
86+
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) {
87+
return (R) proc.call(t1, t2, t3, t4, t5);
88+
}
89+
90+
@Override
91+
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) {
92+
return (R) proc.call(t1, t2, t3, t4, t5, t6);
93+
}
94+
95+
@Override
96+
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) {
97+
return (R) proc.call(t1, t2, t3, t4, t5, t6, t7);
98+
}
99+
100+
@Override
101+
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) {
102+
return (R) proc.call(t1, t2, t3, t4, t5, t6, t7, t8);
103+
}
104+
105+
@Override
106+
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) {
107+
return (R) proc.call(t1, t2, t3, t4, t5, t6, t7, t8, t9);
108+
}
109+
110+
@Override
111+
public R call(Object... args) {
112+
return (R) proc.call(args);
113+
}
114+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 rx.lang.jruby;
17+
18+
import org.jruby.RubyProc;
19+
import rx.Observable.OnSubscribeFunc;
20+
import rx.Observer;
21+
import rx.Subscription;
22+
23+
/**
24+
* Concrete wrapper that accepts a {@link RubyProc} and produces a {@link OnSubscribeFunc}.
25+
*
26+
* @param <T>
27+
*/
28+
public class JRubyOnSubscribeFuncWrapper<T> implements OnSubscribeFunc<T> {
29+
30+
private final RubyProc<Subscription> proc;
31+
32+
public GroovyOnSubscribeFuncWrapper(RubyProc<Subscription> proc) {
33+
this.proc = proc;
34+
}
35+
36+
@Override
37+
public Subscription onSubscribe(Observer<? super T> observer) {
38+
return proc.call(observer);
39+
}
40+
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
klasses = [Java::Rx::Observable, Java::RxObservables::BlockingObservable]
2+
function = Java::RxUtilFunctions::Function.java_class
3+
4+
WRAPPERS = {
5+
Java::RxUtilFunctions::Action.java_class => Java::RxLangJruby::JRubyActionWrapper,
6+
Java::RxObservable::OnSubscribeFunc.java_class => Java::RxLangJruby::JRubyOnSubscribeFuncWrapper
7+
}
8+
9+
WRAPPERS.default = Java::RxLangJruby::JRubyFunctionWrapper
10+
11+
klasses.each do |klass|
12+
function_methods = klass.java_class.declared_instance_methods.select do |method|
13+
method.public? && method.parameter_types.any? {|type| function.assignable_from?(type)}
14+
end
15+
16+
parameter_types = function_methods.group_by(&:name).each_with_object({}) do |(method_name, methods), memo|
17+
types = methods.map(&:parameter_types).select {|type| function.assignable_from?(type)}.flatten.uniq
18+
raise ArgumentError, "More than one function type for #{method_name}" if types.length > 1
19+
20+
memo[method_name] = WRAPPERS[types.first]
21+
end
22+
23+
function_methods.map(&:name).uniq.each do |method_name|
24+
klass.class_eval <<EOS
25+
def #{method_name}(*args, &block)
26+
args.map! do |arg|
27+
if arg.is_a?(Proc)
28+
#{parameter_types[method_name]}.new(arg)
29+
else
30+
arg
31+
end
32+
end
33+
34+
if block_given?
35+
block = #{parameter_types[method_name]}.new(block)
36+
end
37+
38+
super(*args, &block)
39+
end
40+
EOS
41+
end
42+
end

0 commit comments

Comments
 (0)