I have been playing with the validation pattern from this blog post. Everything works out as expected but I am unable to add generics. Namely,
public interface Validator extends Function<User, ValidationResult> {
static Validator validate(Predicate<User> tester, String error) {
return user ->
tester.test(user) ? ValidationResult.valid() :
ValidationResult.invalid(error);
}
}
However, when I try to make the Validator
interface generic
public interface Validator<T> extends Function<T, ValidationResult> {
static Validator validate(Predicate<T> tester, String error) {
return subject ->
tester.test(subject) ? ValidationResult.valid() :
ValidationResult.invalid(error);
}
}
I get a compilation error:
Validatior.this cannot be referenced from the static context.
I can't understand why. What am I doing wrong?