本文整理了Java中rx.Observable.firstOrDefault()
方法的一些代码示例,展示了Observable.firstOrDefault()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.firstOrDefault()
方法的具体详情如下:
包路径:rx.Observable
类名称: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
.doOnSuccess(fbData -> increment(fbClient.getServerGroupName(), _cacheName, "RETRY_" + ((fbData == null) ? "MISS" : "HIT")))
.toObservable()))
.firstOrDefault(null, fbData -> (fbData != null)).toSingle();
代码示例来源:origin: Netflix/EVCache
.doOnSuccess(fbData -> increment(fbClient.getServerGroupName(), _cacheName, "RETRY_" + ((fbData == null) ? "MISS" : "HIT")))
.toObservable()))
.firstOrDefault(null, fbData -> (fbData != null)).toSingle();
代码示例来源:origin: leeowenowen/rxjava-examples
@Override
public void run() {
Observable.range(1, 10).firstOrDefault(3).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
log(integer);
}
});
}
});
代码示例来源:origin: org.zalando.paradox/paradox-nakadi-consumer-core
@Override
public Single<String> getContent(final EventTypeCursor cursor) {
final Observable<HttpResponseChunk> request = getContent0(cursor, 1);
return request.map(HttpResponseChunk::getContent).firstOrDefault(null).toSingle();
}
代码示例来源:origin: com.microsoft.azure/azure-mgmt-compute
/**
* Retrieves encryption extension installed in the virtual machine, if the extension is
* not installed then return an empty observable.
*
* @return an observable that emits the encryption extension installed in the virtual machine
*/
private Observable<VirtualMachineExtension> getEncryptionExtensionInstalledInVMAsync() {
return virtualMachine.listExtensionsAsync()
// firstOrDefault() is used intentionally here instead of first() to ensure
// this method return empty observable if matching extension is not found.
//
.firstOrDefault(null, new Func1<VirtualMachineExtension, Boolean>() {
@Override
public Boolean call(final VirtualMachineExtension extension) {
return extension.publisherName().equalsIgnoreCase(encryptionExtensionPublisher)
&& extension.typeName().equalsIgnoreCase(encryptionExtensionType());
}
}).flatMap(new Func1<VirtualMachineExtension, Observable<VirtualMachineExtension>>() {
@Override
public Observable<VirtualMachineExtension> call(VirtualMachineExtension extension) {
if (extension == null) {
return Observable.empty();
}
return Observable.just(extension);
}
});
}
代码示例来源:origin: com.netflix.rxjava/rxjava-core
/**
* Returns the first item emitted by this {@code BlockingObservable} that matches a predicate, or a default
* value if it emits no such items.
*
* @param defaultValue
* a default value to return if this {@code BlockingObservable} emits no matching items
* @param predicate
* a predicate function to evaluate items emitted by this {@code BlockingObservable}
* @return the first item emitted by this {@code BlockingObservable} that matches the predicate, or the
* default value if this {@code BlockingObservable} emits no matching items
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Blocking-Observable-Operators#first-and-firstordefault">RxJava Wiki: firstOrDefault()</a>
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229759.aspx">MSDN: Observable.FirstOrDefault</a>
*/
public T firstOrDefault(T defaultValue, Func1<? super T, Boolean> predicate) {
return blockForSingle(o.filter(predicate).map(Functions.<T>identity()).firstOrDefault(defaultValue));
}
代码示例来源:origin: com.netflix.rxjava/rxjava-core
/**
* Returns the first item emitted by this {@code BlockingObservable}, or a default value if it emits no
* items.
*
* @param defaultValue
* a default value to return if this {@code BlockingObservable} emits no items
* @return the first item emitted by this {@code BlockingObservable}, or the default value if it emits no
* items
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Blocking-Observable-Operators#first-and-firstordefault">RxJava Wiki: firstOrDefault()</a>
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229320.aspx">MSDN: Observable.FirstOrDefault</a>
*/
public T firstOrDefault(T defaultValue) {
return blockForSingle(o.map(Functions.<T>identity()).firstOrDefault(defaultValue));
}
代码示例来源:origin: Azure/azure-libraries-for-java
/**
* Retrieves encryption extension installed in the virtual machine, if the extension is
* not installed then return an empty observable.
*
* @return an observable that emits the encryption extension installed in the virtual machine
*/
private Observable<VirtualMachineExtension> getEncryptionExtensionInstalledInVMAsync() {
return virtualMachine.listExtensionsAsync()
// firstOrDefault() is used intentionally here instead of first() to ensure
// this method return empty observable if matching extension is not found.
//
.firstOrDefault(null, new Func1<VirtualMachineExtension, Boolean>() {
@Override
public Boolean call(final VirtualMachineExtension extension) {
return extension.publisherName().equalsIgnoreCase(encryptionExtensionPublisher)
&& extension.typeName().equalsIgnoreCase(encryptionExtensionType());
}
}).flatMap(new Func1<VirtualMachineExtension, Observable<VirtualMachineExtension>>() {
@Override
public Observable<VirtualMachineExtension> call(VirtualMachineExtension extension) {
if (extension == null) {
return Observable.empty();
}
return Observable.just(extension);
}
});
}
代码示例来源:origin: org.zalando.paradox/paradox-nakadi-consumer-core
@Override
public Single<String> getEvent(final EventTypeCursor cursor) {
final Observable<HttpResponseChunk> request = getContent0(cursor, 1);
return request.map(chunk -> getEvent0(chunk.getContent(), cursor.getEventType())).firstOrDefault(null)
.toSingle();
}
代码示例来源:origin: org.zalando.paradox/paradox-nakadi-consumer-core
@Override
public Single<List<NakadiPartition>> getPartitions(final EventType eventType) {
final HttpGetPartitions httpGetPartitions = new HttpGetPartitions(nakadiUrl, eventType);
final Observable<HttpResponseChunk> request = new RxHttpRequest(partitionsTimeoutMillis,
authorizationValueProvider).createRequest(httpGetPartitions);
return request.filter(chunk -> {
checkArgument(chunk.getStatusCode() == 200,
"Get partitions for event [%s] , result [%s / %s]", eventType, chunk.getStatusCode(),
chunk.getContent());
return true;
}).map(chunk -> getPartitions(chunk.getContent())).firstOrDefault(Collections.emptyList())
.toSingle();
}
代码示例来源:origin: com.couchbase.client/java-client
.getFromReplica(id, ReplicaMode.ALL, target)
.timeout(replicaTimeout, TimeUnit.MILLISECONDS)
.firstOrDefault(null)
.filter(new Func1<D, Boolean>() {
@Override
代码示例来源:origin: akarnokd/akarnokd-misc
.observeOn(uiScheduler)
.firstOrDefault(defaultList, items -> items != null && !items.isEmpty())
.subscribe(subscriber);
内容来源于网络,如有侵权,请联系作者删除!