Class BasePaginator<T,J,P extends BasePaginator<T,J,P>>

java.lang.Object
com.mewna.catnip.util.pagination.BasePaginator<T,J,P>
Type Parameters:
T - Type of the fetched entities.
J - (Internal use)
P - (Internal use)
Direct Known Subclasses:
ArrayOfObjectPaginator, AuditLogPaginator

public abstract class BasePaginator<T,J,P extends BasePaginator<T,J,P>> extends Object
Utility class for iterating paginated endpoints, handling fetching the next objects automatically, until a given limit is reached or no more entities can be fetched.
Since:
10/8/18.
Author:
natanbc
  • Field Details

    • idOf

      protected final Function<T,String> idOf
    • maxRequestSize

      protected final int maxRequestSize
    • requestSize

      protected int requestSize
    • limit

      protected int limit
  • Constructor Details

    • BasePaginator

      public BasePaginator(@Nonnull Function<T,String> idOf, @Nonnegative int maxRequestSize)
  • Method Details

    • limit

      public P limit(@Nonnegative int limit)
      Sets a limit to how many objects will be requested.
      Only affects future calls to methods that start a pagination.
      The actual amount of entities fetched may be smaller than the limit.
      Parameters:
      limit - Maximum amount of entities to fetch.
      Returns:
      this, for chaining calls.
    • requestSize

      public P requestSize(@Nonnegative int requestSize)
      Sets how many entities to fetch per request.
      Usually you don't need to touch this method, as an appropriate value will be chosen by default.
      Parameters:
      requestSize - Amount of entities to fetch per request.
      Returns:
      this, for chaining calls.
    • fetch

      @Nonnull @CheckReturnValue public io.reactivex.rxjava3.core.Observable<T> fetch()
      Fetches up to limit entities, returning a list containing all of them.
      This method will keep all entities in memory, so for unbounded pagination it should be avoided.
      Returns:
      A list containing all the fetched entities.
    • forEach

      @Nonnull public io.reactivex.rxjava3.core.Observable<UnitHelper.Unit> forEach(@Nonnull Consumer<T> action)
      Fetches up to limit entities, providing them to a given callback.
      This method will not cache the provided entities, so it's recommended for unbounded pagination.
      If the provided callback throws an exception, pagination will stop and the returned completion stage will be failed.
      Parameters:
      action - Callback for fetched entities.
      Returns:
      A completion stage representing the end of the iteration.
    • fetchWhile

      @Nonnull public io.reactivex.rxjava3.core.Observable<UnitHelper.Unit> fetchWhile(@Nonnull PaginationCallback<T> callback)
      Fetches entities until the provided callback returns false or the limit is reached.
      This method will not cache the provided entities, so it's recommended for unbounded pagination.
      If the provided callback throws an exception, pagination will stop and the returned observable will be failed.
      Parameters:
      callback - Callback for fetched entities.
      Returns:
      A completion stage representing the end of the iteration.
    • fetch

      @Nonnull @CheckReturnValue protected io.reactivex.rxjava3.core.Observable<UnitHelper.Unit> fetch(@Nonnull PaginationCallback<T> action)
    • fetch

      @Nonnull @CheckReturnValue protected io.reactivex.rxjava3.core.Observable<UnitHelper.Unit> fetch(@Nullable String id, @Nonnull BasePaginator.RequestState<T> state)
    • update

      protected abstract void update(@Nonnull BasePaginator.RequestState<T> state, @Nonnull J data)
    • fetchNext

      @Nonnull @CheckReturnValue protected abstract io.reactivex.rxjava3.core.Observable<J> fetchNext(@Nonnull BasePaginator.RequestState<T> state, @Nullable String lastId, @Nonnegative int requestSize)