0
class AnotherClass {

    Option<Integer> validateAndGetInt(Xyz xyz, Mno mno, String strValue) {
        if (strValue != null) {
           int intValue = 1;
           
           try{
              intValue = Integer.parseInt(strValue);
           } catch (Exception e) {
              throw new Exception("err");
           }
       
           Printer.getInstance().validateInt(xyz, mno, intValue);

           return Optional.of(intValue);
        }
    }
} // class ends

class Printer {
   void validateInt(Xyz xyz, Mno mno, int x) {
      int m = xyz.getTeamVal(mno, x);
      if(m < 0) {
          throw new CustomException("invalid team value");
      }
   }
}

I am planning to move from void to CompletionStage<Void> for validate(int x).

validate will change as below, but I am not sure how to use in the calling side.

  CompletionStage<Void> validateInt(Xyz xyz, Mno mno, int x) {
      CompletableFuture<Void> f = new CompletableFuture<Void>();
      int m = xyz.getTeamVal(mno, x);
      if(m < 0) {
          f.completeExceptionally(new CustomException("invalid team value"));
      } else {
          f.complete(null);
      }
      return f;
   }

Can someone help explain how I can use it in AnotherClass?

I don't want to use this, as much as possible :

Printer.getInstance().validateInt(xyz, mno, intValue).toCompletableFuture().get()

sau123
  • 352
  • 5
  • 18
  • Well, what do you want to happen? Using a `CompletionStage` really only makes sense if `validateInt` executes asynchronously. – Slaw Aug 12 '20 at 22:59
  • @Slaw I have edited the question to update how `validateInt` will look like. – sau123 Aug 12 '20 at 23:02
  • Still not sure why you want to use `CompletionStage` here. Your method never executes asynchronously and always returns an already-completed stage—what's the point? – Slaw Aug 12 '20 at 23:17
  • are you saying this piece of code ``` CompletionStage validateInt(Xyz xyz, Mno mno, int x) { CompletableFuture f = new CompletableFuture(); int m = xyz.getTeamVal(mno, x); if(m < 0) { f.completeExceptionally(new CustomException("invalid team value")); } else { f.complete(null); } return f; } ``` is not asynchronous? I'm sorry I don't quite understand your comment. – sau123 Aug 13 '20 at 03:20
  • 1
    Yes, that's what I'm saying. Your method is not asynchronous. It creates a stage, fetches a value, tests the value, completes the stage based on said test, and then returns the stage. The caller now has a completed `CompletionStage` which it must query to know if it failed or not. My question is: Why add that layer of indirection? – Slaw Aug 13 '20 at 04:13
  • 1
    That said, I realize I'm questioning the premise of your question rather than answering it. If you want to avoid calling `toCompletableFuture().get()` then you need to call one of the other methods of `CompletionStage`. One option is `whenComplete[Async]` which is invoked when the triggering stage completes, whether successful or exceptional; you can test if the error is non-null to know if the validation failed. But I still question the wisdom of using `CompletionStage` in the first place (assuming your question adequately demonstrates your real code). – Slaw Aug 13 '20 at 04:17

0 Answers0