package eu.ztsh.wymiana.validation; import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest; import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; public class ValidExchangeRequestValidator implements ConstraintValidator { @Override public boolean isValid(CurrencyExchangeRequest request, ConstraintValidatorContext constraintValidatorContext) { if (request == null) { return false; } // Apart from @NotNull annotation we need to check if request.from() != null to avoid NPE in equals return (request.from() != null && !request.from().equals(request.to())) && !((request.toBuy() == null && request.toSell() == null) || (request.toBuy() != null && request.toSell() != null)) && ((request.toBuy() != null && request.toBuy() >= 0) || (request.toSell() != null && request.toSell() >= 0)); } }