java.lang.Object
net.luis.utils.util.Result<T>
- Type Parameters:
T
- The type of the result
Represents the result of an operation that may fail.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
error()
Returns the error message of the result as anOptional
.static <T> @NotNull Result
<T> Creates a new failed result with the specified error message.@NotNull String
Gets the error message of the operation or throws an exception.<R> @NotNull Result
<R> Maps the result of the operation to another result of the same type.
This is useful for chaining operations or validating the result.
If the result is an error, the mapping is not applied.get()
int
hashCode()
boolean
isError()
Checks if the result is an error.boolean
Checks if the result is successful.<R> @NotNull Result
<R> Maps the result of the operation to another type.
If the result is an error, the mapping is not applied.@UnknownNullability T
Gets the result of the operation or the specified default value.
This is useful for providing a fallback value if the operation fails.
This method is equivalent toorElseGet(Supplier)
with a constant supplier.@UnknownNullability T
Gets the result of the operation or the result of the specified supplier.
This is useful for providing a fallback value if the operation fails.@UnknownNullability T
orThrow()
Gets the result of the operation or throws an exception.<X extends RuntimeException>
@UnknownNullability TGets the result of the operation or throws the specified exception.result()
Returns the result of the operation as anOptional
.static <T> @NotNull Result
<T> success
(T value) Creates a new successful result with the specified value.toString()
-
Field Details
-
result
The internal result of this instance.
This is either the result or the error message.
-
-
Constructor Details
-
Result
Constructs a new result with the specified result.- Parameters:
result
- The result of the operation- Throws:
NullPointerException
- If the result is null
-
-
Method Details
-
success
Creates a new successful result with the specified value.- Type Parameters:
T
- The type of the result- Parameters:
value
- The value of the result- Returns:
- The created result
-
error
Creates a new failed result with the specified error message.- Type Parameters:
T
- The type of the result- Parameters:
error
- The error message of the result- Returns:
- The created result
- Throws:
NullPointerException
- If the error message is null
-
get
-
isSuccess
public boolean isSuccess()Checks if the result is successful.- Returns:
- True if the result is successful, otherwise false
-
result
Returns the result of the operation as anOptional
.- Returns:
- The result of the operation
-
orThrow
Gets the result of the operation or throws an exception.- Returns:
- The result of the operation
- Throws:
IllegalStateException
- If the result is an error
-
orThrow
public <X extends RuntimeException> @UnknownNullability T orThrow(@NotNull @NotNull Function<String, ? extends X> exceptionSupplier) Gets the result of the operation or throws the specified exception.- Type Parameters:
X
- The type of the exception- Parameters:
exceptionSupplier
- The supplier holding the exception to be thrown- Returns:
- The result of the operation
- Throws:
NullPointerException
- If the exception supplier is nullX
- If the result is an error
-
isError
public boolean isError()Checks if the result is an error.- Returns:
- True if the result is an error, otherwise false
-
error
Returns the error message of the result as anOptional
.- Returns:
- The error message
-
errorOrThrow
Gets the error message of the operation or throws an exception.- Returns:
- The error message
- Throws:
IllegalStateException
- If the result is not an error
-
map
Maps the result of the operation to another type.
If the result is an error, the mapping is not applied.- Type Parameters:
R
- The type of the mapped result- Parameters:
mapper
- The mapper function- Returns:
- The mapped result
- Throws:
NullPointerException
- If the mapper is null
-
flatMap
Maps the result of the operation to another result of the same type.
This is useful for chaining operations or validating the result.
If the result is an error, the mapping is not applied.- Type Parameters:
R
- The type of the mapped result- Parameters:
mapper
- The mapper function- Returns:
- The mapped result
- Throws:
NullPointerException
- If the mapper is null
-
orElse
Gets the result of the operation or the specified default value.
This is useful for providing a fallback value if the operation fails.
This method is equivalent toorElseGet(Supplier)
with a constant supplier.- Parameters:
fallback
- The fallback value- Returns:
- The result of the operation or the fallback value
- Throws:
NullPointerException
- If the fallback value is null
-
orElseGet
Gets the result of the operation or the result of the specified supplier.
This is useful for providing a fallback value if the operation fails.- Parameters:
supplier
- The supplier of the fallback value- Returns:
- The result of the operation or the fallback value
- Throws:
NullPointerException
- If the supplier is null
-
equals
-
hashCode
public int hashCode() -
toString
-