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

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

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

Observable.defaultIfEmpty介绍

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

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

代码示例

代码示例来源:origin: apache/usergrid

private void advance(){
   //map to our old results objects, return a default empty if required
  final Observable<Results>
    observable = buildNewResultsPage( cursor ).map( resultsPage -> createResultsInternal( resultsPage ) ).defaultIfEmpty(
    new Results() );
  if (logger.isTraceEnabled()) {
    logger.trace("Trying to load results page");
  }
  //take the first from our observable
  final Results resultsPage = observable.take(1).toBlocking().first();
  if (logger.isTraceEnabled()) {
    logger.trace("Results page loaded {}", resultsPage);
  }
  //set the results for the iterator
  this.results = resultsPage;
  //set the complete flag
  this.complete =  !resultsPage.hasCursor();
  //if not comlete, set our cursor for the next iteration
  if(!complete){
    this.cursor = Optional.of( results.getCursor());
  }else{
    this.cursor = Optional.absent();
  }
}

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

@Override
  public Void call() throws Exception {
    PooledConnection<String, String> connection =
        preferCurrentELHolderRule.holder.pollThisEventLoopConnections()
                        .defaultIfEmpty(null)
                        .toBlocking().single();
    assertThat("Connection available in the eventloop.", connection, is(connection1));
    return null;
  }
}).get(1, TimeUnit.MINUTES);

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

@Override
  public Void call() throws Exception {
    PooledConnection<String, String> connection = preferCurrentELHolderRule.holder.poll()
                                           .defaultIfEmpty(null)
                                           .toBlocking()
                                           .single();
    assertThat("Connection available in the eventloop.", connection, is(connection1));
    return null;
  }
}).get(1, TimeUnit.MINUTES);

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

@Test(timeout = 60000)
public void testPeek() throws Exception {
  PooledConnection<String, String> connection1 = preferCurrentELHolderRule.addConnection();
  PooledConnection<String, String> connection = preferCurrentELHolderRule.holder.peek()
                                         .defaultIfEmpty(null)
                                         .toBlocking().single();
  assertThat("Connection not available with peek.", connection, is(connection1));
  connection = preferCurrentELHolderRule.holder.peek().defaultIfEmpty(null)
                         .toBlocking().single();
  assertThat("Connection not available after peek.", connection, not(nullValue()));
  assertThat("Unexpected connection on peek.", connection, is(connection1));
}

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

@Test(timeout = 60000)
public void testPollRemovesItem() throws Exception {
  PooledConnection<String, String> connection1 = preferCurrentELHolderRule.addConnection();
  PooledConnection<String, String> connection = preferCurrentELHolderRule.holder.poll()
                                         .defaultIfEmpty(null)
                                         .toBlocking().single();
  assertThat("Connection not available with poll.", connection, is(connection1));
  connection = preferCurrentELHolderRule.holder.poll()
                         .defaultIfEmpty(null)
                         .toBlocking().single();
  assertThat("Connection available after poll.", connection, is(nullValue()));
}

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

@Test(timeout = 60000)
public void testRemove() throws Exception {
  PooledConnection<String, String> connection1 = preferCurrentELHolderRule.addConnection();
  PooledConnection<String, String> connection = preferCurrentELHolderRule.holder.peek()
                                         .defaultIfEmpty(null)
                                         .toBlocking().single();
  assertThat("Connection not available with peek.", connection, is(connection1));
  preferCurrentELHolderRule.holder.remove(connection1);
  connection = preferCurrentELHolderRule.holder.peek().defaultIfEmpty(null).toBlocking().single();
  assertThat("Connection not removed.", connection, is(nullValue()));
}

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

private PooledConnection<String, String> _testRelease() throws Exception {
  final Connection<String, String> connection = pooledFactoryRule.getAConnection();
  pooledFactoryRule.assertNoIdleConnection();
  /* Close will release */
  pooledFactoryRule.closeAndAwait(connection); /*Throw error or close quietly*/
  PooledConnection<String, String> connIdle =
      pooledFactoryRule.holder.peek().defaultIfEmpty(null).toBlocking().single();
  assertThat("Release did not add to idle.", connIdle, not(nullValue()));
  return connIdle;
}

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

@Test(timeout = 60000)
public void testPollOutOfEventloop() throws Exception {
  PooledConnection<String, String> connection1 = preferCurrentELHolderRule.addConnection();
  PooledConnection<String, String> connection = preferCurrentELHolderRule.holder.poll()
                                         .defaultIfEmpty(null).toBlocking()
                                         .single();
  assertThat("Unexpected connection.", connection, is(connection1));
}

