25 lines
1 KiB
Java
25 lines
1 KiB
Java
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<ValidExchangeRequest, CurrencyExchangeRequest> {
|
|
|
|
@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().signum() >= 0)
|
|
|| (request.toSell() != null && request.toSell().signum() >= 0));
|
|
}
|
|
|
|
}
|