All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dc.riot.lol.rx.service.ObservableFactory Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
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 s) -> { try { T t = callable.call(); s.onNext(t); s.onCompleted(); } catch (Exception e) { s.onError(e); } }); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy