Class LoadResult<R>
- Type Parameters:
R- the type of the yielded value
This container is immutable and stores either a success value or an error value. If an error, the error contexts
are provided through getErrorContexts().
Nullability
The success value may or may not be nullable. Callers are encouraged to choose type annotations appropriate to
the succces value's nullabiility. For example, LoadResult<@NonNull String> may not check nullness at
runtime, but it will provide type-level assistance (and interoperability with other JVM languages, like Kotlin).
Equality
This type implements equality based on the success value or error contexts. If the success value is equal to another,
or the error contexts are equal, this LoadResult is equal. If one LoadResult succeeded but the other
did not, they are not equal.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanstatic <R> @NonNull LoadResult<R> failure(@NonNull List<@NonNull ErrorContext> reasons) Creates a failed load result with the given error contextsstatic <R> @NonNull LoadResult<R> failure(@NonNull ErrorContext @NonNull ... reasons) Creates a failed load result with the given error contexts<R_NEW> @NonNull LoadResult<R_NEW> flatMap(Function<? super R, ? extends LoadResult<R_NEW>> mapper) Maps from one load result to another.@NonNull List<@NonNull ErrorContext> Gets the error contexts.Unwraps the success value or throws an exception.@Nullable RgetValue()Tries to get at the success value.inthashCode()voidRuns an action on the success value.booleanChecks if failed.booleanChecks if succeeded.<R_NEW> @NonNull LoadResult<R_NEW> Maps from one load result to another.static <R> @NonNull LoadResult<R> of(R success) Makes a successful load resulttoString()
-
Method Details
-
of
Makes a successful load result- Type Parameters:
R- the yield type- Parameters:
success- the success value- Returns:
- the load result
-
failure
Creates a failed load result with the given error contexts- Type Parameters:
R- the yield type, which can be chosen freely since the result is an error- Parameters:
reasons- the error contexts- Returns:
- the load result
-
failure
Creates a failed load result with the given error contexts- Type Parameters:
R- the yield type, which can be chosen freely since the result is an error- Parameters:
reasons- the error contexts- Returns:
- the load result
-
isSuccess
public boolean isSuccess()Checks if succeeded. If true, a success value will be present- Returns:
- true if successful
-
isFailure
public boolean isFailure()Checks if failed. Opposite ofisSuccess()- Returns:
- true if failed and an error is present
-
getValue
Tries to get at the success value.This might be null either if the result is a failure, or the success value itself is null.
- Returns:
- the success value, or null if failed
-
getErrorContexts
Gets the error contexts. Fails if this load result is not an error.- Returns:
- the error contexts, immutable
-
map
Maps from one load result to another. If this load result is in error, nothing happens.- Type Parameters:
R_NEW- the type of the new returnable value- Parameters:
mapper- the mapping function- Returns:
- the new result
-
flatMap
public <R_NEW> @NonNull LoadResult<R_NEW> flatMap(Function<? super R, ? extends LoadResult<R_NEW>> mapper) Maps from one load result to another. If this load result is in error, nothing happens.- Type Parameters:
R_NEW- the type of the new returnable value- Parameters:
mapper- the mapping function- Returns:
- the new result
-
ifSuccess
Runs an action on the success value. If this load result is in error, nothing happens.- Parameters:
action- the action
-
getOrThrow
Unwraps the success value or throws an exception.If failed, the exception message will include some details about the errors. The format of this message is not specified, nor is the level of verbosity it provides.
- Returns:
- the success value
- Throws:
NoSuchElementException- if the success value is not present
-
equals
-
hashCode
public int hashCode() -
toString
-