Search in sources :

Example 96 with Function

use of io.reactivex.functions.Function in project apollo-android by apollographql.

the class Rx2ApolloTest method queryWatcherNotUpdatedSameQuerySameResults.

@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherNotUpdatedSameQuerySameResults() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestObserver<EpisodeHeroNameQuery.Data> observer = new TestObserver<>();
    Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {

        @Override
        public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            return response.data();
        }
    }).subscribeWith(observer);
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
    observer.assertValueCount(1).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {

        @Override
        public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
            assertThat(data.hero().name()).isEqualTo("R2-D2");
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) Function(io.reactivex.functions.Function) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 97 with Function

use of io.reactivex.functions.Function in project apollo-android by apollographql.

the class Rx2ApolloTest method queryWatcherUpdatedDifferentQueryDifferentResults.

@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherUpdatedDifferentQueryDifferentResults() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestObserver<EpisodeHeroNameQuery.Data> observer = new TestObserver<>();
    Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {

        @Override
        public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            return response.data();
        }
    }).subscribeWith(observer);
    server.enqueue(mockResponse("HeroAndFriendsNameWithIdsNameChange.json"));
    apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(NEWHOPE))).enqueue(null);
    observer.assertValueCount(2).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {

        @Override
        public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
            assertThat(data.hero().name()).isEqualTo("R2-D2");
            return true;
        }
    }).assertValueAt(1, new Predicate<EpisodeHeroNameQuery.Data>() {

        @Override
        public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
            assertThat(data.hero().name()).isEqualTo("Artoo");
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) Function(io.reactivex.functions.Function) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) TestObserver(io.reactivex.observers.TestObserver) Predicate(io.reactivex.functions.Predicate) Test(org.junit.Test)

Example 98 with Function

use of io.reactivex.functions.Function in project RxJavaInAction by fengzhizi715.

the class TestHttpClientWithMaybe method main.

public static void main(String[] args) {
    Maybe.create(new MaybeOnSubscribe<String>() {

        @Override
        public void subscribe(@NonNull MaybeEmitter<String> e) throws Exception {
            String url = "http://www.163.com";
            e.onSuccess(url);
        }
    }).map(new Function<String, CloseableHttpResponse>() {

        @Override
        public CloseableHttpResponse apply(@NonNull String url) throws Exception {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet get = new HttpGet(url);
            return client.execute(get);
        }
    }).subscribe(new Consumer<CloseableHttpResponse>() {

        @Override
        public void accept(CloseableHttpResponse response) throws Exception {
            // 服务器返回码
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode = " + statusCode);
            HttpEntity entity = response.getEntity();
            // 服务器返回内容
            String respStr = null;
            if (entity != null) {
                respStr = EntityUtils.toString(entity, "UTF-8");
            }
            System.out.println(respStr);
            // 释放资源
            EntityUtils.consume(entity);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) NonNull(io.reactivex.annotations.NonNull) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 99 with Function

use of io.reactivex.functions.Function in project RxJavaInAction by fengzhizi715.

the class FlatMapForParallel2 method main.

public static void main(String[] args) {
    int threadNum = Runtime.getRuntime().availableProcessors() + 1;
    ExecutorService executor = Executors.newFixedThreadPool(threadNum);
    final Scheduler scheduler = Schedulers.from(executor);
    Observable.range(1, 100).flatMap(new Function<Integer, ObservableSource<String>>() {

        @Override
        public ObservableSource<String> apply(Integer integer) throws Exception {
            return Observable.just(integer).subscribeOn(scheduler).map(new Function<Integer, String>() {

                @Override
                public String apply(Integer integer) throws Exception {
                    return integer.toString();
                }
            });
        }
    }).subscribe(new Consumer<String>() {

        @Override
        public void accept(String str) throws Exception {
            System.out.println(str);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) Scheduler(io.reactivex.Scheduler) ExecutorService(java.util.concurrent.ExecutorService)

Example 100 with Function

use of io.reactivex.functions.Function in project RxJavaInAction by fengzhizi715.

the class RoundRobinForParallel1 method main.

public static void main(String[] args) {
    final AtomicInteger batch = new AtomicInteger(0);
    Observable.range(1, 100).groupBy(new Function<Integer, Integer>() {

        @Override
        public Integer apply(@NonNull Integer integer) throws Exception {
            return batch.getAndIncrement() % 5;
        }
    }).flatMap(new Function<GroupedObservable<Integer, Integer>, ObservableSource<?>>() {

        @Override
        public ObservableSource<?> apply(@NonNull GroupedObservable<Integer, Integer> integerIntegerGroupedObservable) throws Exception {
            return integerIntegerGroupedObservable.observeOn(Schedulers.io()).map(new Function<Integer, String>() {

                @Override
                public String apply(@NonNull Integer integer) throws Exception {
                    return integer.toString();
                }
            });
        }
    }).subscribe(new Consumer<Object>() {

        @Override
        public void accept(@NonNull Object o) throws Exception {
            System.out.println(o);
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(io.reactivex.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NonNull(io.reactivex.annotations.NonNull) GroupedObservable(io.reactivex.observables.GroupedObservable)

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