S - the type of the success valueF - the type of the failure valuepublic interface Result<S,F>
S, indicating that the operation has
completed as intended.
F, indicating that the operation did not
complete as intended.
The exact definitions of success and failure depend on the semantics of the operation.
Introduction,
Getting StartedResult is primarily, but not only, intended for use as a method return type to handle anticipated
failures without throwing exceptions or returning null.Result should be avoided.| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object obj)
Indicates whether some other object is "equal to" this
Result. |
Result<S,F> |
filter(Predicate<? super S> isAcceptable,
Function<? super S,? extends F> mapper)
Transforms this successful
Result into 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 this
Result into a different one. |
<F2> Result<S,F2> |
flatMapFailure(Function<? super F,? extends Result<? extends S,? extends F2>> mapper)
Transforms this failed
Result into a different one. |
<S2> Result<S2,F> |
flatMapSuccess(Function<? super S,? extends Result<? extends S2,? extends F>> mapper)
Transforms this successful
Result into a different one. |
Optional<F> |
getFailure()
Returns this
Result's failure value as a possibly-empty Optional. |
Optional<S> |
getSuccess()
Returns this
Result's success value as a possibly-empty Optional. |
boolean |
hasFailure()
Checks if this
Result is failed. |
int |
hashCode()
Returns the hash code of this
Result's value. |
boolean |
hasSuccess()
Checks if this
Result is successful. |
Result<S,F> |
ifFailure(Consumer<? super F> action)
Performs the given action with this
Result's failure value. |
Result<S,F> |
ifSuccess(Consumer<? super S> action)
Performs the given action with this
Result's success value. |
Result<S,F> |
ifSuccessOrElse(Consumer<? super S> successAction,
Consumer<? super F> failureAction)
Performs either of the given actions with this
Result's value. |
<S2,F2> Result<S2,F2> |
map(Function<? super S,? extends S2> successMapper,
Function<? super F,? extends F2> failureMapper)
Transforms this
Result's success or failure value. |
<F2> Result<S,F2> |
mapFailure(Function<? super F,? extends F2> mapper)
Transforms this
Result's failure value. |
<S2> Result<S2,F> |
mapSuccess(Function<? super S,? extends S2> mapper)
Transforms this
Result's success value. |
S |
orElse(S other)
Returns this
Result's success value, or the alternative one. |
S |
orElseMap(Function<? super F,? extends S> mapper)
Returns this
Result's success value, or maps its failure value. |
Result<S,F> |
recover(Predicate<? super F> isRecoverable,
Function<? super F,? extends S> mapper)
Transforms this failed
Result into a successful one, based on the given condition. |
Stream<F> |
streamFailure()
Returns this
Result's failure value as a possibly-empty Stream. |
Stream<S> |
streamSuccess()
Returns this
Result's success value as a possibly-empty Stream. |
String |
toString()
Returns a string representation of this
Result. |
boolean hasSuccess()
Result is successful.
Result<Integer, String> r = getResult();
boolean x = r.hasSuccess();
Result is successful, true; otherwise, falsehasFailure()boolean hasFailure()
Result is failed.
Result<Integer, String> r = getResult();
boolean x = r.hasFailure();
Result is failed, true; otherwise, falsehasSuccess()Optional<S> getSuccess()
Result's success value as a possibly-empty Optional.
Result<Integer, String> r = getResult();
Optional<Integer> x = r.getSuccess();
Result is successful, an Optional containing its value; otherwise, an empty
OptionalgetFailure()Optional<F> getFailure()
Result's failure value as a possibly-empty Optional.
Result<Integer, String> r = getResult();
Optional<String> x = r.getFailure();
Result is failed, an Optional containing its value; otherwise, an empty
OptionalgetSuccess()S orElse(S other)
Result's success value, or the alternative one.
Result<Integer, String> r = getResult();
int x = r.orElse(8);
other - the alternative success value; may be nullResult is successful, its value; otherwise otherorElseMap(Function)S orElseMap(Function<? super F,? extends S> mapper)
Result's success value, or maps its failure value.
If this Result is failed, mapper will be applied to its value to produce an alternative success
value.
Result<Integer, String> r = getResult();
int x = r.orElseMap(f -> 8);
mapper - the mapping Function that produces the alternative success value; may return nullResult is successful, its value; otherwise the value produced by mapperNullPointerException - if this Result is failed and mapper is nullorElse(Object)Stream<S> streamSuccess()
Result's success value as a possibly-empty Stream.
Result<Integer, String> r = getResult();
Stream<Integer> x = r.streamSuccess();
Result is successful, a sequential Stream containing only its value; otherwise an
empty StreamStream<F> streamFailure()
Result's failure value as a possibly-empty Stream.
Result<Integer, String> r = getResult();
Stream<String> x = r.streamFailure();
Result is failed, a sequential Stream containing only its value; otherwise an
empty StreamResult<S,F> ifSuccess(Consumer<? super S> action)
Result's success value.
If this Result is successful, performs action with its value; otherwise does nothing.
Result<Integer, String> r = getResult();
Result<Integer, String> x = r.ifSuccess(System.out::println);
action - the Consumer to be applied to this Result's success valueResultNullPointerException - if this Result is successful and action is nullifFailure(Consumer),
ifSuccessOrElse(Consumer, Consumer)Result<S,F> ifFailure(Consumer<? super F> action)
Result's failure value.
If this Result is failed, performs action with its value; otherwise does nothing.
Result<Integer, String> r = getResult();
Result<Integer, String> x = r.ifFailure(System.err::println);
action - the Consumer to be applied to this Result's failure valueResultNullPointerException - if this Result is failed and action is nullifSuccess(Consumer),
ifSuccessOrElse(Consumer, Consumer)Result<S,F> ifSuccessOrElse(Consumer<? super S> successAction, Consumer<? super F> failureAction)
Result's value.
If this Result is successful, performs successAction; otherwise performs failureAction.
Result<Integer, String> r = getResult();
Result<Integer, String> x = r.ifSuccessOrElse(System.out::println, System.err::println);
successAction - the Consumer to be applied to this Result's success valuefailureAction - the Consumer to be applied to this Result's failure valueResultNullPointerException - if this Result is successful and successAction is null; or
if it is failed and failureAction is nullifFailure(Consumer),
ifSuccess(Consumer)Result<S,F> filter(Predicate<? super S> isAcceptable, Function<? super S,? extends F> mapper)
Result into a failed one, based on the given condition.
If this is a successful Result whose value does not satisfy isAcceptable, mapper will 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");
isAcceptable - the Predicate to apply to this Result's success valuemapper - the mapping Function that produces the failure valueResult whose value is deemed not acceptable, a new failed Result
holding the value produced by mapper; otherwise, this ResultNullPointerException - if this Result is successful and isAcceptable is null; or if
its success value is not acceptable and mapper is null or returns nullrecover(Predicate, Function)Result<S,F> recover(Predicate<? super F> isRecoverable, Function<? super F,? extends S> mapper)
Result into a successful one, based on the given condition.
If this is a failed Result whose value satisfies isRecoverable, mapper will 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);
isRecoverable - the Predicate to apply to this Result's failure valuemapper - the mapping Function that produces the success valueResult whose value is deemed recoverable, a new successful Result
holding the value produced by mapper; otherwise, this ResultNullPointerException - if this Result is failed and isRecoverable is null; or if
its failure value is recoverable and mapper is null or returns nullfilter(Predicate, Function)<S2> Result<S2,F> mapSuccess(Function<? super S,? extends S2> mapper)
Result's success value.
If this Result is successful, mapper will 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);
S2 - the type of the value returned by mappermapper - the mapping Function that produces the new success valueResult, a new successful Result holding the value produced by
mapper; otherwise, this ResultNullPointerException - if this Result is successful and mapper is null or returns nullmap(Function, Function),
mapFailure(Function)<F2> Result<S,F2> mapFailure(Function<? super F,? extends F2> mapper)
Result's failure value.
If this Result is failed, mapper will 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);
F2 - the type of the value returned by mappermapper - the mapping Function that produces the new failure valueResult, a new failed Result holding the value produced by
mapper; otherwise, this ResultNullPointerException - if this Result is failed and mapper is null or returns nullmap(Function, Function),
mapSuccess(Function)<S2,F2> Result<S2,F2> map(Function<? super S,? extends S2> successMapper, Function<? super F,? extends F2> failureMapper)
Result's success or failure value.
If this is Result is successful, successMapper will be applied to its value to produce a new one.
Otherwise, failureMapper will 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);
S2 - the type of the value returned by successMapperF2 - the type of the value returned by failureMappersuccessMapper - the mapping Function that produces the new success valuefailureMapper - the mapping Function that produces the new failure valueResult, a new successful Result holding the value produced by
successMapper; otherwise, a new failed Result holding the value produced by
failureMapperNullPointerException - if this result is successful and successMapper is null or returns null; or if this Result is failed and failureMapper is null or returns nullmapFailure(Function),
mapSuccess(Function)<S2> Result<S2,F> flatMapSuccess(Function<? super S,? extends Result<? extends S2,? extends F>> mapper)
Result into a different one.
If this Result is successful, mapper will be applied to its value to produce a new
Result, 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"));
S2 - the success type of the Result returned by mappermapper - the mapping Function that produces a new ResultResult is successful, a new Result produced by mapper; otherwise, this
ResultNullPointerException - if this Result is successful and mapper is null or returns nullflatMap(Function, Function),
flatMapFailure(Function)<F2> Result<S,F2> flatMapFailure(Function<? super F,? extends Result<? extends S,? extends F2>> mapper)
Result into a different one.
If this Result is failed, mapper will be applied to its value to produce a new Result,
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));
F2 - the failure type of the Result returned by mappermapper - the mapping Function that produces a new ResultResult is failed, a new Result produced by mapper; otherwise, this
ResultNullPointerException - if this Result is failed and mapper is null or returns nullflatMap(Function, Function),
flatMapSuccess(Function)<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)
Result into a different one.
If this Result is successful, successMapper will be applied to its value to produce a new
Result; otherwise, failureMapper will be applied to its value to produce a new Result.
The new Result may 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));
S2 - the success type of the Result returned by successMapper and failureMapperF2 - the failure type of the Result returned by successMapper and failureMappersuccessMapper - the mapping Function that produces a new Result if this Result is
successfulfailureMapper - the mapping Function that produces a new Result if this Result is
failedResult produced by either successMapper or failureMapperNullPointerException - if this Result is successful and successMapper is null or
returns null; or if this Result is failed and failureMapper is null or returns nullflatMapFailure(Function),
flatMapSuccess(Function)boolean equals(Object obj)
Result.
The other object is considered equal if:
Result and;
equals().
equals in class Objectobj - the object to be tested for equalitytrue if the other object is "equal to" this object; otherwise falsehashCode()int hashCode()
Result's value.String toString()
Result.
The exact presentation format is unspecified and may vary between implementations and versions.