Record Class Throwing<T>

java.lang.Object
java.lang.Record
com.winterhavenmc.library.messagebuilder.validation.Throwing<T>
Type Parameters:
T - the type of the value being validated
All Implemented Interfaces:
Validator<T>

public record Throwing<T>(Supplier<ValidationException> exceptionSupplier) extends Record implements Validator<T>
A Validator implementation that throws a ValidationException when a value fails validation.

The exception is supplied via a Supplier, allowing lazy instantiation and support for dynamic message construction.

The exception's stack trace is reset via Throwable.fillInStackTrace() to ensure that the reported call site accurately reflects the point of failure, rather than the supplier's origin.

Typical usage:

 validate(value, Predicate, Validator.throwing(ErrorMessageKey, Parameter));
See Also:
  • Constructor Details

    • Throwing

      public Throwing(Supplier<ValidationException> exceptionSupplier)
      Creates an instance of a Throwing record class.
      Parameters:
      exceptionSupplier - the value for the exceptionSupplier record component
  • Method Details

    • handleInvalid

      public Optional<T> handleInvalid(T value)
      Throws the supplied exception when the value is invalid.
      Specified by:
      handleInvalid in interface Validator<T>
      Parameters:
      value - the invalid value
      Returns:
      never returns normally
      Throws:
      ValidationException - always thrown when this handler is invoked
    • 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.
    • exceptionSupplier

      public Supplier<ValidationException> exceptionSupplier()
      Returns the value of the exceptionSupplier record component.
      Returns:
      the value of the exceptionSupplier record component