Interface Result<V,E>
- Type Parameters:
V- the type of the success valueE- the type of the error value
- All Known Implementing Classes:
Result.Err, Result.Ok
The result of an operation that can fail: a success value (Result.Ok) or an error value (Result.Err).
jsonlens reports errors with this type, not with exceptions. The parser, the evaluator, and
the registration of function extensions all return a Result.
Examine a result
Result is sealed, so a switch with record patterns must handle both cases. The compiler
tells you if a case is missing:
Result<Integer, Problem> port = parsePort("8080");
// Examine a result with a switch. The compiler checks that all cases are present.
String text = switch (port) {
case Result.Ok(Integer value) -> "port " + value;
case Result.Err(Problem problem) -> "no port: " + problem.reason();
};
// Or change it with map, flatMap, and fold.
Result<String, Problem> url = port.map(value -> "http://localhost:" + value);
int value = port.orElse(80);
Make a result
Use ok(Object) and err(Object). The error type is usually a sealed interface of records:
static Result<Integer, Problem> parsePort(String text) {
if (!text.matches("[0-9]{1,5}")) {
return Result.err(new Problem("not a number: " + text));
}
int port = Integer.parseInt(text);
return port <= 65_535 ? Result.ok(port) : Result.err(new Problem("too large: " + port));
}
Operations
| Method | On Result.Ok |
On Result.Err |
|---|---|---|
map(Function) |
applies the function to the value | no change |
mapErr(Function) |
no change | applies the function to the error |
flatMap(Function) |
applies an operation that can fail | no change |
fold(Function, Function) |
applies the first function | applies the second function |
orElse(Object) |
gives the value | gives the fallback value |
flatMap(Function) chains operations that can fail, for example a parse and an evaluation:
// Result.flatMap chains the parse and the evaluation. The error type is a String here.
Result<List<Node<JsonNode>>, String> authors = JsonPathParser.standard()
.parse("$..author")
.mapErr(ParseError::message)
.flatMap(query -> JsonPathEvaluator.standard()
.evaluate(query, root, JacksonJsonModel.INSTANCE)
.mapErr(EvaluationError::message));
int count = authors.fold(List::size, error -> 0); // 4
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <V,E> Result <V, E> err(E error) Makes an error result.Applies an operation that can fail to the success value.default <R> RChanges the success value or the error value into one value.default booleanisErr()Returns true for an error result.default booleanisOk()Returns true for a success result.Changes the success value.Changes the error value.static <V,E> Result <V, E> ok(V value) Makes a success result.default VReturns the success value, or a fallback value for an error result.
-
Method Details
-
ok
Makes a success result.- Type Parameters:
V- the type of the success valueE- the type of the error value- Parameters:
value- the value- Returns:
- a success result
-
err
Makes an error result.- Type Parameters:
V- the type of the success valueE- the type of the error value- Parameters:
error- the error- Returns:
- an error result
-
isOk
-
isErr
-
map
-
mapErr
-
flatMap
Applies an operation that can fail to the success value. An error result does not change.- Type Parameters:
U- the type of the success value of the operation- Parameters:
mapper- the operation for the success value- Returns:
- the result of the operation, or this error
-
fold
Changes the success value or the error value into one value.- Type Parameters:
R- the type of the value- Parameters:
onOk- the function for a success valueonErr- the function for an error value- Returns:
- the value from the applicable function
-
orElse
-