Class Response<T>
java.lang.Object
io.github.matyrobbrt.curseforgeapi.request.Response<T>
- Type Parameters:
T
- the type of value
A response object which may or may not contain a value. Similar
Implementation to
Optional
.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Response<T>
Returns an emptyResponse
instance.boolean
If a value is present, and the value matches the given predicate, returns anResponse
describing the value, otherwise returns an emptyResponse
.<U> Response<U>
If a value is present, returns the result of applying the givenResponse
-bearing mapping function to the value, otherwise returns an emptyResponse
.get()
If a value is present, returns the value, otherwise throwsNoSuchElementException
.int
hashCode()
void
If a value is present, performs the given action with the value, otherwise does nothing.void
ifPresentOrElse
(Consumer<? super T> action, Runnable emptyAction) If a value is present, performs the given action with the value, otherwise performs the given empty-based action.boolean
isEmpty()
boolean
<U> Response<U>
If a value is present, returns anResponse
describing (as if byofNullable(T, java.lang.Integer)
) the result of applying the given mapping function to the value, otherwise returns an emptyResponse
.<U> U
Map the value inside theResponse
to another one if present, or else, return the suppliedorElse
.<U> U
mapOrElseWithException
(Function<? super T, ? extends U> mapper, Supplier<U> orElse) Map the value inside theResponse
to another one if present, or else, return the suppliedorElse
.static <T> Response<T>
Returns anResponse
describing the given non-null
value.static <T> Response<T>
ofNullable
(T value, Integer statusCode) Returns anResponse
describing the given value, if non-null
, otherwise returns an emptyResponse
.static <T> Response<T>
ofNullableAndStatusCode
(T value, Integer statusCode) If a response value is present, returns anResponse
describing the value, otherwise returns anResponse
produced by the supplying function.If a value is present, returns the value, otherwise returnsother
.If a value is present, returns the value, otherwise returns the result produced by the supplying function.If a value is present, returns the value, otherwise throwsNoSuchElementException
.orElseThrow
(Supplier<? extends X> exceptionSupplier) If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.stream()
If a response value is present, returns a sequentialStream
containing only that value, otherwise returns an emptyStream
.
-
Method Details
-
empty
Returns an emptyResponse
instance. No value is present for thisResponse
.- Type Parameters:
T
- The type of the non-existent value- Parameters:
statusCode
- the nullable status code of theResponse
- Returns:
- an empty
Response
-
of
Returns anResponse
describing the given non-null
value.- Type Parameters:
T
- the type of the value- Parameters:
value
- the value to describe, which must be non-null
statusCode
- the nullable status code of theResponse
- Returns:
- an
Response
with the value present - Throws:
NullPointerException
- if value isnull
-
ofNullable
Returns anResponse
describing the given value, if non-null
, otherwise returns an emptyResponse
.- Type Parameters:
T
- the type of the value- Parameters:
value
- the possibly-null
value to describestatusCode
- the nullable status code of theResponse
- Returns:
- an
Response
with a present value if the specified value is non-null
, otherwise an emptyResponse
-
ofNullableAndStatusCode
-
getStatusCode
- Returns:
- the status code of this response
-
isPresent
public boolean isPresent()- Returns:
true
if a value is present, otherwisefalse
-
isEmpty
public boolean isEmpty()- Returns:
true
if a value is not present, otherwisefalse
-
ifPresent
If a value is present, performs the given action with the value, otherwise does nothing.- Parameters:
action
- the action to be performed, if a value is present- Throws:
NullPointerException
- if value is present and the given action isnull
-
ifPresentOrElse
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.- Parameters:
action
- the action to be performed, if a value is presentemptyAction
- the empty-based action to be performed, if no value is present- Throws:
NullPointerException
- if a value is present and the given action isnull
, or no value is present and the given empty-based action isnull
.
-
filter
If a value is present, and the value matches the given predicate, returns anResponse
describing the value, otherwise returns an emptyResponse
. ThestatusCode
is copied in the newResponse
.- Parameters:
predicate
- the predicate to apply to a value, if present- Returns:
- an
Response
describing the value of thisResponse
, if a value is present and the value matches the given predicate, otherwise an emptyResponse
- Throws:
NullPointerException
- if the predicate isnull
-
map
If a value is present, returns anResponse
describing (as if byofNullable(T, java.lang.Integer)
) the result of applying the given mapping function to the value, otherwise returns an emptyResponse
.
ThestatusCode
is copied in the newResponse
.If the mapping function returns a
null
result then this method returns an emptyResponse
.- Type Parameters:
U
- The type of the value returned from the mapping function- Parameters:
mapper
- the mapping function to apply to a value, if present- Returns:
- an
Response
describing the result of applying a mapping function to the value of thisResponse
, if a value is present, otherwise an emptyResponse
- Throws:
NullPointerException
- if the mapping function isnull
-
mapOrElse
Map the value inside theResponse
to another one if present, or else, return the suppliedorElse
.- Type Parameters:
U
- the type of the new value- Parameters:
mapper
- the mapper if the value is presentorElse
- the value to return is the value is not present- Returns:
- the mapped value
-
mapOrElseWithException
public <U> U mapOrElseWithException(Function<? super T, ? extends U> mapper, Supplier<U> orElse) throws ExceptionMap the value inside theResponse
to another one if present, or else, return the suppliedorElse
.
Opposed tomapOrElse(Function, Supplier)
, this method accepts anExceptionFunction
andExceptionSupplier
for providing the mapper and the supplier.- Type Parameters:
U
- the type of the new value- Parameters:
mapper
- the mapper if the value is presentorElse
- the value to return is the value is not present- Returns:
- the mapped value
- Throws:
Exception
-
flatMap
If a value is present, returns the result of applying the givenResponse
-bearing mapping function to the value, otherwise returns an emptyResponse
.This method is similar to
map(Function)
, but the mapping function is one whose result is already anResponse
, and if invoked,flatMap
does not wrap it within an additionalResponse
.- Type Parameters:
U
- The type of value of theResponse
returned by the mapping function- Parameters:
mapper
- the mapping function to apply to a value, if present- Returns:
- the result of applying an
Response
-bearing mapping function to the value of thisResponse
, if a value is present, otherwise an emptyResponse
- Throws:
NullPointerException
- if the mapping function isnull
or returns anull
result
-
or
If a response value is present, returns anResponse
describing the value, otherwise returns anResponse
produced by the supplying function.- Parameters:
supplier
- the supplying function that produces anResponse
to be returned- Returns:
- returns an
Response
describing the value of thisResponse
, if a value is present, otherwise anResponse
produced by the supplying function. - Throws:
NullPointerException
- if the supplying function isnull
or produces anull
result
-
stream
If a response value is present, returns a sequentialStream
containing only that value, otherwise returns an emptyStream
.- Returns:
- the response value as a
Stream
-
orElse
If a value is present, returns the value, otherwise returnsother
.- Parameters:
other
- the value to be returned, if no value is present. May benull
.- Returns:
- the value, if present, otherwise
other
-
orElseGet
If a value is present, returns the value, otherwise returns the result produced by the supplying function.- Parameters:
supplier
- the supplying function that produces a value to be returned- Returns:
- the value, if present, otherwise the result produced by the supplying function
- Throws:
NullPointerException
- if no value is present and the supplying function isnull
-
get
If a value is present, returns the value, otherwise throwsNoSuchElementException
.- Returns:
- the non-
null
value described by thisResponse
- Throws:
NoSuchElementException
- if no value is present
-
orElseThrow
If a value is present, returns the value, otherwise throwsNoSuchElementException
.- Returns:
- the non-
null
value described by thisResponse
- Throws:
NoSuchElementException
- if no value is present
-
orElseThrow
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.- Type Parameters:
X
- Type of the exception to be thrown- Parameters:
exceptionSupplier
- the supplying function that produces an exception to be thrown- Returns:
- the value, if present
- Throws:
X
- if no value is presentNullPointerException
- if no value is present and the exception supplying function isnull
-
toOptional
- Returns:
- the
Optional
-
equals
-
hashCode
public int hashCode()
-