Skip to content

Instantly share code, notes, and snippets.

@justintuchek
Last active December 20, 2017 14:02
Show Gist options
  • Save justintuchek/0f4f88d7823790010e535219e3031bfe to your computer and use it in GitHub Desktop.
Save justintuchek/0f4f88d7823790010e535219e3031bfe to your computer and use it in GitHub Desktop.
public class RxInteropFunction<T, R> implements Func1<T, R>, Function<T, R> {
public static <T, R> RxInteropFunction<T, R> wrap(Function<T, R> function) {
return new RxInteropFunction<>(function);
}
private final Function<T, R> function;
private RxInteropFunction(Function<T, R> function) {
this.function = function;
}
@Override
public R apply(T t) throws Exception {
return function.apply(t);
}
@Override
public R call(T t) {
try {
return function.apply(t);
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
}
public class RxInteropPredicate<T> implements Predicate<T>, Func1<T, Boolean> {
public static <T> RxInteropPredicate<T> wrap(Predicate<T> predicate) {
return new RxInteropPredicate<>(predicate);
}
private final Predicate<T> predicate;
public RxInteropPredicate(Predicate<T> predicate) {
this.predicate = predicate;
}
@Override
public boolean test(T t) throws Exception {
return predicate.test(t);
}
@Override
public Boolean call(T t) {
try {
return predicate.test(t);
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment