Skip to content

Commit

Permalink
First pass at jruby compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Ragalie committed Sep 23, 2013
1 parent fa72a27 commit fc0a706
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.lang.jruby;

import org.jruby.RubyProc;
import rx.util.functions.Action;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import rx.util.functions.Action2;
import rx.util.functions.Action3;

/**
* Concrete wrapper that accepts a {@link RubyProc} and produces any needed Rx {@link Action}.
*
* @param <T1>
* @param <T2>
* @param <T3>
* @param <T4>
*/
public class JRubyActionWrapper<T1, T2, T3, T4> implements Action, Action0, Action1<T1>, Action2<T1, T2>, Action3<T1, T2, T3> {

private final RubyProc<Void> proc;

public GroovyActionWrapper(RubyProc<Void> proc) {

This comment has been minimized.

Copy link
@iconara

iconara Sep 23, 2013

This should probably be JRubyActionWrapper

this.proc = proc;
}

@Override
public void call() {
proc.call();
}

@Override
public void call(T1 t1) {
proc.call(t1);
}

@Override
public void call(T1 t1, T2 t2) {
proc.call(t1, t2);
}

@Override
public void call(T1 t1, T2 t2, T3 t3) {
proc.call(t1, t2, t3);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.lang.jruby;

import org.jruby.RubyProc;
import rx.util.functions.Func0;
import rx.util.functions.Func1;
import rx.util.functions.Func2;
import rx.util.functions.Func3;
import rx.util.functions.Func4;
import rx.util.functions.Func5;
import rx.util.functions.Func6;
import rx.util.functions.Func7;
import rx.util.functions.Func8;
import rx.util.functions.Func9;
import rx.util.functions.FuncN;
import rx.util.functions.Function;

/**
* Concrete wrapper that accepts a {@link RubyProc} and produces any needed Rx {@link Function}.
*
* @param <T1>
* @param <T2>
* @param <T3>
* @param <T4>
* @param <R>
*/
public class JRubyFunctionWrapper<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> implements
Func0<R>,
Func1<T1, R>,
Func2<T1, T2, R>,
Func3<T1, T2, T3, R>,
Func4<T1, T2, T3, T4, R>,
Func5<T1, T2, T3, T4, T5, R>,
Func6<T1, T2, T3, T4, T5, T6, R>,
Func7<T1, T2, T3, T4, T5, T6, T7, R>,
Func8<T1, T2, T3, T4, T5, T6, T7, T8, R>,
Func9<T1, T2, T3, T4, T5, T6, T7, T8, T9, R>,
FuncN<R> {

private final RubyProc<R> proc;

public GroovyFunctionWrapper(RubyProc<R> proc) {

This comment has been minimized.

Copy link
@daveray

daveray Sep 23, 2013

Typo :)

this.proc = proc;
}

@Override
public R call() {
return (R) proc.call();
}

@Override
public R call(T1 t1) {
return (R) proc.call(t1);
}

@Override
public R call(T1 t1, T2 t2) {
return (R) proc.call(t1, t2);
}

@Override
public R call(T1 t1, T2 t2, T3 t3) {
return (R) proc.call(t1, t2, t3);
}

@Override
public R call(T1 t1, T2 t2, T3 t3, T4 t4) {
return (R) proc.call(t1, t2, t3, t4);
}

@Override
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) {
return (R) proc.call(t1, t2, t3, t4, t5);
}

@Override
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) {
return (R) proc.call(t1, t2, t3, t4, t5, t6);
}

@Override
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) {
return (R) proc.call(t1, t2, t3, t4, t5, t6, t7);
}

@Override
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) {
return (R) proc.call(t1, t2, t3, t4, t5, t6, t7, t8);
}

@Override
public R call(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) {
return (R) proc.call(t1, t2, t3, t4, t5, t6, t7, t8, t9);
}

@Override
public R call(Object... args) {
return (R) proc.call(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.lang.jruby;

import org.jruby.RubyProc;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;

/**
* Concrete wrapper that accepts a {@link RubyProc} and produces a {@link OnSubscribeFunc}.
*
* @param <T>
*/
public class JRubyOnSubscribeFuncWrapper<T> implements OnSubscribeFunc<T> {

private final RubyProc<Subscription> proc;

public GroovyOnSubscribeFuncWrapper(RubyProc<Subscription> proc) {

This comment has been minimized.

Copy link
@iconara

iconara Sep 24, 2013

Here's another one.

this.proc = proc;
}

@Override
public Subscription onSubscribe(Observer<? super T> observer) {
return proc.call(observer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
klasses = [Java::Rx::Observable, Java::RxObservables::BlockingObservable]
function = Java::RxUtilFunctions::Function.java_class

WRAPPERS = {
Java::RxUtilFunctions::Action.java_class => Java::RxLangJruby::JRubyActionWrapper,
Java::RxObservable::OnSubscribeFunc.java_class => Java::RxLangJruby::JRubyOnSubscribeFuncWrapper
}

WRAPPERS.default = Java::RxLangJruby::JRubyFunctionWrapper

klasses.each do |klass|
function_methods = klass.java_class.declared_instance_methods.select do |method|
method.public? && method.parameter_types.any? {|type| function.assignable_from?(type)}
end

parameter_types = function_methods.group_by(&:name).each_with_object({}) do |(method_name, methods), memo|
types = methods.map(&:parameter_types).select {|type| function.assignable_from?(type)}.flatten.uniq
raise ArgumentError, "More than one function type for #{method_name}" if types.length > 1

memo[method_name] = WRAPPERS[types.first]
end

function_methods.map(&:name).uniq.each do |method_name|
klass.class_eval <<EOS
def #{method_name}(*args, &block)
args.map! do |arg|
if arg.is_a?(Proc)
#{parameter_types[method_name]}.new(arg)
else
arg
end
end
if block_given?
block = #{parameter_types[method_name]}.new(block)
end
super(*args, &block)
end
EOS
end
end

0 comments on commit fc0a706

Please sign in to comment.