本文整理了Java中io.reactivex.Observable.count()
方法的一些代码示例,展示了Observable.count()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.count()
方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:count
[英]Returns a Single that counts the total number of items emitted by the source ObservableSource and emits this count as a 64-bit Long.
Scheduler: count does not operate by default on a particular Scheduler.
[中]返回对源ObservableSource发出的项目总数进行计数的单个值,并以64位长的形式发出此计数。
计划程序:默认情况下,计数不会在特定计划程序上运行。
代码示例来源:origin: ReactiveX/RxJava
@Override
public SingleSource<Long> apply(Observable<Object> o) throws Exception {
return o.count();
}
});
代码示例来源:origin: ReactiveX/RxJava
@Override
public ObservableSource<Long> apply(Observable<Object> o) throws Exception {
return o.count().toObservable();
}
});
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCountError() {
Observable<String> o = Observable.error(new Callable<Throwable>() {
@Override
public Throwable call() {
return new RuntimeException();
}
});
o.count().subscribe(wo);
verify(wo, never()).onSuccess(anyInt());
verify(wo, times(1)).onError(any(RuntimeException.class));
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void dispose() {
TestHelper.checkDisposed(Observable.just(1).count());
TestHelper.checkDisposed(Observable.just(1).count().toObservable());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCountAFewItems() {
Observable<String> o = Observable.just("a", "b", "c", "d");
o.count().subscribe(wo);
// we should be called only once
verify(wo, times(1)).onSuccess(anyLong());
verify(wo).onSuccess(4L);
verify(wo, never()).onError(any(Throwable.class));
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCountAFewItemsObservable() {
Observable<String> o = Observable.just("a", "b", "c", "d");
o.count().toObservable().subscribe(w);
// we should be called only once
verify(w, times(1)).onNext(anyLong());
verify(w).onNext(4L);
verify(w, never()).onError(any(Throwable.class));
verify(w, times(1)).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCountErrorObservable() {
Observable<String> o = Observable.error(new Callable<Throwable>() {
@Override
public Throwable call() {
return new RuntimeException();
}
});
o.count().toObservable().subscribe(w);
verify(w, never()).onNext(anyInt());
verify(w, never()).onComplete();
verify(w, times(1)).onError(any(RuntimeException.class));
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCountZeroItems() {
Observable<String> o = Observable.empty();
o.count().subscribe(wo);
// we should be called only once
verify(wo, times(1)).onSuccess(anyLong());
verify(wo).onSuccess(0L);
verify(wo, never()).onError(any(Throwable.class));
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void justUnsubscribed() throws Exception {
o = new Object();
WeakReference<Object> wr = new WeakReference<Object>(o);
TestObserver<Long> to = Observable.just(o).count().toObservable().onTerminateDetach().test();
o = null;
to.cancel();
System.gc();
Thread.sleep(200);
Assert.assertNull("Object retained!", wr.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCountZeroItemsObservable() {
Observable<String> o = Observable.empty();
o.count().toObservable().subscribe(w);
// we should be called only once
verify(w, times(1)).onNext(anyLong());
verify(w).onNext(0L);
verify(w, never()).onError(any(Throwable.class));
verify(w, times(1)).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void just() throws Exception {
o = new Object();
WeakReference<Object> wr = new WeakReference<Object>(o);
TestObserver<Object> to = new TestObserver<Object>();
Observable.just(o).count().toObservable().onTerminateDetach().subscribe(to);
to.assertValue(1L);
to.assertComplete();
to.assertNoErrors();
o = null;
System.gc();
Thread.sleep(200);
Assert.assertNull("Object retained!", wr.get());
}
代码示例来源:origin: Polidea/RxAndroidBle
/**
* Observable that emits `true` if the permission was granted on the time of subscription
* @param locationServicesStatus the LocationServicesStatus
* @param timerScheduler the Scheduler
* @return the observable
*/
@NonNull
private static Single<Boolean> checkPermissionUntilGranted(
final LocationServicesStatus locationServicesStatus,
Scheduler timerScheduler
) {
return Observable.interval(0, 1L, TimeUnit.SECONDS, timerScheduler)
.takeWhile(new Predicate<Long>() {
@Override
public boolean test(Long timer) {
return !locationServicesStatus.isLocationPermissionOk();
}
})
.count()
.map(new Function<Long, Boolean>() {
@Override
public Boolean apply(Long count) throws Exception {
// if no elements were emitted then the permission was granted from the beginning
return count == 0;
}
});
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testTakeLastZeroProcessesAllItemsButIgnoresThem() {
final AtomicInteger upstreamCount = new AtomicInteger();
final int num = 10;
long count = Observable.range(1, num).doOnNext(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
upstreamCount.incrementAndGet();
}})
.takeLast(0).count().blockingGet();
assertEquals(num, upstreamCount.get());
assertEquals(0L, count);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void fromArityArgs1() {
Observable<String> items = Observable.just("one");
assertEquals((Long)1L, items.count().blockingGet());
assertEquals("one", items.takeLast(1).blockingSingle());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testUpstreamIsProcessedButIgnoredObservable() {
final int num = 10;
final AtomicInteger upstreamCount = new AtomicInteger();
long count = Observable.range(1, num)
.doOnNext(new Consumer<Integer>() {
@Override
public void accept(Integer t) {
upstreamCount.incrementAndGet();
}
})
.ignoreElements()
.toObservable()
.count().blockingGet();
assertEquals(num, upstreamCount.get());
assertEquals(0, count);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void fromIterable() {
ArrayList<String> items = new ArrayList<String>();
items.add("one");
items.add("two");
items.add("three");
assertEquals((Long)3L, Observable.fromIterable(items).count().blockingGet());
assertEquals("two", Observable.fromIterable(items).skip(1).take(1).blockingSingle());
assertEquals("three", Observable.fromIterable(items).takeLast(1).blockingSingle());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testIssue1522() {
// https://github.com/ReactiveX/RxJava/issues/1522
assertNull(Observable
.empty()
.count()
.filter(new Predicate<Long>() {
@Override
public boolean test(Long v) {
return false;
}
})
.blockingGet());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void fromArityArgs3() {
Observable<String> items = Observable.just("one", "two", "three");
assertEquals((Long)3L, items.count().blockingGet());
assertEquals("two", items.skip(1).take(1).blockingSingle());
assertEquals("three", items.takeLast(1).blockingSingle());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void fromArray() {
String[] items = new String[] { "one", "two", "three" };
assertEquals((Long)3L, Observable.fromArray(items).count().blockingGet());
assertEquals("two", Observable.fromArray(items).skip(1).take(1).blockingSingle());
assertEquals("three", Observable.fromArray(items).takeLast(1).blockingSingle());
}
代码示例来源:origin: silentbalanceyh/vertx-zero
private boolean isPlugin(final Class<?> clazz) {
final Field[] fields = clazz.getDeclaredFields();
final Long counter = Observable.fromArray(fields)
.filter(field -> field.isAnnotationPresent(Plugin.class))
.count().blockingGet();
return 0 < counter;
}
}
内容来源于网络,如有侵权,请联系作者删除!