kotlin - 如果其中之一为空,则发布者串联
问题描述
我正在尝试连接两个发布者,但我知道它this
可以为空,我希望结果也为空。
fun Flux<String>.prefixWith(rhs: Mono<String>) = rhs
.flux()
.concatWith(this)
这只是rhs
按预期返回。Flux
如果为空,如何返回this
空?
解决方案
您可以使用hasElements()
方法 ( doc )Flux
来确定通量是否具有元素,然后使用flatMapMany
, 如果存在元素则返回串联,如果不存在元素则返回空通量本身。
fun Flux<String>.prefixWith(rhs: Mono<String>) = this.hasElements().flatMapMany<String> {
if (it) {
rhs.concatWith(this) //when the flux has elements
} else {
this //this would be empty flux
}
}
推荐阅读
- javascript - SyntaxError:JSON.parse 中的意外标记 t
- php - 管理员设置在 Woocommerce WordPress 中不起作用?
- javascript - 如何在 JavaScript 中计算没有 for 循环的对象数组中的值差异?
- assembly - 装配中的除法和舍入
- android - Android OnTouchEvent 在第一次触摸时不会触发
- javascript - 用于选择的自定义 html5 验证
- python-3.x - 为什么我的 kivy 程序没有从另一个类调用函数?
- java - JPA 和 Hibernates 绑定参数的 Java.Util.Date 查询问题
- python - python将字符串拆分成新行到文件中
- php - 媒体未在 Wordpress 库中显示,但在上传文件夹中可用