rx.Observable.firstOrDefault()方法的使用及代码示例

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

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

Observable.firstOrDefault介绍

[英]Returns an Observable that emits only the very first item emitted by the source Observable, or a default item if the source Observable completes without emitting anything.

Scheduler: firstOrDefault does not operate by default on a particular Scheduler.
[中]返回仅发射源可观测项发射的第一项的可观测项,或者如果源可观测项完成而不发射任何内容,则返回默认项。
调度程序:默认情况下,firstOrDefault不会在特定调度程序上运行。

代码示例

代码示例来源:origin: Netflix/EVCache

  1. .doOnSuccess(fbData -> increment(fbClient.getServerGroupName(), _cacheName, "RETRY_" + ((fbData == null) ? "MISS" : "HIT")))
  2. .toObservable()))
  3. .firstOrDefault(null, fbData -> (fbData != null)).toSingle();

代码示例来源:origin: Netflix/EVCache

  1. .doOnSuccess(fbData -> increment(fbClient.getServerGroupName(), _cacheName, "RETRY_" + ((fbData == null) ? "MISS" : "HIT")))
  2. .toObservable()))
  3. .firstOrDefault(null, fbData -> (fbData != null)).toSingle();

代码示例来源:origin: leeowenowen/rxjava-examples

  1. @Override
  2. public void run() {
  3. Observable.range(1, 10).firstOrDefault(3).subscribe(new Action1<Integer>() {
  4. @Override
  5. public void call(Integer integer) {
  6. log(integer);
  7. }
  8. });
  9. }
  10. });

代码示例来源:origin: org.zalando.paradox/paradox-nakadi-consumer-core

  1. @Override
  2. public Single<String> getContent(final EventTypeCursor cursor) {
  3. final Observable<HttpResponseChunk> request = getContent0(cursor, 1);
  4. return request.map(HttpResponseChunk::getContent).firstOrDefault(null).toSingle();
  5. }

代码示例来源:origin: com.microsoft.azure/azure-mgmt-compute

  1. /**
  2. * Retrieves encryption extension installed in the virtual machine, if the extension is
  3. * not installed then return an empty observable.
  4. *
  5. * @return an observable that emits the encryption extension installed in the virtual machine
  6. */
  7. private Observable<VirtualMachineExtension> getEncryptionExtensionInstalledInVMAsync() {
  8. return virtualMachine.listExtensionsAsync()
  9. // firstOrDefault() is used intentionally here instead of first() to ensure
  10. // this method return empty observable if matching extension is not found.
  11. //
  12. .firstOrDefault(null, new Func1<VirtualMachineExtension, Boolean>() {
  13. @Override
  14. public Boolean call(final VirtualMachineExtension extension) {
  15. return extension.publisherName().equalsIgnoreCase(encryptionExtensionPublisher)
  16. && extension.typeName().equalsIgnoreCase(encryptionExtensionType());
  17. }
  18. }).flatMap(new Func1<VirtualMachineExtension, Observable<VirtualMachineExtension>>() {
  19. @Override
  20. public Observable<VirtualMachineExtension> call(VirtualMachineExtension extension) {
  21. if (extension == null) {
  22. return Observable.empty();
  23. }
  24. return Observable.just(extension);
  25. }
  26. });
  27. }

代码示例来源:origin: com.netflix.rxjava/rxjava-core

  1. /**
  2. * Returns the first item emitted by this {@code BlockingObservable} that matches a predicate, or a default
  3. * value if it emits no such items.
  4. *
  5. * @param defaultValue
  6. * a default value to return if this {@code BlockingObservable} emits no matching items
  7. * @param predicate
  8. * a predicate function to evaluate items emitted by this {@code BlockingObservable}
  9. * @return the first item emitted by this {@code BlockingObservable} that matches the predicate, or the
  10. * default value if this {@code BlockingObservable} emits no matching items
  11. * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Blocking-Observable-Operators#first-and-firstordefault">RxJava Wiki: firstOrDefault()</a>
  12. * @see <a href="http://msdn.microsoft.com/en-us/library/hh229759.aspx">MSDN: Observable.FirstOrDefault</a>
  13. */
  14. public T firstOrDefault(T defaultValue, Func1<? super T, Boolean> predicate) {
  15. return blockForSingle(o.filter(predicate).map(Functions.<T>identity()).firstOrDefault(defaultValue));
  16. }

