本文整理了Java中io.reactivex.Observable.switchIfEmpty()
方法的一些代码示例,展示了Observable.switchIfEmpty()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.switchIfEmpty()
方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:switchIfEmpty
[英]Returns an Observable that emits the items emitted by the source ObservableSource or the items of an alternate ObservableSource if the source ObservableSource is empty.
Scheduler: switchIfEmpty does not operate by default on a particular Scheduler.
[中]返回一个ObservableSource,该ObservableSource发出源ObservableSource发出的项,如果源ObservableSource为空,则返回备用ObservableSource发出的项。
调度器:switchIfEmpty默认情况下不会在特定的调度器上运行。
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void switchIfEmptyNull() {
just1.switchIfEmpty(null);
}
代码示例来源:origin: ReactiveX/RxJava
/**
* Returns an Observable that emits the items emitted by the source ObservableSource or a specified default item
* if the source ObservableSource is empty.
* <p>
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/defaultIfEmpty.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code defaultIfEmpty} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param defaultItem
* the item to emit if the source ObservableSource emits no items
* @return an Observable that emits either the specified default item if the source ObservableSource emits no
* items, or the items emitted by the source ObservableSource
* @see <a href="http://reactivex.io/documentation/operators/defaultifempty.html">ReactiveX operators documentation: DefaultIfEmpty</a>
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<T> defaultIfEmpty(T defaultItem) {
ObjectHelper.requireNonNull(defaultItem, "defaultItem is null");
return switchIfEmpty(just(defaultItem));
}
代码示例来源:origin: redisson/redisson
/**
* Returns an Observable that emits the items emitted by the source ObservableSource or a specified default item
* if the source ObservableSource is empty.
* <p>
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/defaultIfEmpty.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code defaultIfEmpty} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param defaultItem
* the item to emit if the source ObservableSource emits no items
* @return an Observable that emits either the specified default item if the source ObservableSource emits no
* items, or the items emitted by the source ObservableSource
* @see <a href="http://reactivex.io/documentation/operators/defaultifempty.html">ReactiveX operators documentation: DefaultIfEmpty</a>
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<T> defaultIfEmpty(T defaultItem) {
ObjectHelper.requireNonNull(defaultItem, "defaultItem is null");
return switchIfEmpty(just(defaultItem));
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testSwitchWhenNotEmpty() throws Exception {
final AtomicBoolean subscribed = new AtomicBoolean(false);
final Observable<Integer> o = Observable.just(4)
.switchIfEmpty(Observable.just(2)
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable d) {
subscribed.set(true);
}
}));
assertEquals(4, o.blockingSingle().intValue());
assertFalse(subscribed.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testSwitchWhenEmpty() throws Exception {
final Observable<Integer> o = Observable.<Integer>empty()
.switchIfEmpty(Observable.fromIterable(Arrays.asList(42)));
assertEquals(42, o.blockingSingle().intValue());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testSwitchShouldTriggerUnsubscribe() {
final Disposable d = Disposables.empty();
Observable.unsafeCreate(new ObservableSource<Long>() {
@Override
public void subscribe(final Observer<? super Long> observer) {
observer.onSubscribe(d);
observer.onComplete();
}
}).switchIfEmpty(Observable.<Long>never()).subscribe();
assertTrue(d.isDisposed());
}
}
代码示例来源:origin: ReactiveX/RxJava
.switchIfEmpty(withProducer)
.lift(new ObservableOperator<Long, Long>() {
@Override
代码示例来源:origin: GrossumUA/TAS_Android_Boilerplate
/**
* Replaces observable of objects with observable of optionals.
* Result observable always contains optionals, Optional.empty() if source observable has no items.
*/
public static <T> Observable<Optional<T>> toOptObs(Observable<T> source) {
return source.map(Optional::ofNullable)
.switchIfEmpty(just(Optional.empty()));
}
代码示例来源:origin: fengzhizi715/RxCache
@Override
public <T> Observable<Record<T>> execute(RxCache rxCache, String key, Observable<T> source, Type type) {
Observable<Record<T>> cache = rxCache.<T>load2Observable(key, type);
Observable<Record<T>> remote = source
.map(new Function<T, Record<T>>() {
@Override
public Record<T> apply(@NonNull T t) throws Exception {
rxCache.save(key, t);
return new Record<>(Source.CLOUD, key, t);
}
});
return cache.switchIfEmpty(remote);
}
}
代码示例来源:origin: fengzhizi715/RxCache
@Override
public <T> Observable<Record<T>> execute(RxCache rxCache, String key, Observable<T> source, Type type) {
Observable<Record<T>> cache = rxCache.<T>load2Observable(key, type);
Observable<Record<T>> remote = source
.map(new Function<T, Record<T>>() {
@Override
public Record<T> apply(@NonNull T t) throws Exception {
rxCache.save(key, t);
return new Record<>(Source.CLOUD, key, t);
}
});
return remote.switchIfEmpty(cache);
}
}
代码示例来源:origin: WallaceXiao/StockChart-MPAndroidChart
@Override
public <T> Observable<CacheResult<T>> execute(RxCache rxCache, String key, long time, Observable<T> source, Type type) {
Observable<CacheResult<T>> cache = loadCache(rxCache, type, key, time, true);
Observable<CacheResult<T>> remote = loadRemote(rxCache, key, source, false);
return cache.switchIfEmpty(remote);
}
}
代码示例来源:origin: TetraTutorials/clean-android
@Override
public Observable<Result> getResultData() {
return getResultsFromMemory().switchIfEmpty(getResultsFromNetwork());
}
}
代码示例来源:origin: TetraTutorials/clean-android
@Override
public Observable<String> getCountryData() {
return getCountriesFromMemory().switchIfEmpty(getCountriesFromNetwork());
}
代码示例来源:origin: fengzhizi715/RxCache
@Override
public <T> Observable<Record<T>> execute(RxCache rxCache, String key, Observable<T> source, Type type) {
Observable<Record<T>> cache = rxCache.<T>load2Observable(key, type)
.filter(new Predicate<Record<T>>() {
@Override
public boolean test(Record<T> record) throws Exception {
return System.currentTimeMillis() - record.getCreateTime() <= timestamp;
}
});
Observable<Record<T>> remote = source
.map(new Function<T, Record<T>>() {
@Override
public Record<T> apply(@NonNull T t) throws Exception {
rxCache.save(key, t);
return new Record<>(Source.CLOUD, key, t);
}
});
return cache.switchIfEmpty(remote);
}
}
代码示例来源:origin: io.gravitee.am.gateway.handlers/gravitee-am-gateway-handler
public Single<Token> grant(TokenRequest tokenRequest, Client client) {
return Observable
.fromIterable(tokenGranters.values())
.filter(tokenGranter -> tokenGranter.handle(tokenRequest.getGrantType()))
.switchIfEmpty(Observable.error(new UnsupportedGrantTypeException("Unsupported grant type: " + tokenRequest.getGrantType())))
.flatMapSingle(tokenGranter -> tokenGranter.grant(tokenRequest, client)).singleOrError();
}
代码示例来源:origin: gravitee-io/graviteeio-access-management
public Single<Token> grant(TokenRequest tokenRequest, Client client) {
return Observable
.fromIterable(tokenGranters.values())
.filter(tokenGranter -> tokenGranter.handle(tokenRequest.getGrantType()))
.switchIfEmpty(Observable.error(new UnsupportedGrantTypeException("Unsupported grant type: " + tokenRequest.getGrantType())))
.flatMapSingle(tokenGranter -> tokenGranter.grant(tokenRequest, client)).singleOrError();
}
代码示例来源:origin: gravitee-io/graviteeio-access-management
@Override
public Single<AuthorizationResponse> run(AuthorizationRequest authorizationRequest, Client client, User endUser) {
return Observable
.fromIterable(flows)
.filter(flow -> flow.handle(authorizationRequest.getResponseType()))
.switchIfEmpty(Observable.error(new UnsupportedResponseTypeException("Unsupported response type: " + authorizationRequest.getResponseType())))
.flatMapSingle(flow -> flow.run(authorizationRequest, client, endUser)).singleOrError();
}
代码示例来源:origin: io.gravitee.am.gateway.handlers/gravitee-am-gateway-handler
@Override
public Single<AuthorizationResponse> run(AuthorizationRequest authorizationRequest, Client client, User endUser) {
return Observable
.fromIterable(flows)
.filter(flow -> flow.handle(authorizationRequest.getResponseType()))
.switchIfEmpty(Observable.error(new UnsupportedResponseTypeException("Unsupported response type: " + authorizationRequest.getResponseType())))
.flatMapSingle(flow -> flow.run(authorizationRequest, client, endUser)).singleOrError();
}
代码示例来源:origin: com.firstdata.clovergo/domain
}).switchIfEmpty(Observable.<EmployeeMerchant>error(new Error("no_employee_to_set_passcode", "PassCode set failed please re-activate")));
内容来源于网络,如有侵权,请联系作者删除!