org.solidcoding.validation.api.EndingValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solidcoding-validation Show documentation
Show all versions of solidcoding-validation Show documentation
A small package that enables validation of (business) rules through a fluent API.
package org.solidcoding.validation.api;
import java.util.function.Function;
import java.util.function.Supplier;
final class EndingValidator extends AbstractLoggingValidator implements ReturningValidator {
private final Supplier supplier;
EndingValidator(Supplier supplier, ContinuingValidator validator) {
super(validator);
this.supplier = supplier;
}
public R orElseThrow(Function throwableFunction) {
var isValid = validator.validate();
if (!isValid) {
throw throwableFunction.apply(validator.getMessage());
}
return supplier.get();
}
@Override
public R orElseReturn(R other) {
var isValid = validator.validate();
if (!isValid) {
return other;
}
return supplier.get();
}
@Override
public R orElseReturn(Function other) {
var isValid = validator.validate();
var message = validator.getMessage();
if (!isValid) {
return other.apply(message);
}
return supplier.get();
}
}