spring-cloud-stream-demo icon indicating copy to clipboard operation
spring-cloud-stream-demo copied to clipboard

Statuses.valueOf instead of checking against all the enum values

Open fractalliter opened this issue 1 year ago • 0 comments

https://github.com/benwilcock/spring-cloud-stream-demo/blob/fbdadd1b8d30c5f3d42c18e03835474aaa052f29/loancheck/src/main/java/io/pivotal/loancheck/Loan.java#L34-L38

https://github.com/benwilcock/spring-cloud-stream-demo/blob/fbdadd1b8d30c5f3d42c18e03835474aaa052f29/loansource/src/main/java/io/pivotal/loansource/Loan.java#L37-L46

Instead of all checking against all of the Statuses enum values, we can easily check the existence of the status with the help of Statuses.valueOf which throws IllegalArgumentException as well.

try {
      this.status = Statuses.valueOf(status).name();
} catch (IllegalArgumentException ex){
      throw new IllegalArgumentException("Cannot set the LoanApplication's status to " + status);
}

fractalliter avatar Mar 31 '23 16:03 fractalliter