Package com.vaadin.data
Interface Result<R>
-
- Type Parameters:
R
- the result value type
- All Superinterfaces:
Serializable
public interface Result<R> extends 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)
anderror(String)
, denoting success and failure respectively.Unless otherwise specified,
Result
method arguments cannot be null.- Since:
- 8.0
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <R> Result<R>
error(String message)
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.Optional<String>
getMessage()
Returns an Optional of the result message, or an empty Optional if none.<X extends Throwable>
RgetOrThrow(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 Detail
-
ok
static <R> Result<R> ok(R value)
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
static <R> Result<R> error(String message)
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
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. 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
<S> Result<S> flatMap(SerializableFunction<R,Result<S>> mapper)
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
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.- Parameters:
ifOk
- the function to call if successifError
- the function to call if failure
-
ifOk
default void ifOk(SerializableConsumer<R> consumer)
Applies theconsumer
if result is not an error.- Parameters:
consumer
- consumer to apply in case it's not an error
-
ifError
default void ifError(SerializableConsumer<String> consumer)
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
Optional<String> 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 X extends Throwable
Return 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 errorX extends Throwable
-
-