Java Code Examples for io.reactivex.Observable.map()

The following are Jave code examples for showing how to use map() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: RxPay   File: RxPayUtils.java   Source Code and License Vote up 7 votes
public static ObservableTransformer<WxPayResult, WxPayResult> checkWechatResult() {
    return new ObservableTransformer<WxPayResult, WxPayResult>() {
        @Override
        public ObservableSource<WxPayResult> apply(Observable<WxPayResult> payResultObservable) {
            return payResultObservable.map(new Function<WxPayResult, WxPayResult>() {
                @Override
                public WxPayResult apply(WxPayResult wxPayResult) {
                    if (!wxPayResult.isSucceed()) {
                        throw new PayFailedException(wxPayResult.getErrInfo());
                    }
                    return wxPayResult;
                }
            });
        }
    };
}
 
Example 2
Project: rxjavatraining   File: ObservableTestingTest.java   Source Code and License Vote up 7 votes
public Observable<Tuple2<String, Double>> processStock(List<String> stocks, Function<String, String> f) {
    Observable<String> stockNames = Observable.fromIterable(stocks);
    Observable<String> urls = stockNames.map(s -> "https://finance.google.com/finance/historical?output=csv&q=" + s);
    Observable<Optional<String>> optionalObservable = urls
            .subscribeOn(Schedulers.from(executorService))
            .flatMap(s -> {
                try {
                    return Observable.just(Optional.of(f.apply(s)));
                } catch (Throwable t) {
                    return Observable.just(Optional.<String>empty());
                }
            })

            .observeOn(Schedulers.computation())
            .map(opt -> opt.map(doc -> doc.split("\n")[1].split(",")[4]))
            .doOnNext(x -> System.out.println(">>>>" + x));
    return Observable.zip(stockNames, optionalObservable, (name, price) ->
            new Tuple2<>(name, Double.parseDouble(price.orElse("0.0"))));
}
 
Example 3
Project: Learning-RxJava   File: Ch2_3.java   Source Code and License Vote up 6 votes
public static void main(String[] args) {
    Observable<String> source = Observable.create(emitter -> {
        try {
            emitter.onNext("Alpha");
            emitter.onNext("Beta");
            emitter.onNext("Gamma");
            emitter.onNext("Delta");
            emitter.onNext("Epsilon");
            emitter.onComplete();
        } catch (Throwable e) {
            emitter.onError(e);
        }
    });
    Observable<Integer> lengths = source.map(String::length);
    Observable<Integer> filtered = lengths.filter(i -> i >= 5);
    filtered.subscribe(s -> System.out.println("RECEIVED: " +
            s));
}
 
Example 4
Project: RxPay   File: RxPayUtils.java   Source Code and License Vote up 6 votes
public static ObservableTransformer<PayResult, PayResult> checkAliPayResult() {
    return new ObservableTransformer<PayResult, PayResult>() {
        @Override
        public ObservableSource<PayResult> apply(Observable<PayResult> upstream) {
            return upstream.map(new Function<PayResult, PayResult>() {
                @Override
                public PayResult apply(PayResult payResult) throws Exception {
                    if (!payResult.isSucceed()) {
                        throw new PayFailedException(payResult.getErrInfo());
                    }
                    return payResult;
                }
            });
        }
    };
}
 
Example 5
Project: rxjavatraining   File: StockFinderTest.java   Source Code and License Vote up 6 votes
@Test
    public void testStockPriceSchedulerWithErrorHandling() throws IOException, InterruptedException {
        Observable<String> stockNames = Observable.just("M", "MSFT", "BTC", "T", "ORCL");
        Observable<String> urls = stockNames.map(s -> "https://finance.google.com/finance/historical?output=csv&q=" + s);
        Observable<Optional<String>> optionalObservable = urls
                .subscribeOn(Schedulers.from(executorService))
                .flatMap(s -> {
                    try {
                        return Observable.just(Optional.of(this.getInfoFromURL(s)));
                    } catch (Throwable t) {
                        return Observable.just(Optional.<String>empty());
                    }
                })
                .observeOn(Schedulers.computation())
                .map(opt -> opt.map(doc -> doc.split("\n")[1].split(",")[4]));
//                .doOnNext(x -> System.out.println(">>>" + x));


        Observable.zip(stockNames, optionalObservable, (name, price) ->
                new Tuple2<>(name, Double.parseDouble(price.orElse("0.0"))))
                .subscribe(System.out::println, Throwable::printStackTrace);
        Thread.sleep(10000);
    }
 
