use of io.reactivex.functions.Function in project sqlbrite by square.
the class QueryObservableTest method mapToListThrowsFromQueryRun.
@Test
public void mapToListThrowsFromQueryRun() {
final IllegalStateException error = new IllegalStateException("test exception");
Query query = new Query() {
@Override
public Cursor run() {
throw error;
}
};
//
new QueryObservable(Observable.just(query)).mapToList(new Function<Cursor, Object>() {
@Override
public Object apply(Cursor cursor) {
throw new AssertionError("Must not be called");
}
}).test().assertNoValues().assertError(error);
}
use of io.reactivex.functions.Function in project Luban by Curzibn.
the class MainActivity method withRx.
private <T> void withRx(final List<T> photos) {
mDisposable.add(Flowable.just(photos).observeOn(Schedulers.io()).map(new Function<List<T>, List<File>>() {
@Override
public List<File> apply(@NonNull List<T> list) throws Exception {
return Luban.with(MainActivity.this).setTargetDir(getPath()).load(list).get();
}
}).observeOn(AndroidSchedulers.mainThread()).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
Log.e(TAG, throwable.getMessage());
}
}).onErrorResumeNext(Flowable.<List<File>>empty()).subscribe(new Consumer<List<File>>() {
@Override
public void accept(@NonNull List<File> list) {
for (File file : list) {
Log.i(TAG, file.getAbsolutePath());
showResult(originPhotos, file);
}
}
}));
}
use of io.reactivex.functions.Function in project RxCache by VictorAlbertos.
the class ProcessorProvidersBehaviour method getDataFromLoader.
private Observable<Reply> getDataFromLoader(final io.rx_cache2.ConfigProvider configProvider, final Record record) {
return configProvider.getLoaderObservable().map(new Function<Object, Reply>() {
@Override
public Reply apply(Object data) throws Exception {
boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
if (data == null && useExpiredData && record != null) {
return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
}
clearKeyIfNeeded(configProvider);
if (data == null) {
throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey());
}
twoLayersCache.save(configProvider.getProviderKey(), configProvider.getDynamicKey(), configProvider.getDynamicKeyGroup(), data, configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
return new Reply(data, Source.CLOUD, configProvider.isEncrypted());
}
}).onErrorReturn(new Function<Object, Object>() {
@Override
public Object apply(Object o) throws Exception {
clearKeyIfNeeded(configProvider);
boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
if (useExpiredData && record != null) {
return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
}
throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey(), (Throwable) o);
}
});
}
use of io.reactivex.functions.Function in project RxRelay by JakeWharton.
the class PublishRelayTest method testNestedSubscribe.
@Test
public void testNestedSubscribe() {
final PublishRelay<Integer> s = PublishRelay.create();
final AtomicInteger countParent = new AtomicInteger();
final AtomicInteger countChildren = new AtomicInteger();
final AtomicInteger countTotal = new AtomicInteger();
final ArrayList<String> list = new ArrayList<String>();
s.flatMap(new Function<Integer, Observable<String>>() {
@Override
public Observable<String> apply(final Integer v) {
countParent.incrementAndGet();
// then subscribe to subject again (it will not receive the previous value)
return s.map(new Function<Integer, String>() {
@Override
public String apply(Integer v2) {
countChildren.incrementAndGet();
return "Parent: " + v + " Child: " + v2;
}
});
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
countTotal.incrementAndGet();
list.add(v);
}
});
for (int i = 0; i < 10; i++) {
s.accept(i);
}
// System.out.println("countParent: " + countParent.get());
// System.out.println("countChildren: " + countChildren.get());
// System.out.println("countTotal: " + countTotal.get());
// 9+8+7+6+5+4+3+2+1+0 == 45
assertEquals(45, list.size());
}
use of io.reactivex.functions.Function in project RxRelay by JakeWharton.
the class PublishRelayTest method testUnsubscriptionCase.
@Test(timeout = 1000)
public void testUnsubscriptionCase() {
PublishRelay<String> src = PublishRelay.create();
for (int i = 0; i < 10; i++) {
final Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
String v = "" + i;
System.out.printf("Turn: %d%n", i);
src.firstElement().toObservable().flatMap(new Function<String, Observable<String>>() {
@Override
public Observable<String> apply(String t1) {
return Observable.just(t1 + ", " + t1);
}
}).subscribe(new DefaultObserver<String>() {
@Override
public void onNext(String t) {
o.onNext(t);
}
@Override
public void onError(Throwable e) {
o.onError(e);
}
@Override
public void onComplete() {
o.onComplete();
}
});
src.accept(v);
inOrder.verify(o).onNext(v + ", " + v);
inOrder.verify(o).onComplete();
verify(o, never()).onError(any(Throwable.class));
}
}
Aggregations