Search in sources :

Example 66 with Function

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);
}
Also used : Function(io.reactivex.functions.Function) Query(com.squareup.sqlbrite3.SqlBrite.Query) QueryObservable(com.squareup.sqlbrite3.QueryObservable) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 67 with Function

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);
            }
        }
    }));
}
Also used : Function(io.reactivex.functions.Function) Consumer(io.reactivex.functions.Consumer) NonNull(io.reactivex.annotations.NonNull) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 68 with Function

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);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) Reply(io.rx_cache2.Reply)

Example 69 with Function

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(io.reactivex.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 70 with Function

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));
    }
}
Also used : Function(io.reactivex.functions.Function) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Aggregations

Function (io.reactivex.functions.Function)135 Disposable (io.reactivex.disposables.Disposable)45 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)38 List (java.util.List)36 Test (org.junit.Test)35 TestException (io.reactivex.exceptions.TestException)24 InOrder (org.mockito.InOrder)21 ArrayList (java.util.ArrayList)18 Observable (io.reactivex.Observable)14 NonNull (io.reactivex.annotations.NonNull)14 Reply (io.rx_cache2.Reply)9 FavoriteException (com.dante.exception.FavoriteException)8 MessageException (com.dante.exception.MessageException)8 Consumer (io.reactivex.functions.Consumer)8 BaseResult (com.dante.data.model.BaseResult)6 BuildDetails (com.khmelenko.lab.varis.network.response.BuildDetails)6 DynamicKeyGroup (io.rx_cache2.DynamicKeyGroup)6 EvictDynamicKeyGroup (io.rx_cache2.EvictDynamicKeyGroup)6 ApiResponse (com.xinshang.audient.model.entities.ApiResponse)5 Flowable (io.reactivex.Flowable)5