Interface Result<S,F>
- Type Parameters:
S- the type of the success valueF- the type of the failure value
- Successful results hold a value of type
S, indicating that the operation has completed as intended. - Failed results hold a value of type
F, indicating that the operation did not complete as intended.
The exact definitions of success and failure depend on the semantics of the operation.
- Author:
- Guillermo Calvo
- See Also:
- API Note:
Resultis primarily, but not only, intended for use as a method return type to handle anticipated failures without throwing exceptions or returningnull.- Impl Spec:
- This is a
value-based type; use of identity-sensitive operations on instances of
Resultshould be avoided.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanIndicates whether some other object is "equal to" thisResult.Transforms this successfulResultinto a failed one, based on the given condition.<S2,F2> Result <S2, F2> flatMap(Function<? super S, ? extends Result<? extends S2, ? extends F2>> successMapper, Function<? super F, ? extends Result<? extends S2, ? extends F2>> failureMapper) Transforms thisResultinto a different one.flatMapFailure(Function<? super F, ? extends Result<? extends S, ? extends F2>> mapper) Transforms this failedResultinto a different one.flatMapSuccess(Function<? super S, ? extends Result<? extends S2, ? extends F>> mapper) Transforms this successfulResultinto a different one.Returns thisResult's failure value as a possibly-emptyOptional.Returns thisResult's success value as a possibly-emptyOptional.booleanChecks if thisResultis failed.inthashCode()Returns the hash code of thisResult's value.booleanChecks if thisResultis successful.Performs the given action with thisResult's failure value.Performs the given action with thisResult's success value.ifSuccessOrElse(Consumer<? super S> successAction, Consumer<? super F> failureAction) Performs either of the given actions with thisResult's value.<S2,F2> Result <S2, F2> Transforms thisResult's success or failure value.mapFailure(Function<? super F, ? extends F2> mapper) Transforms thisResult's failure value.mapSuccess(Function<? super S, ? extends S2> mapper) Transforms thisResult's success value.Returns thisResult's success value, or the alternative one.Returns thisResult's success value, or maps its failure value.Transforms this failedResultinto a successful one, based on the given condition.Returns thisResult's failure value as a possibly-emptyStream.Returns thisResult's success value as a possibly-emptyStream.toString()Returns a string representation of thisResult.
-
Method Details
-
hasSuccess
boolean hasSuccess()Checks if thisResultis successful.Result<Integer, String> r = getResult(); boolean x = r.hasSuccess();- Returns:
- if this
Resultis successful,true; otherwise,false - See Also:
-
hasFailure
boolean hasFailure()Checks if thisResultis failed.Result<Integer, String> r = getResult(); boolean x = r.hasFailure();- Returns:
- if this
Resultis failed,true; otherwise,false - See Also:
-
getSuccess
Returns thisResult's success value as a possibly-emptyOptional.Result<Integer, String> r = getResult(); Optional<Integer> x = r.getSuccess();- Returns:
- if this
Resultis successful, anOptionalcontaining its value; otherwise, an emptyOptional - See Also:
-
getFailure
Returns thisResult's failure value as a possibly-emptyOptional.Result<Integer, String> r = getResult(); Optional<String> x = r.getFailure();- Returns:
- if this
Resultis failed, anOptionalcontaining its value; otherwise, an emptyOptional - See Also:
-
orElse
Returns thisResult's success value, or the alternative one.Result<Integer, String> r = getResult(); int x = r.orElse(8);- Parameters:
other- the alternative success value; may benull- Returns:
- if this
Resultis successful, its value; otherwiseother - See Also:
-
orElseMap
Returns thisResult's success value, or maps its failure value.If this
Resultis failed,mapperwill be applied to its value to produce an alternative success value.Result<Integer, String> r = getResult(); int x = r.orElseMap(f -> 8);- Parameters:
mapper- the mappingFunctionthat produces the alternative success value; may returnnull- Returns:
- if this
Resultis successful, its value; otherwise the value produced bymapper - Throws:
NullPointerException- if thisResultis failed andmapperisnull- See Also:
-
streamSuccess
Returns thisResult's success value as a possibly-emptyStream.Result<Integer, String> r = getResult(); Stream<Integer> x = r.streamSuccess();- Returns:
- If this
Resultis successful, a sequentialStreamcontaining only its value; otherwise an emptyStream
-
streamFailure
Returns thisResult's failure value as a possibly-emptyStream.Result<Integer, String> r = getResult(); Stream<String> x = r.streamFailure();- Returns:
- if this
Resultis failed, a sequentialStreamcontaining only its value; otherwise an emptyStream
-
ifSuccess
Performs the given action with thisResult's success value.If this
Resultis successful, performsactionwith its value; otherwise does nothing.Result<Integer, String> r = getResult(); Result<Integer, String> x = r.ifSuccess(System.out::println);- Parameters:
action- theConsumerto be applied to thisResult's success value- Returns:
- this
Result - Throws:
NullPointerException- if thisResultis successful andactionisnull- See Also:
-
ifFailure
Performs the given action with thisResult's failure value.If this
Resultis failed, performsactionwith its value; otherwise does nothing.Result<Integer, String> r = getResult(); Result<Integer, String> x = r.ifFailure(System.err::println);- Parameters:
action- theConsumerto be applied to thisResult's failure value- Returns:
- this
Result - Throws:
NullPointerException- if thisResultis failed andactionisnull- See Also:
-
ifSuccessOrElse
Performs either of the given actions with thisResult's value.If this
Resultis successful, performssuccessAction; otherwise performsfailureAction.Result<Integer, String> r = getResult(); Result<Integer, String> x = r.ifSuccessOrElse(System.out::println, System.err::println);- Parameters:
successAction- theConsumerto be applied to thisResult's success valuefailureAction- theConsumerto be applied to thisResult's failure value- Returns:
- this
Result - Throws:
NullPointerException- if thisResultis successful andsuccessActionisnull; or if it is failed andfailureActionisnull- See Also:
-
filter
Transforms this successfulResultinto a failed one, based on the given condition.If this is a successful
Resultwhose value does not satisfyisAcceptable,mapperwill be applied to the value to produce a failure value.Result<Integer, String> r = getResult(); Result<Integer, String> x = r.filter(s -> s < 3, s -> "E");- Parameters:
isAcceptable- thePredicateto apply to thisResult's success valuemapper- the mappingFunctionthat produces the failure value- Returns:
- if this is a successful
Resultwhose value is deemed not acceptable, a new failedResultholding the value produced bymapper; otherwise, thisResult - Throws:
NullPointerException- if thisResultis successful andisAcceptableisnull; or if its success value is not acceptable andmapperisnullor returnsnull- See Also:
-
recover
Transforms this failedResultinto a successful one, based on the given condition.If this is a failed
Resultwhose value satisfiesisRecoverable,mapperwill be applied to the value to produce a success value.Result<Integer, String> r = getResult(); Result<Integer, String> x = r.recover("B"::equals, f -> 5);- Parameters:
isRecoverable- thePredicateto apply to thisResult's failure valuemapper- the mappingFunctionthat produces the success value- Returns:
- if this is a failed
Resultwhose value is deemed recoverable, a new successfulResultholding the value produced bymapper; otherwise, thisResult - Throws:
NullPointerException- if thisResultis failed andisRecoverableisnull; or if its failure value is recoverable andmapperisnullor returnsnull- See Also:
-
mapSuccess
Transforms thisResult's success value.If this
Resultis successful,mapperwill be applied to its value to produce a new one, which may differ in type.Result<Integer, String> r = getResult(); Result<Fruit, String> x = r.mapSuccess(s -> CHERRIES);- Type Parameters:
S2- the type of the value returned bymapper- Parameters:
mapper- the mappingFunctionthat produces the new success value- Returns:
- if this is a successful
Result, a new successfulResultholding the value produced bymapper; otherwise, thisResult - Throws:
NullPointerException- if thisResultis successful andmapperisnullor returnsnull- See Also:
-
mapFailure
Transforms thisResult's failure value.If this
Resultis failed,mapperwill be applied to its value to produce a new one, which may differ in type.Result<Integer, String> r = getResult(); Result<Integer, Suit> x = r.mapFailure(f -> CLUBS);- Type Parameters:
F2- the type of the value returned bymapper- Parameters:
mapper- the mappingFunctionthat produces the new failure value- Returns:
- if this is a failed
Result, a new failedResultholding the value produced bymapper; otherwise, thisResult - Throws:
NullPointerException- if thisResultis failed andmapperisnullor returnsnull- See Also:
-
map
<S2,F2> Result<S2,F2> map(Function<? super S, ? extends S2> successMapper, Function<? super F, ? extends F2> failureMapper) Transforms thisResult's success or failure value.If this is
Resultis successful,successMapperwill be applied to its value to produce a new one. Otherwise,failureMapperwill be applied to its failure value to produce a new one.Both success and failure values may differ in type.
Result<Integer, String> r = getResult(); Result<Fruit, Suit> x = r.map(s -> CHERRIES, f -> CLUBS);- Type Parameters:
S2- the type of the value returned bysuccessMapperF2- the type of the value returned byfailureMapper- Parameters:
successMapper- the mappingFunctionthat produces the new success valuefailureMapper- the mappingFunctionthat produces the new failure value- Returns:
- if this is a successful
Result, a new successfulResultholding the value produced bysuccessMapper; otherwise, a new failedResultholding the value produced byfailureMapper - Throws:
NullPointerException- if this result is successful andsuccessMapperisnullor returnsnull; or if thisResultis failed andfailureMapperisnullor returnsnull- See Also:
-
flatMapSuccess
<S2> Result<S2,F> flatMapSuccess(Function<? super S, ? extends Result<? extends S2, ? extends F>> mapper) Transforms this successfulResultinto a different one.If this
Resultis successful,mapperwill be applied to its value to produce a newResult, which may now hold either a success or a failure value. New success value may differ in type.Result<Integer, String> r = getResult(); Result<Fruit, String> x = r.flatMapSuccess(s -> s < 3 ? success(CHERRIES) : failure("E"));- Type Parameters:
S2- the success type of theResultreturned bymapper- Parameters:
mapper- the mappingFunctionthat produces a newResult- Returns:
- if this
Resultis successful, a newResultproduced bymapper; otherwise, thisResult - Throws:
NullPointerException- if thisResultis successful andmapperisnullor returnsnull- See Also:
-
flatMapFailure
<F2> Result<S,F2> flatMapFailure(Function<? super F, ? extends Result<? extends S, ? extends F2>> mapper) Transforms this failedResultinto a different one.If this
Resultis failed,mapperwill be applied to its value to produce a newResult, which may now hold either a success or a failure value. New failure value may differ in type.Result<Integer, String> r = getResult(); Result<Integer, Suit> x = r.flatMapFailure(f -> f.equals("B") ? success(5) : failure(CLUBS));- Type Parameters:
F2- the failure type of theResultreturned bymapper- Parameters:
mapper- the mappingFunctionthat produces a newResult- Returns:
- if this
Resultis failed, a newResultproduced bymapper; otherwise, thisResult - Throws:
NullPointerException- if thisResultis failed andmapperisnullor returnsnull- See Also:
-
flatMap
<S2,F2> Result<S2,F2> flatMap(Function<? super S, ? extends Result<? extends S2, ? extends F2>> successMapper, Function<? super F, ? extends Result<? extends S2, ? extends F2>> failureMapper) Transforms thisResultinto a different one.If this
Resultis successful,successMapperwill be applied to its value to produce a newResult; otherwise,failureMapperwill be applied to its value to produce a newResult.The new
Resultmay now hold either a success or a failure value. Both may differ in type.Result<Integer, String> r = getResult(); Result<Fruit, Suit> x = r.flatMap(s -> s < 3 ? success(CHERRIES) : failure(SPADES), f -> f.equals("B") ? success(WATERMELON) : failure(CLUBS));- Type Parameters:
S2- the success type of theResultreturned bysuccessMapperandfailureMapperF2- the failure type of theResultreturned bysuccessMapperandfailureMapper- Parameters:
successMapper- the mappingFunctionthat produces a newResultif thisResultis successfulfailureMapper- the mappingFunctionthat produces a newResultif thisResultis failed- Returns:
- the
Resultproduced by eithersuccessMapperorfailureMapper - Throws:
NullPointerException- if thisResultis successful andsuccessMapperisnullor returnsnull; or if thisResultis failed andfailureMapperisnullor returnsnull- See Also:
-
equals
Indicates whether some other object is "equal to" thisResult.The other object is considered equal if:
- it is also a
Resultand; - both objects are instances of the same class and;
- their values are "equal to" each other via
equals().
- it is also a
-
hashCode
int hashCode()Returns the hash code of thisResult's value. -
toString
String toString()Returns a string representation of thisResult.The exact presentation format is unspecified and may vary between implementations and versions.
-