Class Fallible<T>
- java.lang.Object
-
- be.sddevelopment.commons.validation.Fallible<T>
-
public class Fallible<T> extends Object
Monad-style Wrapper class to be used for condition based execution flows. Intended for use in validation-style flows.
Usage examples
List
errors = Fallible.of("") .ensure(this::mayNotContainNaughtyWords, d -> b -> b.errorCode("NAUGHTY_ERROR")) .ensure(StringUtils::isNotBlank, d -> b -> b.errorCode("MUST_BE_FILLED_IN")) .ensure(StringUtils::isMixedCase, d -> b -> b.errorCode("MUST_BE_MIXED_CASE")) .failures(); - Since:
- 1.0.0
- Version:
- 1.0.0
- Author:
- Stijn Dejongh
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Fallible<T>
ensure(Rule<T> rule)
Fallible<T>
ensure(Function<T,Boolean> assertion)
ensure.Fallible<T>
ensure(Function<T,Boolean> assertion, Function<T,Function<Failure.FailureBuilder,Failure.FailureBuilder>> error)
ensure.Fallible<T>
errorTemplate(ErrorTemplate<T> template)
errorTemplate.List<Failure>
failures()
Get theFailure
objects resulting from applying all required rules to the data object.Fallible<T>
ifValid(Consumer<T> actionToTake)
Execute aConsumer
that takes an object of type T, when all previous {#link ensure()} conditions are met.boolean
isValid()
isValid.static <S> Fallible<S>
of(S toValidate)
Fallible<T>
orElse(Consumer<T> actionToTake)
-
-
-
Method Detail
-
of
public static <S> Fallible<S> of(S toValidate)
- Type Parameters:
S
- class of the data object, will be inferred at creation time.- Parameters:
toValidate
- the data object to execute conditional operations on.- Returns:
- a
Fallible
for a data object of class S.
-
ensure
public Fallible<T> ensure(Function<T,Boolean> assertion, Function<T,Function<Failure.FailureBuilder,Failure.FailureBuilder>> error)
ensure.
-
ensure
public Fallible<T> ensure(Rule<T> rule)
- Parameters:
rule
- to check- Returns:
- a
Fallible
object.
-
ifValid
public Fallible<T> ifValid(Consumer<T> actionToTake)
Execute a
Consumer
that takes an object of type T, when all previous {#link ensure()} conditions are met.
-
isValid
public boolean isValid()
isValid.
- Returns:
- a boolean.
-
errorTemplate
public Fallible<T> errorTemplate(ErrorTemplate<T> template)
errorTemplate.
- Parameters:
template
- aErrorTemplate
object.- Returns:
- a
Fallible
object.
-
-