io.reactivex.Observable.defaultIfEmpty()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(4.6k)|赞(0)|评价(0)|浏览(137)

本文整理了Java中io.reactivex.Observable.defaultIfEmpty()方法的一些代码示例,展示了Observable.defaultIfEmpty()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.defaultIfEmpty()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:defaultIfEmpty

Observable.defaultIfEmpty介绍

[英]Returns an Observable that emits the items emitted by the source ObservableSource or a specified default item if the source ObservableSource is empty.

Scheduler: defaultIfEmpty does not operate by default on a particular Scheduler.
[中]返回发出源ObservableSource发出的项的Observable,如果源ObservableSource为空,则返回指定的默认项。
调度程序:默认情况下,defaultIfEmpty不会在特定调度程序上运行。

代码示例

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = NullPointerException.class)
public void defaultIfEmptyNull() {
  just1.defaultIfEmpty(null);
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public ObservableSource<T> apply(final T v) throws Exception {
    ObservableSource<U> o = ObjectHelper.requireNonNull(itemDelay.apply(v), "The itemDelay returned a null ObservableSource");
    return new ObservableTake<U>(o, 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
  }
}

代码示例来源:origin: pwittchen/ReactiveNetwork

@Override public Observable<Connectivity> observeNetworkConnectivity(final Context context) {
 final IntentFilter filter = new IntentFilter();
 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
 return Observable.create(new ObservableOnSubscribe<Connectivity>() {
  @Override public void subscribe(final ObservableEmitter<Connectivity> emitter)
    throws Exception {
   final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override public void onReceive(Context context, Intent intent) {
     emitter.onNext(Connectivity.create(context));
    }
   };
   context.registerReceiver(receiver, filter);
   Disposable disposable = disposeInUiThread(new Action() {
    @Override public void run() {
     tryToUnregisterReceiver(context, receiver);
    }
   });
   emitter.setDisposable(disposable);
  }
 }).defaultIfEmpty(Connectivity.create());
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testDefaultIfEmpty() {
  Observable<Integer> source = Observable.just(1, 2, 3);
  Observable<Integer> observable = source.defaultIfEmpty(10);
  Observer<Integer> observer = TestHelper.mockObserver();
  observable.subscribe(observer);
  verify(observer, never()).onNext(10);
  verify(observer).onNext(1);
  verify(observer).onNext(2);
  verify(observer).onNext(3);
  verify(observer).onComplete();
  verify(observer, never()).onError(any(Throwable.class));
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testDefaultIfEmptyWithEmpty() {
  Observable<Integer> source = Observable.empty();
  Observable<Integer> observable = source.defaultIfEmpty(10);
  Observer<Integer> observer = TestHelper.mockObserver();
  observable.subscribe(observer);
  verify(observer).onNext(10);
  verify(observer).onComplete();
  verify(observer, never()).onError(any(Throwable.class));
}

代码示例来源:origin: redisson/redisson

@Override
  public ObservableSource<T> apply(final T v) throws Exception {
    ObservableSource<U> o = ObjectHelper.requireNonNull(itemDelay.apply(v), "The itemDelay returned a null ObservableSource");
    return new ObservableTake<U>(o, 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
  @Ignore("Subscribers should not throw")
  public void testEmptyButClientThrows() {
    final Observer<Integer> o = TestHelper.mockObserver();

    Observable.<Integer>empty().defaultIfEmpty(1).subscribe(new DefaultObserver<Integer>() {
      @Override
      public void onNext(Integer t) {
        throw new TestException();
      }

      @Override
      public void onError(Throwable e) {
        o.onError(e);
      }

      @Override
      public void onComplete() {
        o.onComplete();
      }
    });

    verify(o).onError(any(TestException.class));
    verify(o, never()).onNext(any(Integer.class));
    verify(o, never()).onComplete();
  }
}

代码示例来源:origin: uk.os.vt/vt

@Override
 public ObservableSource<List<Entry>> apply(Entry entry) throws Exception {
  return storage.getEntry(entry.getZoomLevel(), entry.getColumn(), entry.getRow())
    .map(new Function<Entry, List<Entry>>() {
     @Override
     public List<Entry> apply(Entry newEntry) throws Exception {
      return Arrays.asList(entry, newEntry);
     }
    }).defaultIfEmpty(Arrays.asList(null, entry));
 }
};

代码示例来源:origin: uk.os.vt/vt

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
private void updateMetadata() {
 storage.getMetadata().defaultIfEmpty(new Metadata.Builder(schema)
   .setAttribution(System.getProperty("user.name")).build())
   .subscribe(new Consumer<Metadata>() {
    @Override
    public void accept(Metadata metadata) throws Exception {
     Metadata.Builder newMetadata = new Metadata.Builder(metadata);
     for (FluentLayer layer : layers) {
      if (!isLayerInMetadata(metadata, layer.getName())) {
       newMetadata.addLayer(layer.getMetadata());
      }
     }
     storage.putMetadata(Single.just(newMetadata.build()));
    }
   });
}

相关文章

Observable类方法