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.

@FunctionalInterface public interface Validator<V>
A named check on a value, pairing the condition with the message that describes its failure.

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 Type
    Method
    Description
    default Validator<V>
    and(Validator<V> other)
    Returns a validator that applies this check and then the other, reporting the first failure.
    check(V value)
    Checks a value.
    static <V> Validator<V>
    of(Predicate<V> check, String message)
    Creates a validator from a condition and a fixed message.
    static <V> Validator<V>
    of(Predicate<V> check, Function<V,String> message)
    Creates a validator from a condition and a message computed from the rejected value.
  • Method Details

    • check

      Optional<String> check(V value)
      Checks a value.
      Parameters:
      value - the value to check
      Returns:
      the failure message, or empty if the value is acceptable
    • of

      static <V> Validator<V> of(Predicate<V> check, String message)
      Creates a validator from a condition and a fixed message.
      Type Parameters:
      V - the value type
      Parameters:
      check - the condition a value must satisfy
      message - the failure message
      Returns:
      a new validator
      Throws:
      NullPointerException - if check or message is null
    • of

      static <V> Validator<V> of(Predicate<V> check, Function<V,String> message)
      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 satisfy
      message - computes the failure message from the rejected value
      Returns:
      a new validator
      Throws:
      NullPointerException - if check or message is null
    • and

      default Validator<V> and(Validator<V> other)
      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