Here is the piece of code
@Test
public void test_mono_void_mono_empty() {
Mono.just("DATA")
.flatMap(s -> Mono.just(s.concat("-")
.concat(s))
.doOnNext(System.out::println)
.then())
.switchIfEmpty(Mono.just("EMPTY")
.doOnNext(System.out::println)
.then())
.block();
}
that gives the following result to the console:
DATA-DATA
EMPTY
That means that the chain in the first flatMap
was recognized as an empty one.
On the other hand Reactor has the following class MonoEmpty that is returned by a Mono.empty()
method. On top of that, the method says the following:
/**
* Create a {@link Mono} that completes without emitting any item.
*
* <p>
* <img class="marble" src="doc-files/marbles/empty.svg" alt="">
* <p>
* @param <T> the reified {@link Subscriber} type
*
* @return a completed {@link Mono}
*/
public static <T> Mono<T> empty() {
return MonoEmpty.instance();
}
without emitting any item - but I emitted Void
typed object with then()
method.
What is the explanation of that?
Okay, the answer is in the official java doc that says The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void
.
That means that its main idea is simply to represent the void return type as a class and contain a Class<Void>
public value. That's it. Besides, it's not instantiable as the constructor is private. All of that subsequently means that the only value we can assign to a Void
variable is null
what always is recognized as empty Mono
.
helpful discussion: Java generics void/Void types