代码示例来源:origin: com.netflix.rxjava/rxjava-core

  1. /**
  2. * Returns the first item emitted by this {@code BlockingObservable}, or a default value if it emits no
  3. * items.
  4. *
  5. * @param defaultValue
  6. * a default value to return if this {@code BlockingObservable} emits no items
  7. * @return the first item emitted by this {@code BlockingObservable}, or the default value if it emits no
  8. * items
  9. * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Blocking-Observable-Operators#first-and-firstordefault">RxJava Wiki: firstOrDefault()</a>
  10. * @see <a href="http://msdn.microsoft.com/en-us/library/hh229320.aspx">MSDN: Observable.FirstOrDefault</a>
  11. */
  12. public T firstOrDefault(T defaultValue) {
  13. return blockForSingle(o.map(Functions.<T>identity()).firstOrDefault(defaultValue));
  14. }

代码示例来源:origin: Azure/azure-libraries-for-java

  1. /**
  2. * Retrieves encryption extension installed in the virtual machine, if the extension is
  3. * not installed then return an empty observable.
  4. *
  5. * @return an observable that emits the encryption extension installed in the virtual machine
  6. */
  7. private Observable<VirtualMachineExtension> getEncryptionExtensionInstalledInVMAsync() {
  8. return virtualMachine.listExtensionsAsync()
  9. // firstOrDefault() is used intentionally here instead of first() to ensure
  10. // this method return empty observable if matching extension is not found.
  11. //
  12. .firstOrDefault(null, new Func1<VirtualMachineExtension, Boolean>() {
  13. @Override
  14. public Boolean call(final VirtualMachineExtension extension) {
  15. return extension.publisherName().equalsIgnoreCase(encryptionExtensionPublisher)
  16. && extension.typeName().equalsIgnoreCase(encryptionExtensionType());
  17. }
  18. }).flatMap(new Func1<VirtualMachineExtension, Observable<VirtualMachineExtension>>() {
  19. @Override
  20. public Observable<VirtualMachineExtension> call(VirtualMachineExtension extension) {
  21. if (extension == null) {
  22. return Observable.empty();
  23. }
  24. return Observable.just(extension);
  25. }
  26. });
  27. }

代码示例来源:origin: org.zalando.paradox/paradox-nakadi-consumer-core

  1. @Override
  2. public Single<String> getEvent(final EventTypeCursor cursor) {
  3. final Observable<HttpResponseChunk> request = getContent0(cursor, 1);
  4. return request.map(chunk -> getEvent0(chunk.getContent(), cursor.getEventType())).firstOrDefault(null)
  5. .toSingle();
  6. }

代码示例来源:origin: org.zalando.paradox/paradox-nakadi-consumer-core

  1. @Override
  2. public Single<List<NakadiPartition>> getPartitions(final EventType eventType) {
  3. final HttpGetPartitions httpGetPartitions = new HttpGetPartitions(nakadiUrl, eventType);
  4. final Observable<HttpResponseChunk> request = new RxHttpRequest(partitionsTimeoutMillis,
  5. authorizationValueProvider).createRequest(httpGetPartitions);
  6. return request.filter(chunk -> {
  7. checkArgument(chunk.getStatusCode() == 200,
  8. "Get partitions for event [%s] , result [%s / %s]", eventType, chunk.getStatusCode(),
  9. chunk.getContent());
  10. return true;
  11. }).map(chunk -> getPartitions(chunk.getContent())).firstOrDefault(Collections.emptyList())
  12. .toSingle();
  13. }

代码示例来源:origin: com.couchbase.client/java-client

  1. .getFromReplica(id, ReplicaMode.ALL, target)
  2. .timeout(replicaTimeout, TimeUnit.MILLISECONDS)
  3. .firstOrDefault(null)
  4. .filter(new Func1<D, Boolean>() {
  5. @Override

代码示例来源:origin: akarnokd/akarnokd-misc

  1. .observeOn(uiScheduler)
  2. .firstOrDefault(defaultList, items -> items != null && !items.isEmpty())
  3. .subscribe(subscriber);

相关文章

Observable类方法