Example 6
Project: Dalaran   File: RequestStrategy.java   Source Code and License Vote up 5 votes
/**
 * build remote data flow(origin,action,map...)
 *
 * @return remote flow
 */
private Observable<V> getRemoteFlow() {
    if (remoteObservableOrigin == null)
        return null;
    Observable result = remoteObservableOrigin;
    if (cacheAction != null) {
        result = remoteObservableOrigin.doOnNext(cacheAction);
    }
    if (netDataMapFunc != null) {
        result = result.map(netDataMapFunc);
    }
    return result;
}
 
Example 7
Project: XSnow   File: CacheStrategy.java   Source Code and License Vote up 5 votes
<T> Observable<CacheResult<T>> loadRemote(final ApiCache apiCache, final String key, Observable<T> source) {
    return source.map(new Function<T, CacheResult<T>>() {
        @Override
        public CacheResult<T> apply(T t) throws Exception {
            ViseLog.i("loadRemote result=" + t);
            apiCache.put(key, t).subscribeOn(Schedulers.io()).subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean status) throws Exception {
                    ViseLog.i("save status => " + status);
                }
            });
            return new CacheResult<>(false, t);
        }
    });
}
 
Example 8
Project: RxNetWork   File: ApplyImpl.java   Source Code and License Vote up 5 votes
@Override
public <T> Observable<CacheResult<T>> apply(@NonNull final Object key, Observable<T> observable, final LruDisk lruDisk, final boolean isInsert) {
    return observable.map(new Function<T, CacheResult<T>>() {
        @Override
        public CacheResult<T> apply(@NonNull T t) throws Exception {
            CacheResult<T> tCacheResult = new CacheResult<>(t, key, CacheResult.CacheType.NETWORK);
            if (isInsert) {
                lruDisk.insert(key, tCacheResult.getResult());
            }
            return tCacheResult;
        }
    });
}
 
Example 9
Project: RxEasyHttp   File: NoStrategy.java   Source Code and License Vote up 5 votes
@Override
public <T> Observable<CacheResult<T>> execute(RxCache rxCache, String cacheKey, long cacheTime, Observable<T> source, Type type) {
    return source.map(new Function<T, CacheResult<T>>() {
        @Override
        public CacheResult<T> apply(@NonNull T t) throws Exception {
            return new CacheResult<T>(false, t);
        }
    });
}
 
Example 10
Project: rxjavatraining   File: StockFinderTest.java   Source Code and License Vote up 5 votes
@Test
public void testStockPriceFinderFindTheLatestCloseBug() throws IOException {
    Observable<String> stockNames = Observable.just("M", "MSFT", "BTC", "T", "ORCL");
    Observable<String> urls = stockNames.map(s -> "https://finance.google.com/finance/historical?output=csv&q=" + s);
    Observable<String> contentObservable = urls.flatMap(u -> Observable.fromFuture(createFuture(u)).onErrorResumeNext((Throwable e) -> Observable.empty()));
    Observable<String> lineObservable = contentObservable.doOnNext(s -> System.out.println(">>>>" + s)).flatMap(doc ->
            Observable.fromArray(doc.split("\n")).skip(1).take(1));
    Observable<Tuple2<String, Double>> tupleObservable = stockNames.zipWith(lineObservable.map(s -> s.split(",")[4]).map(Double::parseDouble), Tuple2::new);
    tupleObservable.subscribe(System.out::println);
}
 
