com.aol.cyclops.rx.adapter.ObservableAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-rx Show documentation
Show all versions of cyclops-rx Show documentation
Converters and Comprehenders for RxJava
package com.aol.cyclops.rx.adapter;
import cyclops.companion.rx.Observables;
import cyclops.monads.RxWitness.obsvervable;
import com.aol.cyclops2.types.extensability.AbstractFunctionalAdapter;
import cyclops.monads.AnyM;
import lombok.AllArgsConstructor;
import rx.Observable;
import java.util.function.Function;
import java.util.function.Predicate;
@AllArgsConstructor
public class ObservableAdapter extends AbstractFunctionalAdapter {
@Override
public Iterable toIterable(AnyM t) {
return ()-> observable(t).toBlocking().toIterable().iterator();
}
@Override
public AnyM ap(AnyM> fn, AnyM apply) {
Observable f = observable(apply);
Observable extends Function super T, ? extends R>> fnF = observable(fn);
Observable res = fnF.zipWith(f, (a, b) -> a.apply(b));
return Observables.anyM(res);
}
@Override
public AnyM filter(AnyM t, Predicate super T> fn) {
return Observables.anyM(observable(t).filter(e->fn.test(e)));
}
Observable observable(AnyM anyM){
ObservableReactiveSeq seq = anyM.unwrap();
return seq.observable;
}
@Override
public AnyM empty() {
return Observables.anyM(Observable.empty());
}
@Override
public AnyM flatMap(AnyM t,
Function super T, ? extends AnyM> fn) {
return Observables.anyM(observable(t).flatMap(x->observable(fn.apply(x))));
}
@Override
public AnyM unitIterable(Iterable it) {
return Observables.anyM(Observable.from(it));
}
@Override
public AnyM unit(T o) {
return Observables.anyM(Observable.just(o));
}
}