Class LoadResult<R>

java.lang.Object
space.arim.dazzleconf.LoadResult<R>
Type Parameters:
R - the type of the yielded value

public final class LoadResult<R> extends Object
A result container for fallible operations.

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 Details

    • of

      public static <R> @NonNull LoadResult<R> of(R success)
      Makes a successful load result
      Type Parameters:
      R - the yield type
      Parameters:
      success - the success value
      Returns:
      the load result
    • failure

      public static <R> @NonNull LoadResult<R> failure(@NonNull ErrorContext @NonNull ... reasons)
      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

      public static <R> @NonNull LoadResult<R> failure(@NonNull List<@NonNull ErrorContext> reasons)
      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 of isSuccess()
      Returns:
      true if failed and an error is present
    • getValue

      public @Nullable R 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

      public @NonNull List<@NonNull ErrorContext> getErrorContexts()
      Gets the error contexts. Fails if this load result is not an error.
      Returns:
      the error contexts, immutable
    • map

      public <R_NEW> @NonNull LoadResult<R_NEW> map(Function<? super R,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
    • 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

      public void ifSuccess(Consumer<? super R> action)
      Runs an action on the success value. If this load result is in error, nothing happens.
      Parameters:
      action - the action
    • getOrThrow

      public R 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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object