Interface Validated<T>

Type Parameters:
T - the type of the contained value.
All Known Implementing Classes:
Validated.Invalid, Validated.Valid

public sealed interface Validated<T> permits Validated.Valid<T>, Validated.Invalid<T>
A container for a value that is either valid or not. The two states are represented by Validated.Valid and Validated.Invalid respectively. In this library, in is used to represent the result of parsing a TypeId string. However, the data structure itself is completely independent of TypeIDs.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final record 
    Implementation of an "invalid Validated".
    static final record 
    Implementation of a "valid Validated".
  • Method Summary

    Modifier and Type
    Method
    Description
    filter(String message, Predicate<? super T> predicate)
    If the value is valid and matches the predicate, return it as a valid Validated, otherwise return an invalid Validated with the given error message.
    <O> Validated<O>
    flatMap(Function<? super T,Validated<O>> mapper)
    Applies the provided mapping function if the value is valid, otherwise returns the current instance
    get()
    Returns the value if it's valid, otherwise throws.
    void
    ifInvalid(Consumer<String> messageConsumer)
    Applies the message consuming function if the value is invalid, otherwise does nothing.
    void
    ifValid(Consumer<T> valueConsumer)
    Applies the consuming function if the value is valid, otherwise does nothing.
    static <T> Validated<T>
    invalid(String message)
    Returns a new invalid Validated with the given error message.
    boolean
    Returns true if the value is valid, otherwise false.
    <O> Validated<O>
    map(Function<? super T,? extends O> mapper)
    Applies the provided mapping function if the value is valid, otherwise returns the current instance
    Returns the message if it's invalid, otherwise throws.
    orElse(T other)
    Returns the value if valid, otherwise return other.
    Returns an Optional with the valid value, otherwise an empty Optional.
    static <T> Validated<T>
    valid(T value)
    Returns a new valid Validated with the given value.
  • Method Details

    • valid

      static <T> Validated<T> valid(T value)
      Returns a new valid Validated with the given value.
      Type Parameters:
      T - the type of the value
      Parameters:
      value - the value to wrap
      Returns:
      the valid Validated
      Throws:
      NullPointerException - if the provided value is null
    • invalid

      static <T> Validated<T> invalid(String message)
      Returns a new invalid Validated with the given error message.
      Type Parameters:
      T - the type of the invalid value
      Parameters:
      message - the error message
      Returns:
      the invalid Validated
      Throws:
      NullPointerException - if the message is null
    • toOptional

      Optional<T> toOptional()
      Returns an Optional with the valid value, otherwise an empty Optional. In the latter case, the error message is lost.
      Returns:
      the Optional
    • isValid

      boolean isValid()
      Returns true if the value is valid, otherwise false.
      Returns:
      true if the value is valid, otherwise false
    • get

      T get()
      Returns the value if it's valid, otherwise throws.
      Returns:
      the valid value
      Throws:
      NoSuchElementException - if the value is invalid
    • orElse

      T orElse(T other)
      Returns the value if valid, otherwise return other.
      Parameters:
      other - the value to be returned if the value is invalid, can be null
      Returns:
      the value if valid, else other
    • message

      String message()
      Returns the message if it's invalid, otherwise throws.
      Returns:
      the message
      Throws:
      NoSuchElementException - if the value is valid
    • map

      <O> Validated<O> map(Function<? super T,? extends O> mapper)
      Applies the provided mapping function if the value is valid, otherwise returns the current instance
      Type Parameters:
      O - The type of the mapping function's result
      Parameters:
      mapper - the mapping function
      Returns:
      the result of applying the mapping function to the value of this Validated instance
      Throws:
      NullPointerException - if the mapping function is null
    • flatMap

      <O> Validated<O> flatMap(Function<? super T,Validated<O>> mapper)
      Applies the provided mapping function if the value is valid, otherwise returns the current instance
      Type Parameters:
      O - The type parameter of the mapping function's returned Validated
      Parameters:
      mapper - the mapping function
      Returns:
      the result of applying the mapping function to the value of this Validated instance
      Throws:
      NullPointerException - if the mapping function is null
    • filter

      Validated<T> filter(String message, Predicate<? super T> predicate)
      If the value is valid and matches the predicate, return it as a valid Validated, otherwise return an invalid Validated with the given error message. If the value is invalid in the first place, return the current invalid Validated
      Parameters:
      message - the message in case the predicate doesn't match
      predicate - the predicate that checks the value
      Returns:
      the resulting Validated
      Throws:
      NullPointerException - if the message and/or predicate is null
    • ifValid

      void ifValid(Consumer<T> valueConsumer)
      Applies the consuming function if the value is valid, otherwise does nothing.
      Parameters:
      valueConsumer - the value Consumer
      Throws:
      NullPointerException - if the Consumer is null
    • ifInvalid

      void ifInvalid(Consumer<String> messageConsumer)
      Applies the message consuming function if the value is invalid, otherwise does nothing.
      Parameters:
      messageConsumer - the message Consumer
      Throws:
      NullPointerException - if the Consumer is null