Record Class ValueOrError<R,E>

java.lang.Object
java.lang.Record
com.amalgamasimulation.excel.ValueOrError<R,E>
Type Parameters:
R - the value type
E - the error type
Record Components:
value - the successful value, when present
error - the error, when present

public record ValueOrError<R,E>(Optional<R> value, Optional<E> error) extends Record
Represents either a successful value or an error.
Author:
Andrey Malykhanov
  • Constructor Details

    • ValueOrError

      public ValueOrError(Optional<R> value, Optional<E> error)
      Creates an instance of a ValueOrError record class.
      Parameters:
      value - the value for the value record component
      error - the value for the error record component
  • Method Details

    • success

      public static <R, E> ValueOrError<R,E> success(R value)
      Creates a successful result.
      Type Parameters:
      R - the value type
      E - the error type
      Parameters:
      value - the successful value
      Returns:
      a result containing the value
    • failure

      public static <R, E> ValueOrError<R,E> failure(E error)
      Creates a failed result.
      Type Parameters:
      R - the value type
      E - the error type
      Parameters:
      error - the error value
      Returns:
      a result containing the error
    • attempt

      public static <R> ValueOrError<R,Exception> attempt(Callable<R> supplier)
      Executes the supplied operation and captures either its result or the thrown exception.
      Type Parameters:
      R - the result type
      Parameters:
      supplier - the operation to execute
      Returns:
      a successful result when the call succeeds, otherwise a failed result with the thrown exception
    • wrap

      public static <T, R> Function<T,ValueOrError<R,Exception>> wrap(Function<T,R> fn)
      Wraps a function so that its execution result is captured as either a value or an exception.
      Type Parameters:
      T - the function input type
      R - the function result type
      Parameters:
      fn - the function to wrap
      Returns:
      a function that returns a successful result or captures a thrown exception
    • peekError

      public Optional<R> peekError(Consumer<E> onError)
      Invokes the given consumer when an error is present and returns the value optional.
      Parameters:
      onError - the consumer to invoke with the error
      Returns:
      the value optional
    • attemptMap

      public <T> ValueOrError<T,Exception> attemptMap(ValueOrError.ThrowingFunction<? super R,? extends T,? extends Exception> transform)
      Transforms the current value with a function that may throw, capturing any thrown exception.
      Type Parameters:
      T - the mapped value type
      Parameters:
      transform - the transformation to apply when a value is present
      Returns:
      a successful mapped result, a failed result containing the thrown exception, or a failed result with an IllegalStateException when no value is present
    • map

      public <T> ValueOrError<T,E> map(Function<? super R,? extends T> fun)
      Transforms the current value when present, preserving the existing error otherwise.
      Type Parameters:
      T - the mapped value type
      Parameters:
      fun - the mapping function to apply
      Returns:
      a successful mapped result when a value is present, otherwise a failed result with the existing error
    • thenMap

      public <T> ValueOrError<T,E> thenMap(Function<? super R,ValueOrError<T,E>> fun)
      Chains another computation that already returns a ValueOrError, preserving the existing error otherwise.
      Type Parameters:
      T - the mapped value type
      Parameters:
      fun - the mapping function to apply
      Returns:
      the chained result when a value is present, otherwise a failed result with the existing error
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • value

      public Optional<R> value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component
    • error

      public Optional<E> error()
      Returns the value of the error record component.
      Returns:
      the value of the error record component