Package com.mewna.catnip.util.pagination
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
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
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
protected final int
protected int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionio.reactivex.rxjava3.core.Observable<T>
fetch()
Fetches up tolimit
entities, returning a list containing all of them.protected io.reactivex.rxjava3.core.Observable<UnitHelper.Unit>
fetch
(PaginationCallback<T> action) protected io.reactivex.rxjava3.core.Observable<UnitHelper.Unit>
fetch
(String id, BasePaginator.RequestState<T> state) protected abstract io.reactivex.rxjava3.core.Observable<J>
fetchNext
(BasePaginator.RequestState<T> state, String lastId, int requestSize) io.reactivex.rxjava3.core.Observable<UnitHelper.Unit>
fetchWhile
(PaginationCallback<T> callback) Fetches entities until the provided callback returns false or thelimit
is reached.io.reactivex.rxjava3.core.Observable<UnitHelper.Unit>
Fetches up tolimit
entities, providing them to a given callback.limit
(int limit) Sets a limit to how many objects will be requested.requestSize
(int requestSize) Sets how many entities to fetch per request.protected abstract void
update
(BasePaginator.RequestState<T> state, J data)
-
Field Details
-
idOf
-
maxRequestSize
protected final int maxRequestSize -
requestSize
protected int requestSize -
limit
protected int limit
-
-
Constructor Details
-
BasePaginator
-
-
Method Details
-
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
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
Fetches up tolimit
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 tolimit
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 returnedcompletion 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 thelimit
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 returnedobservable
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
-
fetchNext
@Nonnull @CheckReturnValue protected abstract io.reactivex.rxjava3.core.Observable<J> fetchNext(@Nonnull BasePaginator.RequestState<T> state, @Nullable String lastId, @Nonnegative int requestSize)
-