Class PaginationActionImpl<T,M extends PaginationAction<T,M>>
- All Implemented Interfaces:
Iterable<T>
,RestAction<@Unmodifiable List<T>>
,PaginationAction<T,
M>
- Direct Known Subclasses:
AuditLogPaginationActionImpl
,BanPaginationActionImpl
,EntitlementPaginationActionImpl
,MessagePaginationActionImpl
,PollVotersPaginationActionImpl
,ReactionPaginationActionImpl
,ScheduledEventMembersPaginationActionImpl
,ThreadChannelPaginationActionImpl
,ThreadMemberPaginationActionImpl
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.dv8tion.jda.api.requests.restaction.pagination.PaginationAction
PaginationAction.PaginationIterator<E>, PaginationAction.PaginationOrder
-
Field Summary
Fields inherited from class net.dv8tion.jda.internal.requests.RestActionImpl
LOG
-
Constructor Summary
ConstructorsConstructorDescriptionPaginationActionImpl
(JDA api) Creates a new PaginationAction instance
This is used for PaginationActions that should not deal withlimit(int)
PaginationActionImpl
(JDA api, Route.CompiledRoute route, int minLimit, int maxLimit, int initialLimit) Creates a new PaginationAction instance -
Method Summary
Modifier and TypeMethodDescriptioncache
(boolean enableCache) Whether already retrieved entities should be stored within the internal cache.int
The current amount of cached entities for this PaginationActiondeadline
(long timestamp) Similar toRestAction.timeout(long, TimeUnit)
but schedules a deadline at which the request has to be completed.forEachAsync
(Procedure<? super T> action, Consumer<? super Throwable> failure) Iterates over all entities until the provided action returnsfalse
!
This operation is different fromIterable.forEach(Consumer)
as it uses successiveRestAction.queue()
tasks to iterate each entity in callback threads instead of the calling active thread.void
forEachRemaining
(Procedure<? super T> action) Iterates over all remaining entities until the provided action returnsfalse
!
Skipping past already cached entities to iterate all remaining entities of this PaginationAction.forEachRemainingAsync
(Procedure<? super T> action, Consumer<? super Throwable> failure) Iterates over all remaining entities until the provided action returnsfalse
!
This operation is different fromPaginationAction.forEachRemaining(Procedure)
as it uses successiveRestAction.queue()
tasks to iterate each entity in callback threads instead of the calling active thread.The currently cached entities of recent execution tasks.getFirst()
The first cached entity retrieved by this PaginationAction instancegetLast()
The most recent entity retrieved by this PaginationAction instancelong
The current iteration anchor used for pagination.final int
getLimit()
The currently used limit.final int
The maximum limit that can be used for this PaginationAction
Limits provided toPaginationAction.limit(int)
must not be greater than the returned value.final int
The minimum limit that can be used for this PaginationAction
Limits provided toPaginationAction.limit(int)
must not be less than the returned value.getOrder()
The current iteration order.boolean
Whether retrieved entities are stored within an internal cache.boolean
isEmpty()
Whether the cache of this PaginationAction is empty.iterator()
PaginationIterator
that will iterate over all entities for this PaginationAction.limit
(int limit) Sets the limit that should be used in the next RestAction completion call.Configure thePaginationAction.PaginationOrder
of this pagination action.setCheck
(BooleanSupplier checks) Sets the last-second checks before finally executing the http request in the queue.skipTo
(long id) Skips past the specified ID for successive requests.takeAsync
(int amount) Convenience method to retrieve an amount of entities from this pagination action.takeRemainingAsync
(int amount) Convenience method to retrieve an amount of entities from this pagination action.Timeout for this RestAction instance.Methods inherited from class net.dv8tion.jda.internal.requests.RestActionImpl
complete, getCheck, getDefaultFailure, getDefaultSuccess, getDefaultTimeout, getJDA, handleResponse, isPassContext, priority, queue, setDefaultFailure, setDefaultSuccess, setDefaultTimeout, setErrorMapper, setPassContext, submit
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.dv8tion.jda.api.requests.restaction.pagination.PaginationAction
forEachAsync, forEachRemainingAsync, getSupportedOrders, parallelStream, reverse, spliterator, stream, takeUntilAsync, takeUntilAsync, takeWhileAsync, takeWhileAsync
Methods inherited from interface net.dv8tion.jda.api.requests.RestAction
addCheck, and, and, complete, complete, completeAfter, delay, delay, delay, delay, flatMap, flatMap, getCheck, getJDA, map, mapToResult, onErrorFlatMap, onErrorFlatMap, onErrorMap, onErrorMap, onSuccess, queue, queue, queue, queueAfter, queueAfter, queueAfter, queueAfter, queueAfter, queueAfter, submit, submit, submitAfter, submitAfter, zip
-
Constructor Details
-
PaginationActionImpl
public PaginationActionImpl(JDA api, Route.CompiledRoute route, int minLimit, int maxLimit, int initialLimit) Creates a new PaginationAction instance- Parameters:
api
- The current JDA instanceroute
- The base routeminLimit
- The inclusive minimum limit that can be used inlimit(int)
maxLimit
- The inclusive maximum limit that can be used inlimit(int)
initialLimit
- The initial limit to use on the pagination endpoint
-
PaginationActionImpl
Creates a new PaginationAction instance
This is used for PaginationActions that should not deal withlimit(int)
- Parameters:
api
- The current JDA instance
-
-
Method Details
-
skipTo
Description copied from interface:PaginationAction
Skips past the specified ID for successive requests. This will reset thePaginationAction.getLast()
entity and cause aNoSuchElementException
to be thrown when attempting to get the last entity until a new retrieve action has been done.
If cache is disabled this can be set to an arbitrary value irrelevant of the current last. Set this to0
to start from the most recent message.Fails if cache is enabled and the target id is newer than the current last id (id > last).
Example
public MessagePaginationAction getOlderThan(MessageChannel channel, long time) { final long timestamp = TimeUtil.getDiscordTimestamp(time); final MessagePaginationAction paginator = channel.getIterableHistory(); return paginator.skipTo(timestamp); } getOlderThan(channel, System.currentTimeMillis() - TimeUnit.DAYS.toMillis(14)) .forEachAsync((message) -> { boolean empty = message.getContentRaw().isEmpty(); if (!empty) System.out.printf("%#s%n", message); // means: print display content return !empty; // means: continue if not empty });
- Specified by:
skipTo
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
id
- The snowflake ID to skip before, this is exclusive rather than inclusive- Returns:
- The current PaginationAction for chaining convenience
- See Also:
-
getLastKey
public long getLastKey()Description copied from interface:PaginationAction
The current iteration anchor used for pagination.
This is updated by each retrieve action.- Specified by:
getLastKey
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- The current iteration anchor
- See Also:
-
getOrder
Description copied from interface:PaginationAction
The current iteration order.
This defaults toPaginationAction.PaginationOrder.BACKWARD
, meaning most recent first, for most pagination endpoints.- Specified by:
getOrder
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- The
PaginationAction.PaginationOrder
- See Also:
-
order
Description copied from interface:PaginationAction
Configure thePaginationAction.PaginationOrder
of this pagination action.You can only supply supported orders, see
PaginationAction.getSupportedOrders()
.- Specified by:
order
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
order
- The pagination order- Returns:
- The current PaginationAction implementation instance
- See Also:
-
setCheck
Description copied from interface:RestAction
Sets the last-second checks before finally executing the http request in the queue.
If the provided supplier evaluates tofalse
or throws an exception this will not be finished. When an exception is thrown from the supplier it will be provided to the failure callback.- Specified by:
setCheck
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Specified by:
setCheck
in interfaceRestAction<T>
- Overrides:
setCheck
in classRestActionImpl<List<T>>
- Parameters:
checks
- The checks to run before executing the request, ornull
to run no checks- Returns:
- The current RestAction for chaining convenience
- See Also:
-
timeout
Description copied from interface:RestAction
Timeout for this RestAction instance.
If the request doesn't get executed within the timeout it will fail.When a RestAction times out, it will fail with a
TimeoutException
. This is the same asdeadline(System.currentTimeMillis() + unit.toMillis(timeout))
.Example
action.timeout(10, TimeUnit.SECONDS) // 10 seconds from now .queueAfter(20, SECONDS); // request will not be executed within deadline and timeout immediately after 20 seconds
- Specified by:
timeout
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Specified by:
timeout
in interfaceRestAction<T>
- Parameters:
timeout
- The timeout to useunit
-Unit
for the timeout value- Returns:
- The same RestAction instance with the applied timeout
- See Also:
-
deadline
Description copied from interface:RestAction
Similar toRestAction.timeout(long, TimeUnit)
but schedules a deadline at which the request has to be completed.
If the deadline is reached, the request will fail with aTimeoutException
.This does not mean that the request will immediately timeout when the deadline is reached. JDA will check the deadline right before executing the request or within intervals in a worker thread. This only means the request will timeout if the deadline has passed.
Example
action.deadline(System.currentTimeMillis() + 10000) // 10 seconds from now .queueAfter(20, SECONDS); // request will not be executed within deadline and timeout immediately after 20 seconds
- Specified by:
deadline
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Specified by:
deadline
in interfaceRestAction<T>
- Overrides:
deadline
in classRestActionImpl<List<T>>
- Parameters:
timestamp
- Millisecond timestamp at which the request will timeout- Returns:
- The same RestAction with the applied deadline
- See Also:
-
cacheSize
public int cacheSize()Description copied from interface:PaginationAction
The current amount of cached entities for this PaginationAction- Specified by:
cacheSize
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- int size of currently cached entities
-
isEmpty
public boolean isEmpty()Description copied from interface:PaginationAction
Whether the cache of this PaginationAction is empty.
Logically equivalent tocacheSize() == 0
.- Specified by:
isEmpty
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- True, if no entities have been retrieved yet.
-
getCached
Description copied from interface:PaginationAction
The currently cached entities of recent execution tasks.
EveryRestAction
success adds to this List. (Thread-Safe due toCopyOnWriteArrayList
)This does not contain all entities for the paginated endpoint unless the pagination has reached an end!
It only contains those entities which already have been retrieved.- Specified by:
getCached
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- Immutable
List
containing all currently cached entities for this PaginationAction
-
getLast
Description copied from interface:PaginationAction
The most recent entity retrieved by this PaginationAction instance- Specified by:
getLast
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- The most recent cached entity
-
getFirst
Description copied from interface:PaginationAction
The first cached entity retrieved by this PaginationAction instance- Specified by:
getFirst
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- The very first cached entity
-
limit
Description copied from interface:PaginationAction
Sets the limit that should be used in the next RestAction completion call.The specified limit may not be below the
Minimum Limit
nor above theMaximum Limit
. Unless these limits are specifically omitted. (See documentation of methods)This limit represents how many entities will be retrieved per request and NOT the maximum amount of entities that should be retrieved for iteration/sequencing.
action.limit(50).complete()
is not the same asaction.stream().limit(50).collect(collector)
- Specified by:
limit
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
limit
- The limit to use- Returns:
- The current PaginationAction implementation instance
-
cache
Description copied from interface:PaginationAction
Whether already retrieved entities should be stored within the internal cache. All cached entities will be available fromPaginationAction.getCached()
. Default: true
This being disabled allows unused entities to be removed from the memory heap by the garbage collector. If this is enabled this will not take place until all references to this PaginationAction have been cleared.- Specified by:
cache
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
enableCache
- Whether to enable entity cache- Returns:
- The current PaginationAction implementation instance
-
isCacheEnabled
public boolean isCacheEnabled()Description copied from interface:PaginationAction
Whether retrieved entities are stored within an internal cache. If this isfalse
entities retrieved by the iterator or a call to aRestAction
terminal operation will not be retrievable fromPaginationAction.getCached()
.
This being disabled allows unused entities to be removed from the memory heap by the garbage collector. If this is enabled this will not take place until all references to this PaginationAction have been cleared.- Specified by:
isCacheEnabled
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- True, If entities will be cached.
-
getMaxLimit
public final int getMaxLimit()Description copied from interface:PaginationAction
The maximum limit that can be used for this PaginationAction
Limits provided toPaginationAction.limit(int)
must not be greater than the returned value.
If no maximum limit is used this will return0
. That means there is no upper border for limiting this PaginationAction- Specified by:
getMaxLimit
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- The maximum limit
-
getMinLimit
public final int getMinLimit()Description copied from interface:PaginationAction
The minimum limit that can be used for this PaginationAction
Limits provided toPaginationAction.limit(int)
must not be less than the returned value.
If no minimum limit is used this will return0
. That means there is no lower border for limiting this PaginationAction- Specified by:
getMinLimit
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- The minimum limit
-
getLimit
public final int getLimit()Description copied from interface:PaginationAction
The currently used limit.
If this PaginationAction does not use limitation this will return0
- Specified by:
getLimit
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- limit
-
takeAsync
Description copied from interface:PaginationAction
Convenience method to retrieve an amount of entities from this pagination action.
This also includes already cached entities similar toPaginationAction.forEachAsync(Procedure)
.- Specified by:
takeAsync
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
amount
- The maximum amount to retrieve- Returns:
CompletableFuture
- Type:List
- See Also:
-
takeRemainingAsync
Description copied from interface:PaginationAction
Convenience method to retrieve an amount of entities from this pagination action.
UnlikePaginationAction.takeAsync(int)
this does not include already cached entities.- Specified by:
takeRemainingAsync
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
amount
- The maximum amount to retrieve- Returns:
CompletableFuture
- Type:List
- See Also:
-
iterator
Description copied from interface:PaginationAction
PaginationIterator
that will iterate over all entities for this PaginationAction.- Specified by:
iterator
in interfaceIterable<T>
- Specified by:
iterator
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Returns:
- new PaginationIterator
-
forEachAsync
@Nonnull public CompletableFuture<?> forEachAsync(@Nonnull Procedure<? super T> action, @Nonnull Consumer<? super Throwable> failure) Description copied from interface:PaginationAction
Iterates over all entities until the provided action returnsfalse
!
This operation is different fromIterable.forEach(Consumer)
as it uses successiveRestAction.queue()
tasks to iterate each entity in callback threads instead of the calling active thread. This means that this method fully works on different threads to retrieve new entities.This iteration will include already cached entities, in order to exclude cached entities use
PaginationAction.forEachRemainingAsync(Procedure, Consumer)
Example
//deletes messages until it finds a user that is still in guild public void cleanupMessages(MessagePaginationAction action) { action.forEachAsync( (message) -> { Guild guild = message.getGuild(); if (!guild.isMember(message.getAuthor())) message.delete().queue(); else return false; return true; }, Throwable::printStackTrace); }
- Specified by:
forEachAsync
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
action
-Procedure
returningtrue
if iteration should continue!failure
-Consumer
that should handle any throwables from the action- Returns:
Future
that can be cancelled to stop iteration from outside!
-
forEachRemainingAsync
@Nonnull public CompletableFuture<?> forEachRemainingAsync(@Nonnull Procedure<? super T> action, @Nonnull Consumer<? super Throwable> failure) Description copied from interface:PaginationAction
Iterates over all remaining entities until the provided action returnsfalse
!
This operation is different fromPaginationAction.forEachRemaining(Procedure)
as it uses successiveRestAction.queue()
tasks to iterate each entity in callback threads instead of the calling active thread. This means that this method fully works on different threads to retrieve new entities.This iteration will exclude already cached entities, in order to include cached entities use
PaginationAction.forEachAsync(Procedure, Consumer)
Example
//deletes messages until it finds a user that is still in guild public void cleanupMessages(MessagePaginationAction action) { action.forEachRemainingAsync( (message) -> { Guild guild = message.getGuild(); if (!guild.isMember(message.getAuthor())) message.delete().queue(); else return false; return true; }, Throwable::printStackTrace); }
- Specified by:
forEachRemainingAsync
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
action
-Procedure
returningtrue
if iteration should continue!failure
-Consumer
that should handle any throwables from the action- Returns:
Future
that can be cancelled to stop iteration from outside!
-
forEachRemaining
Description copied from interface:PaginationAction
Iterates over all remaining entities until the provided action returnsfalse
!
Skipping past already cached entities to iterate all remaining entities of this PaginationAction.This is a blocking operation that might take a while to complete
- Specified by:
forEachRemaining
in interfacePaginationAction<T,
M extends PaginationAction<T, M>> - Parameters:
action
- TheProcedure
which should returntrue
to continue iterating
-
getNextChunk
-