Java Code Examples for io.reactivex.Observable#zip()

The following examples show how to use io.reactivex.Observable#zip() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: rxjava2-lab   File: Code05.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Observable<String> strings = Observable.just("a", "b", "c", "d");
    Observable<Long> ticks = Observable.interval(500, TimeUnit.MILLISECONDS);
    Observable<String> stream = Observable.zip(ticks, strings, (t, s) -> s);

    TestObserver<String> testObserver = stream.test();
    if (testObserver.awaitTerminalEvent(3, TimeUnit.SECONDS)) {
        testObserver
                .assertNoErrors()
                .assertComplete()
                .assertValues("a", "b", "c", "d");
        System.out.println("Cool!");
    } else {
        System.out.println("It did not finish on time");
    }
}
 
Example 2
Source Project: RxRetroJsoup   File: MainActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

    recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
    adapter = new Adapter();
    recyclerView.setAdapter(adapter);

    loadWithRetroJsoup();

    Observable.zip(
            Observable.just(""),
            Observable.just("&"),
            new BiFunction<String, String, String>(){

                @Override
                public String apply(@NonNull String s, @NonNull String s2) throws Exception {
                    return null;
                }
            }
    );
}
 
Example 3
@Test
void zip_test() {
    final WeatherStation station = new BasicWeatherStation();

    Observable<Temperature> temperature = station.temperature();
    Observable<Wind> wind = station.wind();

    Observable.zip(temperature, wind, Weather::new);
    //temperature.zipWith(wind, Weather::new);
}
 
Example 4
@Test
void zip_test2() {
    final WeatherStation station = new BasicWeatherStation();

    Observable<Temperature> temperature = station.temperature();
    Observable<Wind> wind = station.wind();

    Observable<Weather> weatherObservable = Observable.zip(temperature, wind, Weather::new);
    //temperature.zipWith(wind, Weather::new);

    Observable<LocalDate> nextWeekDays = Observable.range(1, 7)
                                                   .map(i -> LocalDate.now().plusDays(i));
    Observable.fromArray(City.values())
              .flatMap(city -> nextWeekDays.map(date -> new Vacation(city, date)))
              .flatMap(vacation ->
                      Observable.zip(
                              weatherObservable.filter(Weather::computeSunny),
                              vacation.cheapFlightFrom(vacation.getWhere()),
                              vacation.cheapHotel(),
                              (w, f, h) -> {
                                  w.setSunny(true);
                                  return vacation.setWeather(w).setFlight(f).setHotel(h);
                              }
                      ))
              .subscribe(item -> System.out.println("we got: " + item.getWhere() + " 航班折扣:" + item.getFlight()
                                                                                                  .getDiscount() + "折  from the Observable"),
                      throwable -> System.out.println("异常-> " + throwable.getMessage()),
                      () -> System.out.println("Emission completed"));
}
 
Example 5
private Observable<Boolean> generateBuyIntent(Activity activity, String skuId,
    int defaultRequestCode) {
  SKU sku = skuManager.getSku(skuId);
  BigDecimal amount = skuManager.getSkuAmount(skuId);
  BigDecimal total = amount.multiply(BigDecimal.TEN.pow(DECIMALS));

  Observable<String> getTokenContractAddress = addressProxy.getAppCoinsAddress(networkId)
      .toObservable()
      .subscribeOn(Schedulers.io());
  Observable<String> getIabContractAddress = addressProxy.getIabAddress(networkId)
      .toObservable()
      .subscribeOn(Schedulers.io());

  return Observable.zip(getTokenContractAddress, getIabContractAddress,
      (tokenContractAddress, iabContractAddress) -> {
        Intent intent = buildPaymentIntent(sku, total, tokenContractAddress,
            iabContractAddress);

        currentPayment = new PaymentDetails(PaymentStatus.FAIL, skuId,
            new Transaction(null, null, developerAddress, total.toString(), Status.PENDING));

        if (payments.containsKey(skuId)) {
          throw new IllegalArgumentException(
              "Pending buy action with the same sku found! Did you forget to consume the former?");
        } else {
          payments.put(skuId, currentPayment);

          activity.startActivityForResult(intent, defaultRequestCode);
        }
        return true;
      });
}
 
Example 6
Source Project: RxBus   File: RxBus.java    License: Apache License 2.0 5 votes vote down vote up
public <CLASS> Observable<CLASS> get(Class<CLASS> theClass) {
    return Observable.zip(
            onEvent(theClass),
            postAsObservable(new AskedEvent(theClass)),
            new BiFunction<CLASS, Object, CLASS>() {
                @Override
                public CLASS apply(@NonNull CLASS neededObject, @NonNull Object _useless) throws Exception {
                    return neededObject;
                }
            });
}
 
Example 7
/**
 * Get a list with all products from backend
 */
public Observable<List<Product>> getAllProducts() {
  return Observable.zip(getProducts(0), getProducts(1), getProducts(2), getProducts(3),
      (products0, products1, products2, products3) -> {
        List<Product> productList = new ArrayList<Product>();
        productList.addAll(products0);
        productList.addAll(products1);
        productList.addAll(products2);
        productList.addAll(products3);
        return productList;
      });
}
 
Example 8
@Override
public Observable<Boolean> isCompletedEventExpired(String eventUid) {
    return Observable.zip(d2.eventModule().events().uid(eventUid).get().toObservable(),
            getExpiryDateFromEvent(eventUid),
            ((event, program) -> DateUtils.getInstance().isEventExpired(null, event.completedDate(), program.completeEventsExpiryDays())));
}