T - The value typepublic abstract class AbstractValidator<T> extends Object implements Validator<T>
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractValidator(String errorMessage)
Constructs a validator with the given error message.
|
| Modifier and Type | Method and Description |
|---|---|
protected String |
getMessage(T value)
Returns the error message for the given value.
|
protected ValidationResult |
toResult(T value,
boolean isValid)
A helper method for creating a
Result from a value and a validity
flag. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitalwaysPass, apply, from, from, from, fromandThenprotected AbstractValidator(String errorMessage)
errorMessage - the message to be included in a failed result, not nullprotected String getMessage(T value)
value - an invalid valueprotected ValidationResult toResult(T value, boolean isValid)
Result from a value and a validity
flag. If the flag is true, returns Result.ok, otherwise yields
Result.error bearing the error message returned by
getMessage(Object).
For instance, the following apply method only accepts even
numbers:
@Override
public Result<T> apply(Integer value) {
return toResult(value, value % 2 == 0);
}
value - the validated valueisValid - whether the value is valid or notCopyright © 2018. All rights reserved.