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>
Invalid - Represents a failed validation with accumulated errors
Invalid - 表示失败的验证,包含累积的错误
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
-
Nested Class Summary
Nested classes/interfaces inherited from interface Validation
Validation.Invalid<E,T>, Validation.Valid<E, T> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<U> Validation<E, U> ap(Validation<E, Function<? super T, ? extends U>> vf) Apply a validated function to this validation (applicative) 将验证的函数应用于此验证(应用函子)booleanerrors()Get the errors 获取错误列表<U> Validation<E, U> flatMap(Function<? super T, Validation<E, U>> mapper) FlatMap the value if Valid (monadic bind) 如果有效则扁平映射值(单子绑定)<R> RFold this Validation by applying one of two functions 通过应用两个函数之一来折叠此 ValidationGet the errors (empty list if Valid) 获取错误列表(如果有效则为空)getValue()Get the value if Valid 获取值(如果有效)inthashCode()booleanCheck if this is Invalid 检查是否无效booleanisValid()Check if this is Valid 检查是否有效<U> Validation<E, U> Transform the value if Valid 如果有效则转换值<E2> Validation<E2, T> Transform the error type if Invalid 如果无效则转换错误类型toEither()Convert to Either (errors as Left, value as Right) 转换为 Either(错误为 Left,值为 Right)toString()Methods inherited from interface Validation
getOrElseThrow, peek, stream, toOption, toTry
-
Constructor Details
-
Invalid
-
-
Method Details
-
errors
-
isValid
public boolean isValid()Description copied from interface:ValidationCheck if this is Valid 检查是否有效- Specified by:
isValidin interfaceValidation<E,T> - Returns:
- true if valid - 如果有效返回 true
-
isInvalid
public boolean isInvalid()Description copied from interface:ValidationCheck if this is Invalid 检查是否无效- Specified by:
isInvalidin interfaceValidation<E,T> - Returns:
- true if invalid - 如果无效返回 true
-
getValue
Description copied from interface:ValidationGet the value if Valid 获取值(如果有效)- Specified by:
getValuein interfaceValidation<E,T> - Returns:
- Optional containing value
-
getErrors
Description copied from interface:ValidationGet the errors (empty list if Valid) 获取错误列表(如果有效则为空)- Specified by:
getErrorsin interfaceValidation<E,T> - Returns:
- list of errors
-
mapError
Description copied from interface:ValidationTransform 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:
mapErrorin interfaceValidation<E,T> - Type Parameters:
E2- new error type - 新的错误类型- Parameters:
mapper- error transformation function - 错误转换函数- Returns:
- Validation with transformed errors - 错误已转换的 Validation
-
map
Description copied from interface:ValidationTransform the value if Valid 如果有效则转换值- Specified by:
mapin interfaceValidation<E,T> - Type Parameters:
U- result type - 结果类型- Parameters:
mapper- transformation function - 转换函数- Returns:
- transformed Validation
-
flatMap
Description copied from interface:ValidationFlatMap the value if Valid (monadic bind) 如果有效则扁平映射值(单子绑定)Note: Unlike
Validation.ap(Validation), flatMap does NOT accumulate errors. If you need error accumulation, useValidation.ap(Validation)orValidation.combine(Validation, Validation, BiFunction).注意:与
Validation.ap(Validation)不同,flatMap 不会累积错误。 如果需要累积错误,请使用Validation.ap(Validation)或Validation.combine(Validation, Validation, BiFunction)。- Specified by:
flatMapin interfaceValidation<E,T> - Type Parameters:
U- result type - 结果类型- Parameters:
mapper- function returning Validation - 返回 Validation 的函数- Returns:
- resulting Validation
-
ap
Description copied from interface:ValidationApply a validated function to this validation (applicative) 将验证的函数应用于此验证(应用函子)This is the key operation for accumulating errors.
这是累积错误的关键操作。
- Specified by:
apin interfaceValidation<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:ValidationFold this Validation by applying one of two functions 通过应用两个函数之一来折叠此 ValidationThis 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:
foldin interfaceValidation<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
Description copied from interface:ValidationConvert to Either (errors as Left, value as Right) 转换为 Either(错误为 Left,值为 Right)- Specified by:
toEitherin interfaceValidation<E,T> - Returns:
- Either
-
equals
-
hashCode
-
toString
-