Processing Optional Value from Mono in Project Reactor











up vote
2
down vote

favorite
1












I want to have a Mono that calls another async method that returns an Optional type to:




  1. have a value if the Optional is not empty,

  2. is MonoEmpty if the Optional value is empty.


Here's what I do right now:




Mono.fromCallable(() -> someApi.asyncCall())
.filter(Optional::isPresent)
.map(Optional::get)



Obviously, this is not ideal since it uses two operators after the callable completed. If possible, I'd like to have the Mono.empty() or mono value from inside fromCallable.



What is the best way to achieve what I want?










share|improve this question


























    up vote
    2
    down vote

    favorite
    1












    I want to have a Mono that calls another async method that returns an Optional type to:




    1. have a value if the Optional is not empty,

    2. is MonoEmpty if the Optional value is empty.


    Here's what I do right now:




    Mono.fromCallable(() -> someApi.asyncCall())
    .filter(Optional::isPresent)
    .map(Optional::get)



    Obviously, this is not ideal since it uses two operators after the callable completed. If possible, I'd like to have the Mono.empty() or mono value from inside fromCallable.



    What is the best way to achieve what I want?










    share|improve this question
























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I want to have a Mono that calls another async method that returns an Optional type to:




      1. have a value if the Optional is not empty,

      2. is MonoEmpty if the Optional value is empty.


      Here's what I do right now:




      Mono.fromCallable(() -> someApi.asyncCall())
      .filter(Optional::isPresent)
      .map(Optional::get)



      Obviously, this is not ideal since it uses two operators after the callable completed. If possible, I'd like to have the Mono.empty() or mono value from inside fromCallable.



      What is the best way to achieve what I want?










      share|improve this question













      I want to have a Mono that calls another async method that returns an Optional type to:




      1. have a value if the Optional is not empty,

      2. is MonoEmpty if the Optional value is empty.


      Here's what I do right now:




      Mono.fromCallable(() -> someApi.asyncCall())
      .filter(Optional::isPresent)
      .map(Optional::get)



      Obviously, this is not ideal since it uses two operators after the callable completed. If possible, I'd like to have the Mono.empty() or mono value from inside fromCallable.



      What is the best way to achieve what I want?







      java reactive-programming project-reactor






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 8:46









      bertzzie

      1,82832137




      1,82832137
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          There is an alternative with flatMap that's a bit better than Optional.isPresent and Optional.get that can lead to accidentally calling get on empty Optional:



          Mono.fromCallable(() -> someApi.asyncCall())
          .flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))





          share|improve this answer





















          • Accepting this as it works. Thank you.
            – bertzzie
            Nov 8 at 2:01











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186025%2fprocessing-optional-value-from-mono-in-project-reactor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          There is an alternative with flatMap that's a bit better than Optional.isPresent and Optional.get that can lead to accidentally calling get on empty Optional:



          Mono.fromCallable(() -> someApi.asyncCall())
          .flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))





          share|improve this answer





















          • Accepting this as it works. Thank you.
            – bertzzie
            Nov 8 at 2:01















          up vote
          1
          down vote



          accepted










          There is an alternative with flatMap that's a bit better than Optional.isPresent and Optional.get that can lead to accidentally calling get on empty Optional:



          Mono.fromCallable(() -> someApi.asyncCall())
          .flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))





          share|improve this answer





















          • Accepting this as it works. Thank you.
            – bertzzie
            Nov 8 at 2:01













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          There is an alternative with flatMap that's a bit better than Optional.isPresent and Optional.get that can lead to accidentally calling get on empty Optional:



          Mono.fromCallable(() -> someApi.asyncCall())
          .flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))





          share|improve this answer












          There is an alternative with flatMap that's a bit better than Optional.isPresent and Optional.get that can lead to accidentally calling get on empty Optional:



          Mono.fromCallable(() -> someApi.asyncCall())
          .flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 11:21









          Ilya Zinkovich

          1,2401521




          1,2401521












          • Accepting this as it works. Thank you.
            – bertzzie
            Nov 8 at 2:01


















          • Accepting this as it works. Thank you.
            – bertzzie
            Nov 8 at 2:01
















          Accepting this as it works. Thank you.
          – bertzzie
          Nov 8 at 2:01




          Accepting this as it works. Thank you.
          – bertzzie
          Nov 8 at 2:01


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186025%2fprocessing-optional-value-from-mono-in-project-reactor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini