Class Ok<T,E>
- Type Parameters:
T- The type of the value wrapped by this class.E- The type of anErrthat 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 theErrvalue 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.booleanisErr()Indicates whether the wrapped value is anErr.booleanisOk()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.voidApply a lambda with no return type to the value of a result.<U> UApply a transformation to the value of a result.ok()Get theOkvalue 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:ResultIndicates whether the wrapped value is anOk. -
isErr
public boolean isErr()Description copied from interface:ResultIndicates whether the wrapped value is anErr. -
ok
Description copied from interface:ResultGet theOkvalue if it exists. -
err
Description copied from interface:ResultGet theErrvalue if it exists. -
unwrap
Description copied from interface:Result -
unwrapErr
Description copied from interface:ResultRetrieve theErrvalue or throw anUnwrapExceptionif 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:ResultReturn the value ifOk, otherwise throw an exception with a custom error message. -
expect
Description copied from interface:ResultReturns the value or throws a caller-defined exception. -
expectErr
Description copied from interface:ResultReturn the value ifErr, otherwise throw an exception with a custom error message.- Specified by:
expectErrin interfaceResult<T,E> - Parameters:
errorMessage- The message to accompany theUnwrapExceptionif thrown.- Returns:
- The value, if
Err. - Throws:
UnwrapException- If the result isOk.
-
expectErr
Description copied from interface:ResultReturns the value or throws a caller-defined exception. -
match
Description copied from interface:ResultApply a transformation to the value of a result.In the example below, the
parseArgsfunction 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 theOkandErrvalues must be mapped to a single output type.- Specified by:
matchin 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
Uafter applying one of the two provided transformations.
-
match
Description copied from interface:ResultApply 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 theparseArgsfunction.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:ResultApply 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:ResultApply 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:ResultReturns the provided argument if the result isOkand propagates the originalErrotherwise.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:ResultApply 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
mapin 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);maphad been used instead, the chain would have looked like:Result<Result<Integer, NumberFormatException>, NoSuchElementException> result = getUserInput().map(parseInt); -
or
Description copied from interface:ResultReturns the provided argument if the result is anErrand propagates the originalOkvalue 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:ResultApply 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)) );
-