Package io.vavr.control
Interface Validation<E,T>
-
- Type Parameters:
E- the type of values in the case of invalidT- the type of values in the case of valid
- All Superinterfaces:
java.lang.Iterable<T>,java.io.Serializable,Value<T>
- All Known Implementing Classes:
Validation.Invalid,Validation.Valid
public interface Validation<E,T> extends Value<T>, java.io.Serializable
An implementation similar to Scalaz's Validation control.The
Validationtype is an applicative functor, not a Monad. While a Monad short-circuits on the first error, an applicative functor accumulates all errors, making it particularly useful for validation scenarios where all errors should be reported.// Validation construction: // Valid Validation<String, Integer> valid = Validation.valid(5); // Invalid Validation<List<String>, Integer>invalid = Validation.invalid(List.of("error1", "error2")); // Validation combination: Validation<String, String> valid1 = Validation.valid("John"); Validation<String, Integer> valid2 = Validation.valid(5); Validation<String, Option<String>> valid3 = Validation.valid(Option.of("123 Fake St.")); Function3<String, Integer, Option<String>, Person> f = ...; Validation<List<String>, String> result = valid1.combine(valid2).ap((name, age) -> "Name: " + name + " Age: " + age); Validation<List<String>, Person> result2 = valid1.combine(valid2).combine(valid3).ap(f); // Another way to combine validations: Validation<List<String>, Person> result3 = Validation.combine(valid1, valid2, valid3).ap(f);- See Also:
- Scalaz Validation source
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classValidation.Builder<E,T1,T2>A builder that holds two Validation instances, used for combining validations and applying functions that take two arguments.static classValidation.Builder3<E,T1,T2,T3>A builder that holds three Validation instances, used for combining validations and applying functions that take three arguments.static classValidation.Builder4<E,T1,T2,T3,T4>A builder that holds four Validation instances, used for combining validations and applying functions that take four arguments.static classValidation.Builder5<E,T1,T2,T3,T4,T5>A builder that holds five Validation instances, used for combining validations and applying functions that take five arguments.static classValidation.Builder6<E,T1,T2,T3,T4,T5,T6>A builder that holds six Validation instances, used for combining validations and applying functions that take six arguments.static classValidation.Builder7<E,T1,T2,T3,T4,T5,T6,T7>A builder that holds seven Validation instances, used for combining validations and applying functions that take seven arguments.static classValidation.Builder8<E,T1,T2,T3,T4,T5,T6,T7,T8>A builder that holds eight Validation instances, used for combining validations and applying functions that take eight arguments.static classValidation.Invalid<E,T>An invalid Validationstatic classValidation.Valid<E,T>A valid Validation
-
Field Summary
Fields Modifier and Type Field Description static longserialVersionUIDThe serial version UID for serialization.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <U> Validation<Seq<E>,U>ap(@NonNull Validation<Seq<E>,? extends java.util.function.Function<? super T,? extends U>> validation)Applies a validation containing a function to this validation's value, combining errors if both are invalid.default <E2,T2>
Validation<E2,T2>bimap(@NonNull java.util.function.Function<? super E,? extends E2> errorMapper, @NonNull java.util.function.Function<? super T,? extends T2> valueMapper)Whereas map only performs a mapping on a valid Validation, and mapError performs a mapping on an invalid Validation, bimap allows you to provide mapping actions for both, and will give you the result based on what type of Validation this is.static <E,T1,T2>
Validation.Builder<E,T1,T2>combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2)Combines twoValidations into aValidation.Builder.static <E,T1,T2,T3>
Validation.Builder3<E,T1,T2,T3>combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3)Combines threeValidations into aValidation.Builder3.static <E,T1,T2,T3,T4>
Validation.Builder4<E,T1,T2,T3,T4>combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4)Combines fourValidations into aValidation.Builder4.static <E,T1,T2,T3,T4,T5>
Validation.Builder5<E,T1,T2,T3,T4,T5>combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5)Combines fiveValidations into aValidation.Builder5.static <E,T1,T2,T3,T4,T5,T6>
Validation.Builder6<E,T1,T2,T3,T4,T5,T6>combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6)Combines sixValidations into aValidation.Builder6.static <E,T1,T2,T3,T4,T5,T6,T7>
Validation.Builder7<E,T1,T2,T3,T4,T5,T6,T7>combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6, @NonNull Validation<E,T7> validation7)Combines sevenValidations into aValidation.Builder7.static <E,T1,T2,T3,T4,T5,T6,T7,T8>
Validation.Builder8<E,T1,T2,T3,T4,T5,T6,T7,T8>combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6, @NonNull Validation<E,T7> validation7, @NonNull Validation<E,T8> validation8)Combines eightValidations into aValidation.Builder8.default <U> Validation.Builder<E,T,U>combine(Validation<E,U> validation)Combines twoValidations to form aValidation.Builder, which can then be used to perform further combines, or apply a function to it in order to transform theValidation.Builderinto aValidation.static <E,T>
Validation<E,T>cond(boolean test, @NonNull java.util.function.Supplier<? extends T> valid, @NonNull java.util.function.Supplier<? extends E> error)Decides whichValidation<E, T>to return, depending on the test value - if it's true, the result will be aValidation.Valid, if it's false - the result will be aValidation.Invalidstatic <E,T>
Validation<E,T>cond(boolean test, @NonNull T valid, @NonNull E error)Decides whichValidation<E, T>to return, depending on the test value - if it's true, the result will be aValidation.Valid, if it's false - the result will be aValidation.Invalidbooleanequals(java.lang.Object o)Clarifies that values have a proper equals() method implemented.default Option<Validation<E,T>>filter(@NonNull java.util.function.Predicate<? super T> predicate)Filters thisValidationby testing a predicate on the value.default <U> Validation<E,U>flatMap(@NonNull java.util.function.Function<? super T,? extends Validation<E,? extends U>> mapper)FlatMaps the value of this Validation if it is valid, otherwise returns this Invalid.default <U> Ufold(@NonNull java.util.function.Function<? super E,? extends U> ifInvalid, @NonNull java.util.function.Function<? super T,? extends U> ifValid)Transforms thisValidationto a value of typeU.default voidforEach(@NonNull java.util.function.Consumer<? super T> action)Performs the given action for the value contained inValid, or does nothing if this is anInvalid.static <E,T>
Validation<E,T>fromEither(@NonNull Either<E,T> either)Creates aValidationof anEither.static <T> Validation<java.lang.Throwable,T>fromTry(@NonNull Try<? extends T> t)Creates aValidationof anTry.Tget()Gets the value of thisValidationif is aValidor throws if this is anInvalid.EgetError()Gets the error of this Validation if it is anInvalidor throws if this is aValid.default TgetOrElseGet(@NonNull java.util.function.Function<? super E,? extends T> other)Gets the value if it is a Valid or an value calculated from the error.inthashCode()Clarifies that values have a proper hashCode() method implemented.static <E,T>
Validation<E,T>invalid(E error)Creates anValidation.Invalidthat contains the givenerror.default booleanisAsync()AValidation's value is computed synchronously.default booleanisEmpty()Checks, thisValueis empty, i.e.booleanisInvalid()Check whether this is of typeInvaliddefault booleanisLazy()AValidation's value is computed eagerly.default booleanisSingleValued()States whether this is a single-valued type.booleanisValid()Check whether this is of typeValiddefault @NonNull Iterator<T>iterator()Returns a richio.vavr.collection.Iterator.default <U> Validation<E,U>map(@NonNull java.util.function.Function<? super T,? extends U> f)Maps the underlying value to a different component type.default <U> Validation<U,T>mapError(@NonNull java.util.function.Function<? super E,? extends U> f)Applies a function f to the error of this Validation if this is an Invalid.static <E,T>
Validation<E,T>narrow(Validation<? extends E,? extends T> validation)Narrows a widenedValidation<? extends E, ? extends T>toValidation<E, T>by performing a type-safe cast.default Validation<E,T>orElse(@NonNull Validation<? extends E,? extends T> other)Returns thisValidationif it is valid, otherwise return the alternative.default Validation<E,T>orElse(@NonNull java.util.function.Supplier<Validation<? extends E,? extends T>> supplier)Returns thisValidationif it is valid, otherwise return the result of evaluating supplier.default Validation<E,T>peek(@NonNull java.util.function.Consumer<? super T> action)Performs the givenactionon the first element if this is an eager implementation.static <E,T>
Validation<Seq<E>,Seq<T>>sequence(@NonNull java.lang.Iterable<? extends Validation<? extends Seq<? extends E>,? extends T>> values)Reduces manyValidationinstances into a singleValidationby transforming anIterable<Validation<? extends T>>into aValidation<Seq<T>>.default Validation<T,E>swap()Flip the valid/invalid values for this Validation.default Either<E,T>toEither()Converts this Validation to anEither.java.lang.StringtoString()Clarifies that values have a proper toString() method implemented.static <E,T,U>
Validation<Seq<E>,Seq<U>>traverse(@NonNull java.lang.Iterable<? extends T> values, @NonNull java.util.function.Function<? super T,? extends Validation<? extends Seq<? extends E>,? extends U>> mapper)Maps the values of an iterable to a sequence of mapped values into a singleValidationby transforming anIterable<? extends T>into aValidation<Seq<U>>.static <E,T>
Validation<E,T>valid(T value)Creates aValidation.Validthat contains the givenvalue.-
Methods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, exists, forAll, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, mapTo, mapToVoid, out, out, spliterator, stderr, stdout, stringPrefix, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
The serial version UID for serialization.- See Also:
- Constant Field Values
-
-
Method Detail
-
valid
static <E,T> Validation<E,T> valid(T value)
Creates aValidation.Validthat contains the givenvalue.- Type Parameters:
E- type of the errorT- type of the givenvalue- Parameters:
value- A value- Returns:
Valid(value)
-
invalid
static <E,T> Validation<E,T> invalid(E error)
Creates anValidation.Invalidthat contains the givenerror.- Type Parameters:
E- type of the givenerrorT- type of the value- Parameters:
error- An error- Returns:
Invalid(error)- Throws:
java.lang.NullPointerException- if error is null
-
fromEither
static <E,T> Validation<E,T> fromEither(@NonNull Either<E,T> either)
Creates aValidationof anEither.- Type Parameters:
E- error typeT- value type- Parameters:
either- AnEither- Returns:
- A
Valid(either.get())if either is a Right, otherwiseInvalid(either.getLeft()). - Throws:
java.lang.NullPointerException- if either is null
-
fromTry
static <T> Validation<java.lang.Throwable,T> fromTry(@NonNull Try<? extends T> t)
Creates aValidationof anTry.- Type Parameters:
T- type of the valid value- Parameters:
t- ATry- Returns:
- A
Valid(t.get())if t is a Success, otherwiseInvalid(t.getCause()). - Throws:
java.lang.NullPointerException- iftis null
-
sequence
static <E,T> Validation<Seq<E>,Seq<T>> sequence(@NonNull java.lang.Iterable<? extends Validation<? extends Seq<? extends E>,? extends T>> values)
Reduces manyValidationinstances into a singleValidationby transforming anIterable<Validation<? extends T>>into aValidation<Seq<T>>.- Type Parameters:
E- value type in the case of invalidT- value type in the case of valid- Parameters:
values- An iterable of Validation instances.- Returns:
- A valid Validation of a sequence of values if all Validation instances are valid or an invalid Validation containing an accumulated List of errors.
- Throws:
java.lang.NullPointerException- if values is null
-
traverse
static <E,T,U> Validation<Seq<E>,Seq<U>> traverse(@NonNull java.lang.Iterable<? extends T> values, @NonNull java.util.function.Function<? super T,? extends Validation<? extends Seq<? extends E>,? extends U>> mapper)
Maps the values of an iterable to a sequence of mapped values into a singleValidationby transforming anIterable<? extends T>into aValidation<Seq<U>>.- Type Parameters:
T- The type of the given values.E- The mapped error value type.U- The mapped valid value type.- Parameters:
values- AnIterableof values.mapper- A mapper of values to Validations- Returns:
- A
Validationof aSeqof results. - Throws:
java.lang.NullPointerException- if values or f is null.
-
narrow
static <E,T> Validation<E,T> narrow(Validation<? extends E,? extends T> validation)
Narrows a widenedValidation<? extends E, ? extends T>toValidation<E, T>by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
E- type of errorT- type of valid value- Parameters:
validation- AValidation.- Returns:
- the given
validationinstance as narrowed typeValidation<E, T>.
-
cond
static <E,T> Validation<E,T> cond(boolean test, @NonNull java.util.function.Supplier<? extends T> valid, @NonNull java.util.function.Supplier<? extends E> error)
Decides whichValidation<E, T>to return, depending on the test value - if it's true, the result will be aValidation.Valid, if it's false - the result will be aValidation.Invalid- Type Parameters:
E- Type of errorT- Type of valid value- Parameters:
test- Abooleanvalue to evaluatevalid- ASupplier<? extends T>supplier of valid value, called if test is trueerror- ASupplier<? extends E>supplier of error, called if test is false- Returns:
Validation<E, T>with valid value or error, depending on the test condition evaluation- Throws:
java.lang.NullPointerException- if any of the arguments is null
-
cond
static <E,T> Validation<E,T> cond(boolean test, @NonNull T valid, @NonNull E error)
Decides whichValidation<E, T>to return, depending on the test value - if it's true, the result will be aValidation.Valid, if it's false - the result will be aValidation.Invalid- Type Parameters:
E- Type of errorT- Type of valid value- Parameters:
test- Abooleanvalue to evaluatevalid- ATvalid value, called if test is trueerror- AnEerror value, called if test is false- Returns:
Validation<E, T>with valid value or error, depending on the test condition evaluation- Throws:
java.lang.NullPointerException- if any of the arguments is null
-
combine
static <E,T1,T2> Validation.Builder<E,T1,T2> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2)
Combines twoValidations into aValidation.Builder.- Type Parameters:
E- type of errorT1- type of first valid valueT2- type of second valid value- Parameters:
validation1- first validationvalidation2- second validation- Returns:
- an instance of Builder<E,T1,T2>
- Throws:
java.lang.NullPointerException- if validation1 or validation2 is null
-
combine
static <E,T1,T2,T3> Validation.Builder3<E,T1,T2,T3> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3)
Combines threeValidations into aValidation.Builder3.- Type Parameters:
E- type of errorT1- type of first valid valueT2- type of second valid valueT3- type of third valid value- Parameters:
validation1- first validationvalidation2- second validationvalidation3- third validation- Returns:
- an instance of Builder3<E,T1,T2,T3>
- Throws:
java.lang.NullPointerException- if validation1, validation2 or validation3 is null
-
combine
static <E,T1,T2,T3,T4> Validation.Builder4<E,T1,T2,T3,T4> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4)
Combines fourValidations into aValidation.Builder4.- Type Parameters:
E- type of errorT1- type of first valid valueT2- type of second valid valueT3- type of third valid valueT4- type of fourth valid value- Parameters:
validation1- first validationvalidation2- second validationvalidation3- third validationvalidation4- fourth validation- Returns:
- an instance of Builder3<E,T1,T2,T3,T4>
- Throws:
java.lang.NullPointerException- if validation1, validation2, validation3 or validation4 is null
-
combine
static <E,T1,T2,T3,T4,T5> Validation.Builder5<E,T1,T2,T3,T4,T5> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5)
Combines fiveValidations into aValidation.Builder5.- Type Parameters:
E- type of errorT1- type of first valid valueT2- type of second valid valueT3- type of third valid valueT4- type of fourth valid valueT5- type of fifth valid value- Parameters:
validation1- first validationvalidation2- second validationvalidation3- third validationvalidation4- fourth validationvalidation5- fifth validation- Returns:
- an instance of Builder3<E,T1,T2,T3,T4,T5>
- Throws:
java.lang.NullPointerException- if validation1, validation2, validation3, validation4 or validation5 is null
-
combine
static <E,T1,T2,T3,T4,T5,T6> Validation.Builder6<E,T1,T2,T3,T4,T5,T6> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6)
Combines sixValidations into aValidation.Builder6.- Type Parameters:
E- type of errorT1- type of first valid valueT2- type of second valid valueT3- type of third valid valueT4- type of fourth valid valueT5- type of fifth valid valueT6- type of sixth valid value- Parameters:
validation1- first validationvalidation2- second validationvalidation3- third validationvalidation4- fourth validationvalidation5- fifth validationvalidation6- sixth validation- Returns:
- an instance of Builder3<E,T1,T2,T3,T4,T5,T6>
- Throws:
java.lang.NullPointerException- if validation1, validation2, validation3, validation4, validation5 or validation6 is null
-
combine
static <E,T1,T2,T3,T4,T5,T6,T7> Validation.Builder7<E,T1,T2,T3,T4,T5,T6,T7> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6, @NonNull Validation<E,T7> validation7)
Combines sevenValidations into aValidation.Builder7.- Type Parameters:
E- type of errorT1- type of first valid valueT2- type of second valid valueT3- type of third valid valueT4- type of fourth valid valueT5- type of fifth valid valueT6- type of sixth valid valueT7- type of seventh valid value- Parameters:
validation1- first validationvalidation2- second validationvalidation3- third validationvalidation4- fourth validationvalidation5- fifth validationvalidation6- sixth validationvalidation7- seventh validation- Returns:
- an instance of Builder3<E,T1,T2,T3,T4,T5,T6,T7>
- Throws:
java.lang.NullPointerException- if validation1, validation2, validation3, validation4, validation5, validation6 or validation7 is null
-
combine
static <E,T1,T2,T3,T4,T5,T6,T7,T8> Validation.Builder8<E,T1,T2,T3,T4,T5,T6,T7,T8> combine(@NonNull Validation<E,T1> validation1, @NonNull Validation<E,T2> validation2, @NonNull Validation<E,T3> validation3, @NonNull Validation<E,T4> validation4, @NonNull Validation<E,T5> validation5, @NonNull Validation<E,T6> validation6, @NonNull Validation<E,T7> validation7, @NonNull Validation<E,T8> validation8)
Combines eightValidations into aValidation.Builder8.- Type Parameters:
E- type of errorT1- type of first valid valueT2- type of second valid valueT3- type of third valid valueT4- type of fourth valid valueT5- type of fifth valid valueT6- type of sixth valid valueT7- type of seventh valid valueT8- type of eighth valid value- Parameters:
validation1- first validationvalidation2- second validationvalidation3- third validationvalidation4- fourth validationvalidation5- fifth validationvalidation6- sixth validationvalidation7- seventh validationvalidation8- eighth validation- Returns:
- an instance of Builder3<E,T1,T2,T3,T4,T5,T6,T7,T8>
- Throws:
java.lang.NullPointerException- if validation1, validation2, validation3, validation4, validation5, validation6, validation7 or validation8 is null
-
isValid
boolean isValid()
Check whether this is of typeValid- Returns:
- true if is a Valid, false if is an Invalid
-
isInvalid
boolean isInvalid()
Check whether this is of typeInvalid- Returns:
- true if is an Invalid, false if is a Valid
-
orElse
default Validation<E,T> orElse(@NonNull Validation<? extends E,? extends T> other)
Returns thisValidationif it is valid, otherwise return the alternative.- Parameters:
other- An alternativeValidation- Returns:
- this
Validationif it is valid, otherwise return the alternative.
-
orElse
default Validation<E,T> orElse(@NonNull java.util.function.Supplier<Validation<? extends E,? extends T>> supplier)
Returns thisValidationif it is valid, otherwise return the result of evaluating supplier.- Parameters:
supplier- An alternativeValidationsupplier- Returns:
- this
Validationif it is valid, otherwise return the result of evaluating supplier.
-
isEmpty
default boolean isEmpty()
Description copied from interface:ValueChecks, thisValueis empty, i.e. if the underlying value is absent.
-
get
T get()
Gets the value of thisValidationif is aValidor throws if this is anInvalid.
-
getOrElseGet
default T getOrElseGet(@NonNull java.util.function.Function<? super E,? extends T> other)
Gets the value if it is a Valid or an value calculated from the error.- Parameters:
other- a function which converts an error to an alternative value- Returns:
- the value, if the underlying Validation is a Valid, or else the alternative value
provided by
otherby applying the error.
-
getError
E getError()
Gets the error of this Validation if it is anInvalidor throws if this is aValid.- Returns:
- The error, if present
- Throws:
java.lang.RuntimeException- if this is aValid
-
toEither
default Either<E,T> toEither()
Converts this Validation to anEither.- Returns:
Either.right(get())if this is valid, otherwiseEither.left(getError()).
-
equals
boolean equals(java.lang.Object o)
Description copied from interface:ValueClarifies that values have a proper equals() method implemented.
-
hashCode
int hashCode()
Description copied from interface:ValueClarifies that values have a proper hashCode() method implemented.See Object.hashCode().
-
toString
java.lang.String toString()
Description copied from interface:ValueClarifies that values have a proper toString() method implemented.See Object.toString().
-
forEach
default void forEach(@NonNull java.util.function.Consumer<? super T> action)
Performs the given action for the value contained inValid, or does nothing if this is anInvalid.
-
fold
default <U> U fold(@NonNull java.util.function.Function<? super E,? extends U> ifInvalid, @NonNull java.util.function.Function<? super T,? extends U> ifValid)
Transforms thisValidationto a value of typeU.Example:
Validation<List<String>, String> valid = ...;<br> int i = valid.fold(List::length, String::length);- Type Parameters:
U- the fold result type- Parameters:
ifInvalid- an error mapperifValid- an mapper for a valid value- Returns:
ifValid.apply(get())if this is valid, otherwiseifInvalid.apply(getError()).- Throws:
java.lang.NullPointerException- if one of the given mappersifInvalidorifValidis null
-
swap
default Validation<T,E> swap()
Flip the valid/invalid values for this Validation. If this is a Valid<E,T>, returns Invalid<T,E>. Or if this is an Invalid<E,T>, return a Valid<T,E>.- Returns:
- a flipped instance of Validation
-
map
default <U> Validation<E,U> map(@NonNull java.util.function.Function<? super T,? extends U> f)
Description copied from interface:ValueMaps the underlying value to a different component type.
-
bimap
default <E2,T2> Validation<E2,T2> bimap(@NonNull java.util.function.Function<? super E,? extends E2> errorMapper, @NonNull java.util.function.Function<? super T,? extends T2> valueMapper)
Whereas map only performs a mapping on a valid Validation, and mapError performs a mapping on an invalid Validation, bimap allows you to provide mapping actions for both, and will give you the result based on what type of Validation this is. Without this, you would have to do something like: validation.map(...).mapError(...);- Type Parameters:
E2- type of the mapping result if this is an invalidT2- type of the mapping result if this is a valid- Parameters:
errorMapper- the invalid mapping operationvalueMapper- the valid mapping operation- Returns:
- an instance of Validation<U,R>
- Throws:
java.lang.NullPointerException- if invalidMapper or validMapper is null
-
mapError
default <U> Validation<U,T> mapError(@NonNull java.util.function.Function<? super E,? extends U> f)
Applies a function f to the error of this Validation if this is an Invalid. Otherwise does nothing if this is a Valid.- Type Parameters:
U- type of the error resulting from the mapping- Parameters:
f- a function that maps the error in this Invalid- Returns:
- an instance of Validation<U,T>
- Throws:
java.lang.NullPointerException- if mapping operation f is null
-
ap
default <U> Validation<Seq<E>,U> ap(@NonNull Validation<Seq<E>,? extends java.util.function.Function<? super T,? extends U>> validation)
Applies a validation containing a function to this validation's value, combining errors if both are invalid. This is the applicative functor's ap operation for Validation.- Type Parameters:
U- type of the result of applying the function- Parameters:
validation- the validation containing the function to apply- Returns:
- a valid Validation with the result if both are valid, otherwise an invalid Validation with accumulated errors
- Throws:
java.lang.NullPointerException- if validation is null
-
combine
default <U> Validation.Builder<E,T,U> combine(Validation<E,U> validation)
Combines twoValidations to form aValidation.Builder, which can then be used to perform further combines, or apply a function to it in order to transform theValidation.Builderinto aValidation.- Type Parameters:
U- type of the value contained in validation- Parameters:
validation- the validation object to combine this with- Returns:
- an instance of Builder
-
filter
default Option<Validation<E,T>> filter(@NonNull java.util.function.Predicate<? super T> predicate)
Filters thisValidationby testing a predicate on the value. If this is an Invalid or if the predicate matches, returns Some of this Validation, otherwise returns None.- Parameters:
predicate- A predicate to test the value- Returns:
Some(this)if this is an Invalid or the predicate matches, otherwiseNone- Throws:
java.lang.NullPointerException- if predicate is null
-
flatMap
default <U> Validation<E,U> flatMap(@NonNull java.util.function.Function<? super T,? extends Validation<E,? extends U>> mapper)
FlatMaps the value of this Validation if it is valid, otherwise returns this Invalid.- Type Parameters:
U- type of the returned Validation value- Parameters:
mapper- the mapper function to apply to the value- Returns:
- a new Validation
- Throws:
java.lang.NullPointerException- if mapper is null
-
peek
default Validation<E,T> peek(@NonNull java.util.function.Consumer<? super T> action)
Description copied from interface:ValuePerforms the givenactionon the first element if this is an eager implementation. Performs the givenactionon all elements (the first immediately, successive deferred), if this is a lazy implementation.
-
isAsync
default boolean isAsync()
AValidation's value is computed synchronously.
-
isLazy
default boolean isLazy()
AValidation's value is computed eagerly.
-
isSingleValued
default boolean isSingleValued()
Description copied from interface:ValueStates whether this is a single-valued type.- Specified by:
isSingleValuedin interfaceValue<E>- Returns:
trueif this is single-valued,falseotherwise.
-
-