代码示例来源:origin: apache/usergrid

} )
.defaultIfEmpty( 0 );

代码示例来源:origin: apache/usergrid

} ).count().defaultIfEmpty( 0 ).toBlocking().last();

代码示例来源:origin: ribot/ribot-app-android

public void loadVenues() {
  getMvpView().showVenuesProgress(true);
  mSubscriptions.add(Observable.combineLatest(
      getTodayLatestCheckInAtVenue().defaultIfEmpty(null), mDataManager.getVenues(),
      new Func2<CheckIn, List<Venue>, VenuesInfo>() {
        @Override
        public VenuesInfo call(CheckIn checkIn, List<Venue> venues) {
          return new VenuesInfo(checkIn, venues);
        }
      })
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.io())
      .subscribe(new Subscriber<VenuesInfo>() {
        @Override
        public void onCompleted() {
        }
        @Override
        public void onError(Throwable e) {
          Timber.e("Error loading venues " + e);
          getMvpView().showVenuesProgress(false);
        }
        @Override
        public void onNext(VenuesInfo venuesInfo) {
          getMvpView().showVenues(
              venuesInfo.listVenues,
              venuesInfo.getTodayLatestCheckInAtVenueId());
          getMvpView().showVenuesProgress(false);
        }
      }));
}

代码示例来源:origin: henrymorgen/android-advanced-light

private void defaultIfEmpty() {
  Observable.create(new Observable.OnSubscribe<Integer>() {
    @Override
    public void call(Subscriber<? super Integer> subscriber) {
      subscriber.onCompleted();
    }
  }).defaultIfEmpty(3).subscribe(new Action1<Integer>() {
    @Override
    public void call(Integer integer) {
      Log.d(TAG, "defaultIfEmpty:"+integer);
    }
  });
}

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

@Override
 public void run() {
  Observable.<Integer>empty().defaultIfEmpty(3).subscribe(new Action1<Integer>() {
   @Override
   public void call(Integer integer) {
    log(integer);
   }
  });
 }
});

代码示例来源:origin: Aptoide/aptoide-client-v8

public <E extends RealmObject> Observable<Long> count(RealmQuery<E> query) {
 return Observable.just(query.count())
   .flatMap(count -> Observable.just(count)
     .unsubscribeOn(RealmSchedulers.getScheduler()))
   .defaultIfEmpty(0L);
}

代码示例来源:origin: org.hawkular.metrics/hawkular-metrics-core-service

@Override
public Observable<Map<String, String>> getMetricTags(MetricId<?> id) {
  return dataAccess.getMetricTags(id)
      .take(1)
      .map(row -> row.getMap(0, String.class, String.class))
      .defaultIfEmpty(new HashMap<>());
}

代码示例来源:origin: hawkular/hawkular-metrics

@Override
public Observable<Map<String, String>> getMetricTags(MetricId<?> id) {
  return dataAccess.getMetricTags(id)
      .take(1)
      .map(row -> row.getMap(0, String.class, String.class))
      .defaultIfEmpty(new HashMap<>());
}

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

public Observable<List<SearchResult>> query(String searchTerm) {
  List<Observable<ServerResponse>> responses = queryProviders(searchTerm);
  return Transform.toSearchResults(responses).map(deduplicate()).defaultIfEmpty(new ArrayList<SearchResult>());
}

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

public Observable<List<SearchResult>> query(String searchTerm, double lat, double lon) {
  List<Observable<ServerResponse>> responses = queryProviders(searchTerm);
  responses.addAll(queryProviders(lat, lon));
  return Transform.toSearchResults(responses).map(deduplicate()).defaultIfEmpty(new ArrayList<SearchResult>());
}

代码示例来源:origin: Aptoide/aptoide-client-v8

public <E extends RealmObject> Observable<E> findFirst(RealmQuery<E> query) {
 return Observable.just(query.findFirst())
   .filter(realmObject -> realmObject != null)
   .flatMap(realmObject -> realmObject.<E>asObservable().unsubscribeOn(
     RealmSchedulers.getScheduler()))
   .flatMap(realmObject -> copyFromRealm(realmObject))
   .defaultIfEmpty(null);
}

代码示例来源:origin: Aptoide/aptoide-client-v8

public <E extends RealmObject> Observable<List<E>> findAsList(RealmQuery<E> query) {
 return Observable.just(query.findAll())
   .filter(realmObject -> realmObject != null)
   .flatMap(realmObject -> realmObject.<E>asObservable().unsubscribeOn(
     RealmSchedulers.getScheduler()))
   .flatMap(realmObject -> copyFromRealm(realmObject))
   .defaultIfEmpty(null);
}

相关文章

Observable类方法