org.dc.riot.lol.rx.service.ObservableFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lol-api-rxjava Show documentation
Show all versions of lol-api-rxjava Show documentation
Service library for League of Legends API
package org.dc.riot.lol.rx.service;
import java.util.concurrent.Callable;
import rx.Observable;
import rx.Subscriber;
/**
* Factory helper to create functional {@link Observable} instances.
* Typical use case for this class is to help in creating the first
* {@link Observable} in a chain.
*
* {@code
RiotApi.Champion champInterface = factory.newChampionInterface(region, true);
ObservableFactory.create(() -> {
return champInterface.getChampions(true);
})
.subscribe((ChampionListDto dto) -> {
... Process champions ...
},
(Throwable e) -> {
e.printStackTrace();
},
() -> {
System.out.println("Done");
});}
*
*
* @author Dc
* @since 1.0.0
*
* @param generic type
*/
public class ObservableFactory {
private ObservableFactory() { }
/**
*
* @param callable {@link Callable} that should make a single API call
* and return the result. We use {@link Callable} instead of {@link java.util.function.Supplier
* Supplier} for the exception handling.
* @param generic type
* @return an {@link Observable} that will emit the object returned by callable
*/
public static Observable create(final Callable callable) {
return Observable.create((Subscriber super T> s) -> {
try {
T t = callable.call();
s.onNext(t);
s.onCompleted();
} catch (Exception e) {
s.onError(e);
}
});
}
}