M
- The current implementation used as chaining return valueT
- The type of entity to paginatejava.lang.Iterable<T>
AuditLogPaginationAction
, MentionPaginationAction
, MessagePaginationAction
, ReactionPaginationAction
public abstract class PaginationAction<T,M extends PaginationAction<T,M>> extends RestAction<java.util.List<T>> implements java.lang.Iterable<T>
RestAction
specification used
to retrieve entities for paginated endpoints (before, after, limit).
Examples
/**
* Retrieves messages until the specified limit is reached. The messages will be limited after being filtered by the user.
* If the user hasn't sent enough messages this will go through all messages so it is recommended to add an additional end condition.
*/
public staticList<Message> getMessagesByUser(MessageChannel channel, User user, int limit)
{
MessagePaginationAction action = channel.getIterableHistory();
Stream<Message> messageStream = action.stream()
.limit(limit * 2) // used to limit amount of messages to check, if user hasn't sent enough messages it would go on forever
.filter( message-> message.getAuthor().equals(user) )
.limit(limit); // limit on filtered stream will be checked independently from previous limit
return messageStream.collect(Collectors.toList());
}
/**
* Iterates messages in an async stream and stops once the limit has been reached.
*/
public static void onEachMessageAsync(MessageChannel channel, Consumer<Message> consumer, int limit)
{
if (limit< 1)
return;
MessagePaginationAction action = channel.getIterableHistory();
AtomicInteger counter = new AtomicInteger(limit);
action.forEachAsync( (message)->
{
consumer.accept(message);
// if false the iteration is terminated; else it continues
return counter.decrementAndGet() == 0;
});
}
Modifier and Type | Class | Description |
---|---|---|
class |
PaginationAction.PaginationIterator |
Iterator implementation for a
PaginationAction . |
RestAction.EmptyRestAction<T>
DEFAULT_FAILURE, DEFAULT_SUCCESS, LOG
Constructor | Description |
---|---|
PaginationAction(JDA api) |
Creates a new PaginationAction instance
This is used for PaginationActions that should not deal with limit(int) |
PaginationAction(JDA api,
net.dv8tion.jda.core.requests.Route.CompiledRoute route,
int minLimit,
int maxLimit,
int initialLimit) |
Creates a new PaginationAction instance
|
Modifier and Type | Method | Description |
---|---|---|
M |
cache(boolean enableCache) |
Whether already retrieved entities should be stored
within the internal cache.
|
int |
cacheSize() |
The current amount of cached entities for this PaginationAction
|
RequestFuture<?> |
forEachAsync(Procedure<T> action) |
Iterates over all entities until the provided action returns
false !
This operation is different from Iterable.forEach(Consumer) as it
uses successive RestAction.queue() tasks to iterate each entity in callback threads instead of
the calling active thread. |
RequestFuture<?> |
forEachAsync(Procedure<T> action,
java.util.function.Consumer<java.lang.Throwable> failure) |
Iterates over all entities until the provided action returns
false !
This operation is different from Iterable.forEach(Consumer) as it
uses successive RestAction.queue() tasks to iterate each entity in callback threads instead of
the calling active thread. |
void |
forEachRemaining(Procedure<T> action) |
Iterates over all remaining entities until the provided action returns
false !
Skipping past already cached entities to iterate all remaining entities of this PaginationAction. |
RequestFuture<?> |
forEachRemainingAsync(Procedure<T> action) |
Iterates over all remaining entities until the provided action returns
false !
This operation is different from forEachRemaining(Procedure) as it
uses successive RestAction.queue() tasks to iterate each entity in callback threads instead of
the calling active thread. |
RequestFuture<?> |
forEachRemainingAsync(Procedure<T> action,
java.util.function.Consumer<java.lang.Throwable> failure) |
Iterates over all remaining entities until the provided action returns
false !
This operation is different from forEachRemaining(Procedure) as it
uses successive RestAction.queue() tasks to iterate each entity in callback threads instead of
the calling active thread. |
java.util.List<T> |
getCached() |
The currently cached entities of recent execution tasks.
|
T |
getFirst() |
The first cached entity retrieved by this PaginationAction instance
|
T |
getLast() |
The most recent entity retrieved by this PaginationAction instance
|
int |
getLimit() |
The currently used limit.
|
int |
getMaxLimit() |
The maximum limit that can be used for this PaginationAction
Limits provided to limit(int) must not be greater
than the returned value. |
int |
getMinLimit() |
The minimum limit that can be used for this PaginationAction
Limits provided to limit(int) must not be less
than the returned value. |
boolean |
isCacheEnabled() |
Whether retrieved entities are stored within an
internal cache.
|
boolean |
isEmpty() |
Whether the cache of this PaginationAction is empty.
|
PaginationAction.PaginationIterator |
iterator() |
PaginationIterator
that will iterate over all entities for this PaginationAction. |
M |
limit(int limit) |
Sets the limit that should be used in the next RestAction completion
call.
|
java.util.stream.Stream<T> |
parallelStream() |
Returns a possibly parallel
Stream with this PaginationAction as its
source. |
M |
setCheck(java.util.function.BooleanSupplier checks) |
Sets the last-second checks before finally executing the http request in the queue.
|
java.util.Spliterator<T> |
spliterator() |
|
java.util.stream.Stream<T> |
stream() |
A sequential
Stream with this PaginationAction as its source. |
RequestFuture<java.util.List<T>> |
takeAsync(int amount) |
Convenience method to retrieve an amount of entities from this pagination action.
|
RequestFuture<java.util.List<T>> |
takeRemainingAsync(int amount) |
Convenience method to retrieve an amount of entities from this pagination action.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
complete, complete, completeAfter, getJDA, isPassContext, queue, queue, queue, queueAfter, queueAfter, queueAfter, queueAfter, queueAfter, queueAfter, setPassContext, submit, submit, submitAfter, submitAfter
public PaginationAction(JDA api, net.dv8tion.jda.core.requests.Route.CompiledRoute route, int minLimit, int maxLimit, int initialLimit)
api
- The current JDA instanceroute
- The base routemaxLimit
- The inclusive maximum limit that can be used in limit(int)
minLimit
- The inclusive minimum limit that can be used in limit(int)
initialLimit
- The initial limit to use on the pagination endpointpublic PaginationAction(JDA api)
limit(int)
api
- The current JDA instancepublic M setCheck(java.util.function.BooleanSupplier checks)
RestAction
false
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.setCheck
in class RestAction<java.util.List<T>>
checks
- The checks to run before executing the request, or null
to run no checkspublic int cacheSize()
public boolean isEmpty()
cacheSize() == 0
.public java.util.List<T> getCached()
RestAction
success
adds to this List. (Thread-Safe due to CopyOnWriteArrayList
)
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.
List
containing all currently cached entities for this PaginationActionpublic T getLast()
java.util.NoSuchElementException
- If no entities have been retrieved yet (see isEmpty()
)public T getFirst()
java.util.NoSuchElementException
- If no entities have been retrieved yet (see isEmpty()
)public M limit(int limit)
The specified limit may not be below the Minimum Limit
nor above
the Maximum 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 as
action.stream().limit(50).collect(collector)
limit
- The limit to usejava.lang.IllegalArgumentException
- If the provided limit is out of rangepublic M cache(boolean enableCache)
getCached()
.
Default: true
enableCache
- Whether to enable entity cachepublic boolean isCacheEnabled()
false
entities
retrieved by the iterator or a call to a RestAction
terminal operation will not be retrievable from getCached()
.
public final int getMaxLimit()
limit(int)
must not be greater
than the returned value.
0
.
That means there is no upper border for limiting this PaginationActionpublic final int getMinLimit()
limit(int)
must not be less
than the returned value.
0
.
That means there is no lower border for limiting this PaginationActionpublic final int getLimit()
0
public RequestFuture<java.util.List<T>> takeAsync(int amount)
forEachAsync(Procedure)
.amount
- The maximum amount to retrieveRequestFuture
- Type: List
forEachAsync(Procedure)
public RequestFuture<java.util.List<T>> takeRemainingAsync(int amount)
takeAsync(int)
this does not include already cached entities.amount
- The maximum amount to retrieveRequestFuture
- Type: List
forEachRemainingAsync(Procedure)
@Nonnull public PaginationAction.PaginationIterator iterator()
PaginationIterator
that will iterate over all entities for this PaginationAction.iterator
in interface java.lang.Iterable<T>
public RequestFuture<?> forEachAsync(Procedure<T> action)
false
!
Iterable.forEach(Consumer)
as it
uses successive RestAction.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 forEachRemainingAsync(Procedure)
//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;
});
}
action
- Procedure
returning true
if iteration should continue!Future
that can be cancelled to stop iteration from outside!java.lang.IllegalArgumentException
- If the provided Procedure is null
public RequestFuture<?> forEachAsync(Procedure<T> action, java.util.function.Consumer<java.lang.Throwable> failure)
false
!
Iterable.forEach(Consumer)
as it
uses successive RestAction.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 forEachRemainingAsync(Procedure, Consumer)
//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);
}
action
- Procedure
returning true
if iteration should continue!failure
- Consumer
that should handle any throwables from the actionFuture
that can be cancelled to stop iteration from outside!java.lang.IllegalArgumentException
- If the provided Procedure or the failure Consumer is null
public RequestFuture<?> forEachRemainingAsync(Procedure<T> action)
false
!
forEachRemaining(Procedure)
as it
uses successive RestAction.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 forEachAsync(Procedure)
//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;
});
}
action
- Procedure
returning true
if iteration should continue!Future
that can be cancelled to stop iteration from outside!java.lang.IllegalArgumentException
- If the provided Procedure is null
public RequestFuture<?> forEachRemainingAsync(Procedure<T> action, java.util.function.Consumer<java.lang.Throwable> failure)
false
!
forEachRemaining(Procedure)
as it
uses successive RestAction.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 forEachAsync(Procedure, Consumer)
//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);
}
action
- Procedure
returning true
if iteration should continue!failure
- Consumer
that should handle any throwables from the actionFuture
that can be cancelled to stop iteration from outside!java.lang.IllegalArgumentException
- If the provided Procedure or the failure Consumer is null
public void forEachRemaining(Procedure<T> action)
false
!
This is a blocking operation that might take a while to complete
action
- The Procedure
which should return true
to continue iteratingpublic java.util.Spliterator<T> spliterator()
spliterator
in interface java.lang.Iterable<T>
public java.util.stream.Stream<T> stream()
Stream
with this PaginationAction as its source.Stream
over the elements in this PaginationActionpublic java.util.stream.Stream<T> parallelStream()
Stream
with this PaginationAction as its
source. It is allowable for this method to return a sequential stream.Stream
over the elements in this PaginationAction