Package de.fxlae.typeid.util
Interface Validated<T>
- Type Parameters:
T- the type of the contained value.
- All Known Implementing Classes:
Validated.Invalid,Validated.Valid
A container for a value that is either valid or not.
The two states are represented by
Validated.Valid and Validated.Invalid respectively.
In this library, in is used to represent the result of parsing a TypeId string. However,
the data structure itself is completely independent of TypeIDs.-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescription<O> Validated<O>Applies the provided mapping function if the value is valid, otherwise returns the current instanceget()Returns the value if it's valid, otherwise throws.voidApplies the message consuming function if the value is invalid, otherwise does nothing.voidApplies the consuming function if the value is valid, otherwise does nothing.static <T> Validated<T>Returns a new invalidValidatedwith the given error message.booleanisValid()Returnstrueif the value is valid, otherwisefalse.<O> Validated<O>Applies the provided mapping function if the value is valid, otherwise returns the current instancemessage()Returns the message if it's invalid, otherwise throws.Returns the value if valid, otherwise returnother.static <T> Validated<T>valid(T value) Returns a new validValidatedwith the given value.
-
Method Details
-
valid
Returns a new validValidatedwith the given value.- Type Parameters:
T- the type of the value- Parameters:
value- the value to wrap- Returns:
- the valid
Validated - Throws:
NullPointerException- if the provided value is null
-
invalid
Returns a new invalidValidatedwith the given error message.- Type Parameters:
T- the type of the invalid value- Parameters:
message- the error message- Returns:
- the invalid
Validated - Throws:
NullPointerException- if the message is null
-
toOptional
Returns anOptionalwith the valid value, otherwise an emptyOptional. In the latter case, the error message is lost.- Returns:
- the
Optional
-
isValid
boolean isValid()Returnstrueif the value is valid, otherwisefalse.- Returns:
trueif the value is valid, otherwisefalse
-
get
T get()Returns the value if it's valid, otherwise throws.- Returns:
- the valid value
- Throws:
NoSuchElementException- if the value is invalid
-
orElse
Returns the value if valid, otherwise returnother.- Parameters:
other- the value to be returned if the value is invalid, can benull- Returns:
- the value if valid, else
other
-
message
String message()Returns the message if it's invalid, otherwise throws.- Returns:
- the message
- Throws:
NoSuchElementException- if the value is valid
-
map
Applies the provided mapping function if the value is valid, otherwise returns the current instance- Type Parameters:
O- The type of the mapping function's result- Parameters:
mapper- the mapping function- Returns:
- the result of applying the mapping function to the value of this
Validatedinstance - Throws:
NullPointerException- if the mapping function is null
-
flatMap
Applies the provided mapping function if the value is valid, otherwise returns the current instance- Type Parameters:
O- The type parameter of the mapping function's returnedValidated- Parameters:
mapper- the mapping function- Returns:
- the result of applying the mapping function to the value of this
Validatedinstance - Throws:
NullPointerException- if the mapping function is null
-
filter
If the value is valid and matches the predicate, return it as a validValidated, otherwise return an invalidValidatedwith the given error message. If the value is invalid in the first place, return the current invalidValidated- Parameters:
message- the message in case the predicate doesn't matchpredicate- the predicate that checks the value- Returns:
- the resulting
Validated - Throws:
NullPointerException- if the message and/or predicate is null
-
ifValid
Applies the consuming function if the value is valid, otherwise does nothing.- Parameters:
valueConsumer- the valueConsumer- Throws:
NullPointerException- if theConsumeris null
-
ifInvalid
Applies the message consuming function if the value is invalid, otherwise does nothing.- Parameters:
messageConsumer- the messageConsumer- Throws:
NullPointerException- if theConsumeris null
-