how to change method signature from void to CompletionStage<Void>?

67 views Asked by At
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()

0

There are 0 answers