title: Chaining several CompletionStage only if a condition is achieved
body: <p>I have several <code>CompletionStage</code> methods that I'd like to chain. The problem is that the result of the first one will determine if the next ones should be executed. Right now the only way to achieve this seems to be passing "special" arguments to next <code>CompletionStage</code> so it doesn't execute the full code. For example:</p>
  
  <pre><code>public enum SomeResult {
      RESULT_1,
      RESULT_2,
      RESULT_3
  }
  
  public CompletionStage&lt;SomeResult&gt; someMethod(SomeArgument someArgument) {
  
      return CompletableFuture.supplyAsync(() -&gt; {
          // loooooong operation
          if (someCondition)
              return validValue;
          else
              return null;
      }).thenCompose(result -&gt; {
          if (result != null)
              return someMethodThatReturnsACompletionStage(result);
          else
              return CompletableFuture.completedFuture(null);
      }).thenApply(result -&gt; {
          if (result == null)
              return ChainingResult.RESULT_1;
          else if (result.someCondition())
              return ChainingResult.RESULT_2;
          else
              return ChainingResult.RESULT_3;
      });
  }
  </code></pre>
  
  <p>Since the whole code depends on the first <code>someCondition</code> (if it's <code>false</code> then the result will be <code>RESULT_1</code>, if not then the whole code should be executed) this construction looks a bit ugly to me. Is there any way to decide if 2nd (<code>thenCompose(...)</code>) and 3rd (<code>thenApply(...)</code>) methods should be executed?</p>