Class Response<T>

java.lang.Object
io.github.matyrobbrt.curseforgeapi.request.Response<T>
Type Parameters:
T - the type of value

public final class Response<T> extends Object
A response object which may or may not contain a value. Similar Implementation to Optional.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Response<T>
    empty(Integer statusCode)
    Returns an empty Response instance.
    boolean
     
    filter(Predicate<? super T> predicate)
    If a value is present, and the value matches the given predicate, returns an Response describing the value, otherwise returns an empty Response.
    <U> Response<U>
    flatMap(Function<? super T,? extends Response<? extends U>> mapper)
    If a value is present, returns the result of applying the given Response-bearing mapping function to the value, otherwise returns an empty Response.
    get()
    If a value is present, returns the value, otherwise throws NoSuchElementException.
     
    int
     
    void
    ifPresent(Consumer<? super T> action)
    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
     
    boolean
     
    <U> Response<U>
    map(Function<? super T,? extends U> mapper)
    If a value is present, returns an Response describing (as if by ofNullable(T, java.lang.Integer)) the result of applying the given mapping function to the value, otherwise returns an empty Response.
    <U> U
    mapOrElse(Function<? super T,? extends U> mapper, Supplier<U> orElse)
    Map the value inside the Response to another one if present, or else, return the supplied orElse.
    <U> U
    mapOrElseWithException(Function<? super T,? extends U> mapper, Supplier<U> orElse)
    Map the value inside the Response to another one if present, or else, return the supplied orElse.
    static <T> Response<T>
    of(T value, Integer statusCode)
    Returns an Response describing the given non-null value.
    static <T> Response<T>
    ofNullable(T value, Integer statusCode)
    Returns an Response describing the given value, if non-null, otherwise returns an empty Response.
    static <T> Response<T>
    ofNullableAndStatusCode(T value, Integer statusCode)
     
    or(Supplier<? extends Response<? extends T>> supplier)
    If a response value is present, returns an Response describing the value, otherwise returns an Response produced by the supplying function.
    orElse(T other)
    If a value is present, returns the value, otherwise returns other.
    orElseGet(Supplier<? extends T> supplier)
    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 throws NoSuchElementException.
    <X extends Throwable>
    T
    orElseThrow(Supplier<? extends X> exceptionSupplier)
    If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
    If a response value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
    Converts this Request to an Optional.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • empty

      public static <T> Response<T> empty(Integer statusCode)
      Returns an empty Response instance. No value is present for this Response.
      Type Parameters:
      T - The type of the non-existent value
      Parameters:
      statusCode - the nullable status code of the Response
      Returns:
      an empty Response
    • of

      public static <T> Response<T> of(T value, Integer statusCode)
      Returns an Response 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 the Response
      Returns:
      an Response with the value present
      Throws:
      NullPointerException - if value is null
    • ofNullable

      public static <T> Response<T> ofNullable(T value, Integer statusCode)
      Returns an Response describing the given value, if non-null, otherwise returns an empty Response.
      Type Parameters:
      T - the type of the value
      Parameters:
      value - the possibly-null value to describe
      statusCode - the nullable status code of the Response
      Returns:
      an Response with a present value if the specified value is non-null, otherwise an empty Response
    • ofNullableAndStatusCode

      public static <T> Response<T> ofNullableAndStatusCode(T value, Integer statusCode)
    • getStatusCode

      public Integer getStatusCode()
      Returns:
      the status code of this response
    • isPresent

      public boolean isPresent()
      Returns:
      true if a value is present, otherwise false
    • isEmpty

      public boolean isEmpty()
      Returns:
      true if a value is not present, otherwise false
    • ifPresent

      public void ifPresent(Consumer<? super T> action)
      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 is null
    • ifPresentOrElse

      public 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.
      Parameters:
      action - the action to be performed, if a value is present
      emptyAction - the empty-based action to be performed, if no value is present
      Throws:
      NullPointerException - if a value is present and the given action is null, or no value is present and the given empty-based action is null.
    • filter

      public Response<T> filter(Predicate<? super T> predicate)
      If a value is present, and the value matches the given predicate, returns an Response describing the value, otherwise returns an empty Response. The statusCode is copied in the new Response.
      Parameters:
      predicate - the predicate to apply to a value, if present
      Returns:
      an Response describing the value of this Response, if a value is present and the value matches the given predicate, otherwise an empty Response
      Throws:
      NullPointerException - if the predicate is null
    • map

      public <U> Response<U> map(Function<? super T,? extends U> mapper)
      If a value is present, returns an Response describing (as if by ofNullable(T, java.lang.Integer)) the result of applying the given mapping function to the value, otherwise returns an empty Response.
      The statusCode is copied in the new Response.

      If the mapping function returns a null result then this method returns an empty Response.

      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 this Response, if a value is present, otherwise an empty Response
      Throws:
      NullPointerException - if the mapping function is null
    • mapOrElse

      public <U> U mapOrElse(Function<? super T,? extends U> mapper, Supplier<U> orElse)
      Map the value inside the Response to another one if present, or else, return the supplied orElse.
      Type Parameters:
      U - the type of the new value
      Parameters:
      mapper - the mapper if the value is present
      orElse - 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 Exception
      Map the value inside the Response to another one if present, or else, return the supplied orElse.
      Opposed to mapOrElse(Function, Supplier), this method accepts an ExceptionFunction and ExceptionSupplier for providing the mapper and the supplier.
      Type Parameters:
      U - the type of the new value
      Parameters:
      mapper - the mapper if the value is present
      orElse - the value to return is the value is not present
      Returns:
      the mapped value
      Throws:
      Exception
    • flatMap

      public <U> Response<U> flatMap(Function<? super T,? extends Response<? extends U>> mapper)
      If a value is present, returns the result of applying the given Response-bearing mapping function to the value, otherwise returns an empty Response.

      This method is similar to map(Function), but the mapping function is one whose result is already an Response, and if invoked, flatMap does not wrap it within an additional Response.

      Type Parameters:
      U - The type of value of the Response 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 this Response, if a value is present, otherwise an empty Response
      Throws:
      NullPointerException - if the mapping function is null or returns a null result
    • or

      public Response<T> or(Supplier<? extends Response<? extends T>> supplier)
      If a response value is present, returns an Response describing the value, otherwise returns an Response produced by the supplying function.
      Parameters:
      supplier - the supplying function that produces an Response to be returned
      Returns:
      returns an Response describing the value of this Response, if a value is present, otherwise an Response produced by the supplying function.
      Throws:
      NullPointerException - if the supplying function is null or produces a null result
    • stream

      public Stream<T> stream()
      If a response value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
      Returns:
      the response value as a Stream
    • orElse

      public T orElse(T other)
      If a value is present, returns the value, otherwise returns other.
      Parameters:
      other - the value to be returned, if no value is present. May be null.
      Returns:
      the value, if present, otherwise other
    • orElseGet

      public T orElseGet(Supplier<? extends T> supplier)
      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 is null
    • get

      public T get()
      If a value is present, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the non-null value described by this Response
      Throws:
      NoSuchElementException - if no value is present
    • orElseThrow

      public T orElseThrow()
      If a value is present, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the non-null value described by this Response
      Throws:
      NoSuchElementException - if no value is present
    • orElseThrow

      public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
      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 present
      NullPointerException - if no value is present and the exception supplying function is null
    • toOptional

      public Optional<T> toOptional()
      Converts this Request to an Optional. The returned Optional is empty if the Response is empty.
      Returns:
      the Optional
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object