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

The following examples show how to use io.reactivex.Observable#zip() . 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 check out the related API usage on the sidebar.
Example 1
Source File: Code05.java    From rxjava2-lab with 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 File: MainActivity.java    From RxRetroJsoup with 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
Source File: OperatorsTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 5 votes vote down vote up
@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
Source File: OperatorsTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 5 votes vote down vote up
@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
Source File: PaymentService.java    From asf-sdk with GNU General Public License v3.0 5 votes vote down vote up
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 File: RxBus.java    From RxBus with 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
Source File: ProductBackendApiDecorator.java    From mosby with Apache License 2.0 5 votes vote down vote up
/**
 * 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
Source File: EventCaptureRepositoryImpl.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@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())));
}