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;
}
});
}
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;
}
});
}
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);
}
});
}
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);
}
});
}
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);
}
});
}
Aggregations