0
public void func() {
  Mono<Boolean> monoBool = ...
  if (!monoBool) {
     throw new CustomException();
  }
}

I have Mono. How i can use him in if statement?

asp
  • 1
  • 1
  • 2
  • 1
    I'd say that the idea is that you don't - similar to Optional, you usually should not check if it contains a value. Instead, you define the actions to take in this case in a more declarative way. – Hulk Aug 13 '20 at 11:34
  • 1
    Have you tried checking the javadoc? –  Aug 13 '20 at 11:35
  • 1
    The [JavaDocs of Mono](https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html) recommend reading this [Reference Guide chapter](https://projectreactor.io/docs/core/release/reference/index.html#which-operator). – Hulk Aug 13 '20 at 11:47

2 Answers2

1

The ! operator basically works for boolean expressions.

So obviously,

boolean someBool = ...
if (!someBool) 

works. The same works when the variable is of type Boolean, as the reference type can be boxed to the primitive type easily.

But there is no rule that would tell java how to turn some generic class (no matter the generic type it is using) into boolean. Therefore !monoBol isn't valid java code.

But, in your case, that Mono instance might provide one (or more) Boolean values to you.

In other words: educate yourself how to use that class, see here for example.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

You can use import java.util.function.Supplier argument method of Mono.

For Example:

boolean flag = true;
Mono<Boolean> monoExample = Mono.fromSupplier(()->{
   if(flag) {
      throw new RuntimeException();
   }
   return flag;
});