Package com.vaadin.flow.data.binder
Interface Result<R>
- Type Parameters:
R
- the result value type
- All Superinterfaces:
Serializable
Represents the result of an operation that might fail, such as type
conversion. A result may contain either a value, signifying a successful
operation, or an error message in case of a failure.
Result instances are created using the factory methods ok(Object)
and error(String)
, denoting success and failure respectively.
Unless otherwise specified, Result
method arguments cannot be null.
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <R> Result<R>
Returns a failure result wrapping the given error message.<S> Result<S>
flatMap
(SerializableFunction<R, Result<S>> mapper) If this Result has a value, applies the given Result-returning function to the value.Returns an Optional of the result message, or an empty Optional if none.getOrThrow
(SerializableFunction<String, ? extends X> exceptionProvider) Return the value, if the result denotes success, otherwise throw an exception to be created by the provided supplier.void
handle
(SerializableConsumer<R> ifOk, SerializableConsumer<String> ifError) Invokes either the first callback or the second one, depending on whether this Result denotes a success or a failure, respectively.default void
ifError
(SerializableConsumer<String> consumer) Applies theconsumer
if result is an error.default void
ifOk
(SerializableConsumer<R> consumer) Applies theconsumer
if result is not an error.boolean
isError()
Checks if the result denotes an error.default <S> Result<S>
map
(SerializableFunction<R, S> mapper) If this Result has a value, returns a Result of applying the given function to the value.static <R> Result<R>
of
(SerializableSupplier<R> supplier, SerializableFunction<Exception, String> onError) Returns a Result representing the result of invoking the given supplier.static <R> Result<R>
ok
(R value) Returns a successful result wrapping the given value.
-
Method Details
-
ok
Returns a successful result wrapping the given value.- Type Parameters:
R
- the result value type- Parameters:
value
- the result value, can be null- Returns:
- a successful result
-
error
Returns a failure result wrapping the given error message.- Type Parameters:
R
- the result value type- Parameters:
message
- the error message- Returns:
- a failure result
-
of
static <R> Result<R> of(SerializableSupplier<R> supplier, SerializableFunction<Exception, String> onError) Returns a Result representing the result of invoking the given supplier. If the supplier returns a value, returns aResult.ok
of the value; if an exception is thrown, returns the message in aResult.error
.- Type Parameters:
R
- the result value type- Parameters:
supplier
- the supplier to runonError
- the function to provide the error message- Returns:
- the result of invoking the supplier
-
map
If this Result has a value, returns a Result of applying the given function to the value. Otherwise, returns a Result bearing the same error as this one. Note that any exceptions thrown by the mapping function are not wrapped but allowed to propagate.- Type Parameters:
S
- the type of the mapped value- Parameters:
mapper
- the mapping function- Returns:
- the mapped result
-
flatMap
If this Result has a value, applies the given Result-returning function to the value. Otherwise, returns a Result bearing the same error as this one. Note that any exceptions thrown by the mapping function are not wrapped but allowed to propagate.- Type Parameters:
S
- the type of the mapped value- Parameters:
mapper
- the mapping function- Returns:
- the mapped result
-
handle
Invokes either the first callback or the second one, depending on whether this Result denotes a success or a failure, respectively.- Parameters:
ifOk
- the function to call if successifError
- the function to call if failure
-
ifOk
Applies theconsumer
if result is not an error.- Parameters:
consumer
- consumer to apply in case it's not an error
-
ifError
Applies theconsumer
if result is an error.- Parameters:
consumer
- consumer to apply in case it's an error
-
isError
boolean isError()Checks if the result denotes an error.- Returns:
true
if the result denotes an error,false
otherwise
-
getMessage
Returns an Optional of the result message, or an empty Optional if none.- Returns:
- the optional message
-
getOrThrow
<X extends Throwable> R getOrThrow(SerializableFunction<String, ? extends X> exceptionProvider) throws XReturn the value, if the result denotes success, otherwise throw an exception to be created by the provided supplier.- Type Parameters:
X
- Type of the exception to be thrown- Parameters:
exceptionProvider
- The provider which will return the exception to be thrown based on the given error message- Returns:
- the value
- Throws:
X
- if this result denotes an error
-