Skip to content
This repository was archived by the owner on Mar 2, 2022. It is now read-only.

Commit 0d17429

Browse files
committed
Added Mono.single()
1 parent 71df374 commit 0d17429

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/main/scala/reactor/core/scala/publisher/Mono.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,19 @@ class Mono[T] private(private val jMono: JMono[T]) extends Publisher[T] with Map
13711371
*/
13721372
final def retryWhen(whenFactory: Flux[Throwable] => Publisher[_]): Mono[T] = Mono[T](jMono.retryWhen(whenFactory))
13731373

1374+
/**
1375+
* Expect exactly one item from this [[Mono]] source or signal
1376+
* [[java.util.NoSuchElementException]] for an empty source.
1377+
* <p>
1378+
* <img class="marble" src="https://raw.githubusercontent.com/reactor/reactor-core/v3.1.1.RELEASE/src/docs/marble/single.png" alt="">
1379+
* <p>
1380+
* Note Mono doesn't need [[Flux.single(AnyRef)]], since it is equivalent to
1381+
* [[Mono.defaultIfEmpty(AnyRef)]] in a [[Mono]].
1382+
*
1383+
* @return a [[Mono]] with the single item or an error signal
1384+
*/
1385+
final def single() = Mono(jMono.single())
1386+
13741387
/**
13751388
* Subscribe to this [[Mono]] and request unbounded demand.
13761389
* <p>

src/test/scala/reactor/core/scala/publisher/MonoTest.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,22 @@ class MonoTest extends FreeSpec with Matchers with TableDrivenPropertyChecks wit
10271027
.verifyComplete()
10281028
}
10291029

1030+
".single" - {
1031+
"should enforce the existence of element" in {
1032+
val mono = Mono.just(randomValue).single()
1033+
StepVerifier.create(mono)
1034+
.expectNext(randomValue)
1035+
.verifyComplete()
1036+
}
1037+
"should throw exception if it is empty" in {
1038+
val mono = Mono.empty.single()
1039+
StepVerifier.create(mono)
1040+
.expectError(classOf[NoSuchElementException])
1041+
.verify()
1042+
}
1043+
1044+
}
1045+
10301046
".subscribe" - {
10311047
"without parameter should return Disposable" in {
10321048
val x = Mono.just(randomValue).subscribe()

0 commit comments

Comments
 (0)