Class Ok<T,E>
- Type Parameters:
T
- The type of the value wrapped by this class.E
- The type of anErr
that could have (but in this case did not) resulted from the parent operation.
- All Implemented Interfaces:
Result<T,
E>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionApply a fallible operation to the wrapped value if the result isOk
.err()
Get theErr
value if it exists.Return the value ifOk
, otherwise throw an exception with a custom error message.Returns the value or throws a caller-defined exception.Return the value ifErr
, otherwise throw an exception with a custom error message.Returns the value or throws a caller-defined exception.boolean
isErr()
Indicates whether the wrapped value is anErr
.boolean
isOk()
Indicates whether the wrapped value is anOk
.Apply a transformation to the wrapped value if the result isOk
.Apply a transformation to the wrapped value if the result is anErr
.void
Apply a lambda with no return type to the value of a result.<U> U
Apply a transformation to the value of a result.ok()
Get theOk
value if it exists.Apply a fallible operation to the wrapped value if the result is anErr
.unwrap()
-
Constructor Details
-
Ok
-
-
Method Details
-
isOk
public boolean isOk()Description copied from interface:Result
Indicates whether the wrapped value is anOk
. -
isErr
public boolean isErr()Description copied from interface:Result
Indicates whether the wrapped value is anErr
. -
ok
Description copied from interface:Result
Get theOk
value if it exists. -
err
Description copied from interface:Result
Get theErr
value if it exists. -
unwrap
Description copied from interface:Result
-
unwrapErr
Description copied from interface:Result
Retrieve theErr
value or throw anUnwrapException
if the result isOk
. If the error is aThrowable
, it will not be thrown. To throw a custom error, useResult.expect(Function)
. -
expect
Description copied from interface:Result
Return the value ifOk
, otherwise throw an exception with a custom error message. -
expect
Description copied from interface:Result
Returns the value or throws a caller-defined exception. -
expectErr
Description copied from interface:Result
Return the value ifErr
, otherwise throw an exception with a custom error message.- Specified by:
expectErr
in interfaceResult<T,
E> - Parameters:
errorMessage
- The message to accompany theUnwrapException
if thrown.- Returns:
- The value, if
Err
. - Throws:
UnwrapException
- If the result isOk
.
-
expectErr
Description copied from interface:Result
Returns the value or throws a caller-defined exception. -
match
Description copied from interface:Result
Apply a transformation to the value of a result.In the example below, the
parseArgs
function attempts to convert a user-provided string to a list of integers, but failing that returns its input unmodified. The output of the function is then used to determine the number of valid user-provided arguments and do some additional logging for convenience.
Note that unlikeResult<List<Integer>, String> parsed = parseArgs(input); int validArgs = parsed.match( ok -> ok.size(), err -> { log.warn("The provided input could not be parsed: \"{}\"", err); return 0; } );
Result.map(Function)
, both theOk
andErr
values must be mapped to a single output type.- Specified by:
match
in interfaceResult<T,
E> - Type Parameters:
U
- the type of the value to be returned after the transformations are applied. Both transformations must converge to this type.- Parameters:
ifOk
- the transformation that is applied to the underlying data if the result isOk
.ifErr
- the transformation that is applied to the underlying data if the result is anErr
.- Returns:
- a value of type
U
after applying one of the two provided transformations.
-
match
Description copied from interface:Result
Apply a lambda with no return type to the value of a result.This is useful for things which produce side effects such as logging. In contrast to
Result.match(Function, Function)
, no output can be obtained from an invocation of this method. In the example below, some informational logging is performed on theparseArgs
function.Result<List<Integer>, String> parsed = parseArgs(input); parsed.match( ok -> log.info("args were parsed successfully: {}", ok), err -> log.warn("The provided input could not be parsed: \"{}\"", err) );
-
map
Description copied from interface:Result
Apply a transformation to the wrapped value if the result isOk
. If the result is anErr
, no transformation will be applied.
If an exception is thrown while applyingassertEquals( 6, Result.ok(5).map(x -> x + 1).unwrap() ); assertEquals( "error", Result.<Integer, String>err("error").map(x -> x + 1).unwrapErr() );
mapping
, the exception will not be caught. Ensure that any lambda passed to this function does not throw any runtime exceptions. -
mapErr
Description copied from interface:Result
Apply a transformation to the wrapped value if the result is anErr
. If the result isOk
, no transformation will be applied.
If an exception is thrown while applyingassertEquals( "error: 'foo'", Result.err("foo").mapErr(err -> String.format("error: '%s'", err)).unwrapErr() ); assertEquals( 5, Result.ok(5).mapErr(err -> String.format("error: '%s'", err)).unwrap() );
mapping
, the exception will not be caught. Ensure that any lambda passed to this function does not throw any runtime exceptions. -
and
Description copied from interface:Result
Returns the provided argument if the result isOk
and propagates the originalErr
otherwise.assertEquals( Result.err("first error"), Result.err("first error").and(Result.err("second error")) ); assertEquals( Result.err("second error"), Result.ok(1).and(Result.err("second error")) ); assertEquals( Result.ok(2), Result.ok("success").and(Result.ok(2)) );
-
andThen
Description copied from interface:Result
Apply a fallible operation to the wrapped value if the result isOk
. If the result is anErr
, the error is propagated to the next operation in the chain.This operation differs from
map
in that if the value yielded by the provided fallible operation is an error, it is returned without being wrapped in a redundant, enclosingResult
. Take, for example, two functions:getUserInput()
which returns aResult<String, RuntimeException>
andparseInt(String s)
which returns aResult<Integer, NumberFormatException>
. A chain of operations could be built up as follows:
IfResult<Integer, RuntimeException> result = getUserInput().andThen(parseInt);
map
had been used instead, the chain would have looked like:Result<Result<Integer, NumberFormatException>, NoSuchElementException> result = getUserInput().map(parseInt);
-
or
Description copied from interface:Result
Returns the provided argument if the result is anErr
and propagates the originalOk
value otherwise.assertEquals( Result.ok("success"), Result.err("error").or(Result.ok("success")) ); assertEquals( Result.ok("first"), Result.ok("first").or(Result.ok("second")) ); assertEquals( Result.err("second"), Result.err("first").or(Result.err("second")) );
-
orElse
Description copied from interface:Result
Apply a fallible operation to the wrapped value if the result is anErr
. If the result isOk
, the value is propagated to the next operation in the chain.assertEquals( Result.ok("recovered from err: foo"), Result.err("foo").orElse(err -> Result.ok("recovered from err: " + err)) ); assertEquals( Result.ok("no error"), Result.ok("no error").orElse(err -> Result.ok("recovered from err: " + err)) );
-