Interface Validator<V>
- Type Parameters:
V- the value type
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A validator reports a failure rather than raising one, which is what lets the same
check be applied in both directions: a codec throws IllegalArgumentException on encode
and CodecException on decode from a single validator. A validator that threw could not
honour that split, since it cannot know which side it is on.
Keeping the condition and its wording together is what makes a check shareable — the pairing is stated once and named, rather than restated at every call site:
static final Validator<Integer> POSITIVE_AMOUNT =
Validator.of(v -> v > 0, "amount must be positive");
Codec<Integer> amount = Codecs.uint16().validate(POSITIVE_AMOUNT);
Codec<Integer> fee = Codecs.uint32().validate(POSITIVE_AMOUNT.and(UNDER_LIMIT));
-
Method Summary
Modifier and TypeMethodDescriptionReturns a validator that applies this check and then the other, reporting the first failure.Checks a value.static <V> Validator<V> Creates a validator from a condition and a fixed message.static <V> Validator<V> Creates a validator from a condition and a message computed from the rejected value.
-
Method Details
-
check
-
of
Creates a validator from a condition and a fixed message.- Type Parameters:
V- the value type- Parameters:
check- the condition a value must satisfymessage- the failure message- Returns:
- a new validator
- Throws:
NullPointerException- if check or message is null
-
of
Creates a validator from a condition and a message computed from the rejected value.The message function runs only on failure, so it is free to be expensive —
expected [AABB] but got [CCDD]costs nothing on the values that pass.- Type Parameters:
V- the value type- Parameters:
check- the condition a value must satisfymessage- computes the failure message from the rejected value- Returns:
- a new validator
- Throws:
NullPointerException- if check or message is null
-
and
Returns a validator that applies this check and then the other, reporting the first failure.The second check does not run once the first has failed: a codec raises on the first problem, so there is nowhere for a second message to go.
- Parameters:
other- the check to apply after this one- Returns:
- the composed validator
- Throws:
NullPointerException- if other is null
-