Example 11
Project: rxjavatraining   File: StockFinderTest.java   Source Code and License Vote up 5 votes
@Test
public void testStockPriceScheduler() throws IOException, InterruptedException {
    Observable<String> stockNames = Observable.just("M", "MSFT", "T", "ORCL");
    Observable<String> urls = stockNames.map(s -> "https://finance.google.com/finance/historical?output=csv&q=" + s);
    urls.doOnNext(x -> System.out.println("doOnNext-1: " + Thread.currentThread().getName()))
        .subscribeOn(Schedulers.from(executorService))
        .map(this::getInfoFromURL)
        .observeOn(Schedulers.computation())
        .doOnNext(x -> System.out.println("doOnNext-2: " + Thread.currentThread().getName()))
        .flatMap(doc -> Observable.fromArray(doc.split("\n")).skip(1).take(1)).subscribe(System.out::println);
    Thread.sleep(10000);
}
 
Example 12
Project: vt-support   File: StorageImpl.java   Source Code and License Vote up 5 votes
@Override
public Observable<StorageResult> put(Observable<Entry> entries) {
  return entries.map(entry -> {
    try {
      FilesystemUtil.addEntry(directory, entry, gzipEnabled);
      return new StorageResult(entry);
    } catch (final IOException ex) {
      return new StorageResult(entry, new IOException("cannot put entry", ex));
    }
  });
}
 
Example 13
Project: vt-support   File: StorageImpl.java   Source Code and License Vote up 5 votes
@Override
public Observable<StorageResult> delete(Observable<Entry> entries) {
  return entries.map(entry -> {
    try {
      FilesystemUtil.removeEntry(directory, entry);
      return new StorageResult(entry);
    } catch (final IOException ex) {
      return new StorageResult(entry, new IOException("cannot delete entry", ex));
    }
  });
}
 
Example 14
Project: RxFirebase2   File: ObsTransformerOfGenericTypeIndicator.java   Source Code and License Vote up 5 votes
@Override public ObservableSource<Optional<T>> apply(Observable<DataSnapshot> upstream) {
  return upstream.map(new Function<DataSnapshot, Optional<T>>() {
    @Override public Optional<T> apply(DataSnapshot dataSnapshot) throws Exception {
      return Optional.of(dataSnapshot.getValue(typeIndicator));
    }
  });
}
 
Example 15
Project: RxFirebase2   File: ObsTransformerOfClazz.java   Source Code and License Vote up 5 votes
@Override public ObservableSource<Optional<T>> apply(Observable<DataSnapshot> upstream) {
  return upstream.map(new Function<DataSnapshot, Optional<T>>() {
    @Override public Optional<T> apply(DataSnapshot dataSnapshot) throws Exception {
      return Optional.of(dataSnapshot.getValue(clazz));
    }
  });
}
 
Example 16
Project: SuperHttp   File: CacheStrategy.java   Source Code and License Vote up 5 votes
<T> Observable<CacheResult<T>> loadRemote(final ApiCache apiCache, final String key, Observable<T> source) {
    return source.map(new Function<T, CacheResult<T>>() {
        @Override
        public CacheResult<T> apply(T t) throws Exception {

            apiCache.put(key, t).subscribeOn(Schedulers.io()).subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean status) throws Exception {

                }
            });
            return new CacheResult<>(false, t);
        }
    });
}
 
Example 17
Project: buckaroo   File: Process.java   Source Code and License Vote up 4 votes
public static <S, T, A extends S, B extends T> Process<S, T> of(final Observable<Either<A, B>> observable) {
    return new Process<>(observable.map(x -> x.flatMap(Either::left, Either::right)));
}
 
Example 18
Project: gigreminder   File: BasePresenterTest.java   Source Code and License Vote up 4 votes
@Override
protected Observable<Result> handleEvents(Observable<UiEvent> events) {
    return events.map(event -> new TestResult(((TestUiEvent) event).data));
}