Record Class Validated.Valid<T>

java.lang.Object
java.lang.Record
de.fxlae.typeid.util.Validated.Valid<T>
Type Parameters:
T - the type of the value
Record Components:
value - the value to wrap
All Implemented Interfaces:
Validated<T>
Enclosing interface:
Validated<T>

public static record Validated.Valid<T>(T value) extends Record implements Validated<T>
Implementation of a "valid Validated".
  • Nested Class Summary

    Nested classes/interfaces inherited from interface de.fxlae.typeid.util.Validated

    Validated.Invalid<T>, Validated.Valid<T>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Valid(T value)
    Creates an instance of a Valid record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    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.
    final int
    Returns a hash code value for this object.
    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.
    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.
    final String
    Returns a string representation of this record class.
    Returns the value of the value record component.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Valid

      public Valid(T value)
      Creates an instance of a Valid record class.
      Parameters:
      value - the value for the value record component
  • Method Details

    • toOptional

      public Optional<T> toOptional()
      Description copied from interface: Validated
      Returns an Optional with the valid value, otherwise an empty Optional. In the latter case, the error message is lost.
      Specified by:
      toOptional in interface Validated<T>
      Returns:
      the Optional
    • isValid

      public boolean isValid()
      Description copied from interface: Validated
      Returns true if the value is valid, otherwise false.
      Specified by:
      isValid in interface Validated<T>
      Returns:
      true if the value is valid, otherwise false
    • get

      public T get()
      Description copied from interface: Validated
      Returns the value if it's valid, otherwise throws.
      Specified by:
      get in interface Validated<T>
      Returns:
      the valid value
    • orElse

      public T orElse(T other)
      Description copied from interface: Validated
      Returns the value if valid, otherwise return other.
      Specified by:
      orElse in interface Validated<T>
      Parameters:
      other - the value to be returned if the value is invalid, can be null
      Returns:
      the value if valid, else other
    • message

      public String message()
      Description copied from interface: Validated
      Returns the message if it's invalid, otherwise throws.
      Specified by:
      message in interface Validated<T>
      Returns:
      the message
    • map

      public <O> Validated<O> map(Function<? super T,? extends O> mapper)
      Description copied from interface: Validated
      Applies the provided mapping function if the value is valid, otherwise returns the current instance
      Specified by:
      map in interface Validated<T>
      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
    • flatMap

      public <O> Validated<O> flatMap(Function<? super T,Validated<O>> mapper)
      Description copied from interface: Validated
      Applies the provided mapping function if the value is valid, otherwise returns the current instance
      Specified by:
      flatMap in interface Validated<T>
      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
    • filter

      public Validated<T> filter(String message, Predicate<? super T> predicate)
      Description copied from interface: Validated
      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
      Specified by:
      filter in interface Validated<T>
      Parameters:
      message - the message in case the predicate doesn't match
      predicate - the predicate that checks the value
      Returns:
      the resulting Validated
    • ifValid

      public void ifValid(Consumer<T> valueConsumer)
      Description copied from interface: Validated
      Applies the consuming function if the value is valid, otherwise does nothing.
      Specified by:
      ifValid in interface Validated<T>
      Parameters:
      valueConsumer - the value Consumer
    • ifInvalid

      public void ifInvalid(Consumer<String> messageConsumer)
      Description copied from interface: Validated
      Applies the message consuming function if the value is invalid, otherwise does nothing.
      Specified by:
      ifInvalid in interface Validated<T>
      Parameters:
      messageConsumer - the message Consumer
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • value

      public T value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component