Record Class Result.Ok<Value,Error>
java.lang.Object
java.lang.Record
dmx.fun.Result.Ok<Value,Error>
- Type Parameters:
Value- the type of the value contained in the successful resultError- the type of the error contained in the erroneous result, unused here- Record Components:
value- the non-null value that represents the successful result; passingnullthrowsNullPointerException
- All Implemented Interfaces:
Result<Value,Error>
public static record Result.Ok<Value,Error> (Value value)
extends Record
implements Result<Value,Error>
Represents a successful result containing a value of type
Value.
This record implements the Result interface and indicates the "Ok" variant
of a result. It encapsulates a non-null value that represents a successful computation
or action. To model a successful result with no meaningful value use Result<Void, Error>;
for an optional value use Result<Option<Value>, Error>.
-
Nested Class Summary
Nested classes/interfaces inherited from interface Result
Result.Err<Value,Error>, Result.Ok<Value, Error>, Result.Partition<V, E> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.default <R> RFolds this container to a single value by applying the appropriate function.default ValueReturns the success value if present, orfallbackotherwise.default ValuegetOrElseGet(Supplier<Value> supplier) Returns the success value if present, or the value produced bysupplierotherwise.default ValueReturns the success value if present, ornullotherwise.default ValuegetOrThrow(Function<Error, ? extends RuntimeException> exMapper) Returns the success value if present, or throws an exception mapped from the error.final inthashCode()Returns a hash code value for this object.default voidExecutes one of the two consumers based on the active variant.stream()Returns a single-element stream of the success value, or an empty stream on error.toOption()Converts this container to anOption.final StringtoString()Returns a string representation of this record class.value()Returns the value of thevaluerecord component.
-
Constructor Details
-
Ok
Compact canonical constructor — validates thatvalueis non-null.- Throws:
NullPointerException- ifvalueisnull
-
-
Method Details
-
toString
-
hashCode
-
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
-
getOrElse
Returns the success value if present, orfallbackotherwise.- Parameters:
fallback- the non-null fallback value- Returns:
- the success value, or
fallbackif this is the error variant - Throws:
NullPointerException- iffallbackisnull
-
getOrElseGet
Returns the success value if present, or the value produced bysupplierotherwise.- Parameters:
supplier- supplier of the fallback value; must not returnnull- Returns:
- the success value, or the value produced by
supplier - Throws:
NullPointerException- ifsupplierisnullor returnsnull
-
getOrNull
Returns the success value if present, ornullotherwise.- Returns:
- the success value or
null
-
getOrThrow
Returns the success value if present, or throws an exception mapped from the error.- Parameters:
exMapper- maps the error to aRuntimeException; must not returnnull- Returns:
- the success value
- Throws:
NullPointerException- ifexMapperisnullor returnsnullRuntimeException- the exception produced byexMapperon the error variant
-
stream
-
fold
Folds this container to a single value by applying the appropriate function.- Type Parameters:
R- the result type- Parameters:
onSuccess- applied to the success value; must not returnnullonError- applied to the error value; must not returnnull- Returns:
- the result of the applied function
- Throws:
NullPointerException- if either function isnullor returnsnull
-
match
Executes one of the two consumers based on the active variant.- Parameters:
onSuccess- called with the success valueonError- called with the error value- Throws:
NullPointerException- if either consumer isnull
-
toOption
Converts this container to anOption. The success variant maps toOption.Some; the error variant maps toOption.none().- Returns:
- an
Optioncontaining the success value, or empty
-