Class Validation.Invalid<E,T>

java.lang.Object
cloud.opencode.base.functional.monad.Validation.Invalid<E,T>
Type Parameters:
E - error type - 错误类型
T - value type - 值类型
All Implemented Interfaces:
Validation<E,T>
Enclosing interface:
Validation<E,T>

public static final class Validation.Invalid<E,T> extends Object implements Validation<E,T>
Invalid - Represents a failed validation with accumulated errors Invalid - 表示失败的验证,包含累积的错误
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
  • Constructor Details

    • Invalid

      public Invalid(List<E> errors)
  • Method Details

    • errors

      public List<E> errors()
      Get the errors 获取错误列表
      Returns:
      the errors - 错误列表
    • isValid

      public boolean isValid()
      Description copied from interface: Validation
      Check if this is Valid 检查是否有效
      Specified by:
      isValid in interface Validation<E,T>
      Returns:
      true if valid - 如果有效返回 true
    • isInvalid

      public boolean isInvalid()
      Description copied from interface: Validation
      Check if this is Invalid 检查是否无效
      Specified by:
      isInvalid in interface Validation<E,T>
      Returns:
      true if invalid - 如果无效返回 true
    • getValue

      public Optional<T> getValue()
      Description copied from interface: Validation
      Get the value if Valid 获取值(如果有效)
      Specified by:
      getValue in interface Validation<E,T>
      Returns:
      Optional containing value
    • getErrors

      public List<E> getErrors()
      Description copied from interface: Validation
      Get the errors (empty list if Valid) 获取错误列表(如果有效则为空)
      Specified by:
      getErrors in interface Validation<E,T>
      Returns:
      list of errors
    • mapError

      public <E2> Validation<E2,T> mapError(Function<? super E, ? extends E2> mapper)
      Description copied from interface: Validation
      Transform the error type if Invalid 如果无效则转换错误类型

      If this is Valid, returns itself (cast). If Invalid, transforms each error using the mapper function.

      如果有效,返回自身(类型转换)。如果无效,使用映射函数转换每个错误。

      Example | 示例:

      Validation<String, Integer> v = Validation.invalid("bad");
      Validation<Error, Integer> mapped = v.mapError(Error::new);
      
      Specified by:
      mapError in interface Validation<E,T>
      Type Parameters:
      E2 - new error type - 新的错误类型
      Parameters:
      mapper - error transformation function - 错误转换函数
      Returns:
      Validation with transformed errors - 错误已转换的 Validation
    • map

      public <U> Validation<E,U> map(Function<? super T, ? extends U> mapper)
      Description copied from interface: Validation
      Transform the value if Valid 如果有效则转换值
      Specified by:
      map in interface Validation<E,T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function - 转换函数
      Returns:
      transformed Validation
    • flatMap

      public <U> Validation<E,U> flatMap(Function<? super T, Validation<E,U>> mapper)
      Description copied from interface: Validation
      FlatMap the value if Valid (monadic bind) 如果有效则扁平映射值(单子绑定)

      Note: Unlike Validation.ap(Validation), flatMap does NOT accumulate errors. If you need error accumulation, use Validation.ap(Validation) or Validation.combine(Validation, Validation, BiFunction).

      注意:与 Validation.ap(Validation) 不同,flatMap 不会累积错误。 如果需要累积错误,请使用 Validation.ap(Validation)Validation.combine(Validation, Validation, BiFunction)

      Specified by:
      flatMap in interface Validation<E,T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - function returning Validation - 返回 Validation 的函数
      Returns:
      resulting Validation
    • ap

      public <U> Validation<E,U> ap(Validation<E, Function<? super T, ? extends U>> vf)
      Description copied from interface: Validation
      Apply a validated function to this validation (applicative) 将验证的函数应用于此验证(应用函子)

      This is the key operation for accumulating errors.

      这是累积错误的关键操作。

      Specified by:
      ap in interface Validation<E,T>
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      vf - validation containing a function - 包含函数的验证
      Returns:
      combined Validation
    • fold

      public <R> R fold(Function<? super List<E>, ? extends R> ifInvalid, Function<? super T, ? extends R> ifValid)
      Description copied from interface: Validation
      Fold this Validation by applying one of two functions 通过应用两个函数之一来折叠此 Validation

      This is the catamorphism for Validation - handles both cases symmetrically.

      这是 Validation 的态射 - 对称地处理两种情况。

      Example | 示例:

      String result = validation.fold(
          errors -> "Errors: " + errors.size(),  // Invalid case
          value -> "Success: " + value           // Valid case
      );
      
      Specified by:
      fold in interface Validation<E,T>
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      ifInvalid - function for Invalid case (receives error list) - Invalid 情况的函数(接收错误列表)
      ifValid - function for Valid case - Valid 情况的函数
      Returns:
      result of applying the appropriate function
    • toEither

      public Either<List<E>,T> toEither()
      Description copied from interface: Validation
      Convert to Either (errors as Left, value as Right) 转换为 Either(错误为 Left,值为 Right)
      Specified by:
      toEither in interface Validation<E,T>
      Returns:
      Either
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object