Package net.dv8tion.jda.api.utils
Class Result<T>
- java.lang.Object
-
- net.dv8tion.jda.api.utils.Result<T>
-
- Type Parameters:
T- The success type
public class Result<T> extends Object
Represents a computation or task result.
This result may be afailureorsuccess.This is a value type and does not implement
Object.equals(Object)orObject.hashCode()!- Since:
- 4.2.1
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <E> Result<E>defer(Supplier<? extends E> supplier)Creates a result instance from the provided supplier.Result<T>expect(Predicate<? super Throwable> predicate)Throws the wrapped exception if the provided predicate returns true.static <E> Result<E>failure(Throwable error)Creates a failure result.<U> Result<U>flatMap(Function<? super T,? extends Result<U>> function)Composite function to convert a result value to another result.Tget()Unwraps the success value of this result.ThrowablegetFailure()Unwraps the error for this result.booleanisFailure()True if this result is a failure.booleanisSuccess()True if this result is a success.<U> Result<U>map(Function<? super T,? extends U> function)Composite function to convert a result value to another value.Result<T>onFailure(Consumer<? super Throwable> callback)Passive error handler.Result<T>onSuccess(Consumer<? super T> callback)Passive success handler.static <E> Result<E>success(E value)Creates a successful result.StringtoString()
-
-
-
Method Detail
-
success
@Nonnull @CheckReturnValue public static <E> Result<E> success(@Nullable E value)
Creates a successful result.- Type Parameters:
E- The success type- Parameters:
value- The success value- Returns:
- Result
-
failure
@Nonnull @CheckReturnValue public static <E> Result<E> failure(@Nonnull Throwable error)
Creates a failure result.- Type Parameters:
E- The success type- Parameters:
error- The failure throwable- Returns:
- Result
- Throws:
IllegalArgumentException- If the provided error is null
-
defer
@Nonnull @CheckReturnValue public static <E> Result<E> defer(@Nonnull Supplier<? extends E> supplier)
Creates a result instance from the provided supplier.
If the supplier throws an exception, a failure result is returned.- Type Parameters:
E- The success type- Parameters:
supplier- The supplier- Returns:
- Result instance with the supplied value or exception failure
- Throws:
IllegalArgumentException- If the supplier is null
-
isFailure
public boolean isFailure()
- Returns:
- True, if this is a failure result
-
isSuccess
public boolean isSuccess()
- Returns:
- True, if this is a successful result
-
onFailure
@Nonnull public Result<T> onFailure(@Nonnull Consumer<? super Throwable> callback)
Passive error handler.
This will apply the provided callback ifisFailure()is true and return the same result for further chaining.- Parameters:
callback- The passive callback- Returns:
- The same result instance
- Throws:
IllegalArgumentException- If the callback is null
-
onSuccess
@Nonnull public Result<T> onSuccess(@Nonnull Consumer<? super T> callback)
Passive success handler.
This will apply the provided callback ifisSuccess()is true and return the same result for further chaining.- Parameters:
callback- The passive callback- Returns:
- The same result instance
- Throws:
IllegalArgumentException- If the callback is null
-
map
@Nonnull @CheckReturnValue public <U> Result<U> map(@Nonnull Function<? super T,? extends U> function)
Composite function to convert a result value to another value.
This will only apply the function isisSuccess()is true.- Type Parameters:
U- The result type- Parameters:
function- The conversion function- Returns:
- The mapped result
- Throws:
IllegalArgumentException- If the provided function is null- See Also:
flatMap(Function)
-
flatMap
@Nonnull @CheckReturnValue public <U> Result<U> flatMap(@Nonnull Function<? super T,? extends Result<U>> function)
Composite function to convert a result value to another result.
This will only apply the function isisSuccess()is true.- Type Parameters:
U- The result type- Parameters:
function- The conversion function- Returns:
- The mapped result
- Throws:
IllegalArgumentException- If the provided function is null
-
get
public T get()
Unwraps the success value of this result.
This only works ifisSuccess()is true and throws otherwise.- Returns:
- The result value
- Throws:
IllegalStateException- If the result is not successful
-
expect
@Nonnull public Result<T> expect(@Nonnull Predicate<? super Throwable> predicate)
Throws the wrapped exception if the provided predicate returns true.
This will never provide a null error to the predicate. A successful result will never throw.- Parameters:
predicate- The test predicate- Returns:
- The same result instance
- Throws:
IllegalArgumentException- If the provided predicate is nullIllegalStateException- If the predicate returns true, thecausewill be the wrapped exception
-
-