Package com.amalgamasimulation.excel
Record Class ValueOrError<R,E>
java.lang.Object
java.lang.Record
com.amalgamasimulation.excel.ValueOrError<R,E>
- Type Parameters:
R- the value typeE- the error type- Record Components:
value- the successful value, when presenterror- the error, when present
Represents either a successful value or an error.
- Author:
- Andrey Malykhanov
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceValueOrError.ThrowingFunction<T,R, X extends Exception> A function that accepts one argument and may throw a checked exception. -
Constructor Summary
ConstructorsConstructorDescriptionValueOrError(Optional<R> value, Optional<E> error) Creates an instance of aValueOrErrorrecord class. -
Method Summary
Modifier and TypeMethodDescriptionstatic <R> ValueOrError<R, Exception> Executes the supplied operation and captures either its result or the thrown exception.<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.final booleanIndicates whether some other object is "equal to" this one.error()Returns the value of theerrorrecord component.static <R,E> ValueOrError <R, E> failure(E error) Creates a failed result.final inthashCode()Returns a hash code value for this object.<T> ValueOrError<T, E> Transforms the current value when present, preserving the existing error otherwise.Invokes the given consumer when an error is present and returns the value optional.static <R,E> ValueOrError <R, E> success(R value) Creates a successful result.<T> ValueOrError<T, E> thenMap(Function<? super R, ValueOrError<T, E>> fun) Chains another computation that already returns aValueOrError, preserving the existing error otherwise.final StringtoString()Returns a string representation of this record class.value()Returns the value of thevaluerecord component.static <T,R> Function <T, ValueOrError<R, Exception>> Wraps a function so that its execution result is captured as either a value or an exception.
-
Constructor Details
-
Method Details
-
success
Creates a successful result.- Type Parameters:
R- the value typeE- the error type- Parameters:
value- the successful value- Returns:
- a result containing the value
-
failure
Creates a failed result.- Type Parameters:
R- the value typeE- the error type- Parameters:
error- the error value- Returns:
- a result containing the error
-
attempt
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
Wraps a function so that its execution result is captured as either a value or an exception.- Type Parameters:
T- the function input typeR- the function result type- Parameters:
fn- the function to wrap- Returns:
- a function that returns a successful result or captures a thrown exception
-
peekError
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
IllegalStateExceptionwhen no value is present
-
map
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
Chains another computation that already returns aValueOrError, 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
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. -
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. -
equals
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 withObjects::equals(Object,Object). -
value
Returns the value of thevaluerecord component.- Returns:
- the value of the
valuerecord component
-
error
Returns the value of theerrorrecord component.- Returns:
- the value of the
errorrecord component
-