-
-
Notifications
You must be signed in to change notification settings - Fork 738
/
PaginationAction.java
689 lines (645 loc) · 24 KB
/
PaginationAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.requests.restaction.pagination;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.requests.*;
import net.dv8tion.jda.core.utils.Checks;
import net.dv8tion.jda.core.utils.Procedure;
import net.dv8tion.jda.core.utils.Promise;
import javax.annotation.Nonnull;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* {@link net.dv8tion.jda.core.requests.RestAction RestAction} specification used
* to retrieve entities for paginated endpoints (before, after, limit).
*
* <p><b>Examples</b>
* <pre><code>
* /**
* * 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 static{@literal List<Message>} getMessagesByUser(MessageChannel channel, User user, int limit)
* {
* <u>MessagePaginationAction</u> action = channel.<u>getIterableHistory</u>();
* Stream{@literal <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{@literal ->} message.getAuthor().equals(user) )
* .limit(limit); // limit on filtered stream will be checked independently from previous limit
* return messageStream.collect(Collectors.toList());
* }
* </code></pre>
*
* <pre><code>
* /**
* * Iterates messages in an async stream and stops once the limit has been reached.
* */
* public static void onEachMessageAsync(MessageChannel channel, {@literal Consumer<Message>} consumer, int limit)
* {
* if (limit{@literal <} 1)
* return;
* <u>MessagePaginationAction</u> action = channel.<u>getIterableHistory</u>();
* AtomicInteger counter = new AtomicInteger(limit);
* action.forEachAsync( (message){@literal ->}
* {
* consumer.accept(message);
* // if false the iteration is terminated; else it continues
* return counter.decrementAndGet() == 0;
* });
* }
* </code></pre>
*
* @param <M>
* The current implementation used as chaining return value
* @param <T>
* The type of entity to paginate
*
* @since 3.1
*/
public abstract class PaginationAction<T, M extends PaginationAction<T, M>>
extends RestAction<List<T>> implements Iterable<T>
{
protected final List<T> cached = new CopyOnWriteArrayList<>();
protected final int maxLimit;
protected final int minLimit;
protected final AtomicInteger limit;
protected volatile T last = null;
protected volatile boolean useCache = true;
/**
* Creates a new PaginationAction instance
*
* @param api
* The current JDA instance
* @param route
* The base route
* @param maxLimit
* The inclusive maximum limit that can be used in {@link #limit(int)}
* @param minLimit
* The inclusive minimum limit that can be used in {@link #limit(int)}
* @param initialLimit
* The initial limit to use on the pagination endpoint
*/
public PaginationAction(JDA api, Route.CompiledRoute route, int minLimit, int maxLimit, int initialLimit)
{
super(api, route);
this.maxLimit = maxLimit;
this.minLimit = minLimit;
this.limit = new AtomicInteger(initialLimit);
}
/**
* Creates a new PaginationAction instance
* <br>This is used for PaginationActions that should not deal with
* {@link #limit(int)}
*
* @param api
* The current JDA instance
*/
public PaginationAction(JDA api)
{
super(api, null);
this.maxLimit = 0;
this.minLimit = 0;
this.limit = new AtomicInteger(0);
}
@Override
@SuppressWarnings("unchecked")
public M setCheck(BooleanSupplier checks)
{
return (M) super.setCheck(checks);
}
/**
* The current amount of cached entities for this PaginationAction
*
* @return int size of currently cached entities
*/
public int cacheSize()
{
return cached.size();
}
/**
* Whether the cache of this PaginationAction is empty.
* <br>Logically equivalent to {@code cacheSize() == 0}.
*
* @return True, if no entities have been retrieved yet.
*/
public boolean isEmpty()
{
return cached.isEmpty();
}
/**
* The currently cached entities of recent execution tasks.
* <br>Every {@link net.dv8tion.jda.core.requests.RestAction RestAction} success
* adds to this List. (Thread-Safe due to {@link java.util.concurrent.CopyOnWriteArrayList CopyOnWriteArrayList})
*
* <p><b>This <u>does not</u> contain all entities for the paginated endpoint unless the pagination has reached an end!</b>
* <br>It only contains those entities which already have been retrieved.
*
* @return Immutable {@link java.util.List List} containing all currently cached entities for this PaginationAction
*/
public List<T> getCached()
{
return Collections.unmodifiableList(cached);
}
/**
* The most recent entity retrieved by this PaginationAction instance
*
* @throws java.util.NoSuchElementException
* If no entities have been retrieved yet (see {@link #isEmpty()})
*
* @return The most recent cached entity
*/
public T getLast()
{
final T last = this.last;
if (last == null)
throw new NoSuchElementException("No entities have been retrieved yet.");
return last;
}
/**
* The first cached entity retrieved by this PaginationAction instance
*
* @throws java.util.NoSuchElementException
* If no entities have been retrieved yet (see {@link #isEmpty()})
*
* @return The very first cached entity
*/
public T getFirst()
{
if (cached.isEmpty())
throw new NoSuchElementException("No entities have been retrieved yet.");
return cached.get(0);
}
/**
* Sets the limit that should be used in the next RestAction completion
* call.
*
* <p>The specified limit may not be below the {@link #getMinLimit() Minimum Limit} nor above
* the {@link #getMaxLimit() Maximum Limit}. Unless these limits are specifically omitted. (See documentation of methods)
*
* <p><b>This limit represents how many entities will be retrieved per request and
* <u>NOT</u> the maximum amount of entities that should be retrieved for iteration/sequencing.</b>
* <br>{@code action.limit(50).complete()}
* <br>is not the same as
* <br>{@code action.stream().limit(50).collect(collector)}
*
*
* @param limit
* The limit to use
*
* @throws java.lang.IllegalArgumentException
* If the provided limit is out of range
*
* @return The current PaginationAction implementation instance
*/
@SuppressWarnings("unchecked")
public M limit(final int limit)
{
Checks.check(maxLimit == 0 || limit <= maxLimit, "Limit must not exceed %d!", maxLimit);
Checks.check(minLimit == 0 || limit >= minLimit, "Limit must be greater or equal to %d", minLimit);
synchronized (this.limit)
{
this.limit.set(limit);
}
return (M) this;
}
/**
* Whether already retrieved entities should be stored
* within the internal cache. All cached entities will be
* available from {@link #getCached()}.
* <b>Default: true</b>
* <br>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.
*
* @param enableCache
* Whether to enable entity cache
*
* @return The current PaginationAction implementation instance
*/
@SuppressWarnings("unchecked")
public M cache(final boolean enableCache)
{
this.useCache = enableCache;
return (M) this;
}
/**
* Whether retrieved entities are stored within an
* internal cache. If this is {@code false} entities
* retrieved by the iterator or a call to a {@link net.dv8tion.jda.core.requests.RestAction RestAction}
* terminal operation will not be retrievable from {@link #getCached()}.
* <br>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.
*
* @return True, If entities will be cached.
*/
public boolean isCacheEnabled()
{
return useCache;
}
/**
* The maximum limit that can be used for this PaginationAction
* <br>Limits provided to {@link #limit(int)} must not be greater
* than the returned value.
* <br>If no maximum limit is used this will return {@code 0}.
* That means there is no upper border for limiting this PaginationAction
*
* @return The maximum limit
*/
public final int getMaxLimit()
{
return maxLimit;
}
/**
* The minimum limit that can be used for this PaginationAction
* <br>Limits provided to {@link #limit(int)} must not be less
* than the returned value.
* <br>If no minimum limit is used this will return {@code 0}.
* That means there is no lower border for limiting this PaginationAction
*
* @return The minimum limit
*/
public final int getMinLimit()
{
return minLimit;
}
/**
* The currently used limit.
* <br>If this PaginationAction does not use limitation
* this will return {@code 0}
*
* @return limit
*/
public final int getLimit()
{
return limit.get();
}
/**
* {@link net.dv8tion.jda.core.requests.restaction.pagination.PaginationAction.PaginationIterator PaginationIterator}
* that will iterate over all entities for this PaginationAction.
*
* @return new PaginationIterator
*/
@Nonnull
@Override
public PaginationIterator iterator()
{
return new PaginationIterator();
}
/**
* Iterates over all entities until the provided action returns {@code false}!
* <br>This operation is different from {@link #forEach(Consumer)} as it
* uses successive {@link #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.
* <p><b>This iteration will include already cached entities, in order to exclude cached
* entities use {@link #forEachRemainingAsync(Procedure)}</b>
*
* <h1>Example</h1>
* <pre>{@code
* //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;
* });
* }
* }</pre>
*
* @param action
* {@link net.dv8tion.jda.core.utils.Procedure Procedure} returning {@code true} if iteration should continue!
*
* @throws java.lang.IllegalArgumentException
* If the provided Procedure is {@code null}
*
* @return {@link java.util.concurrent.Future Future} that can be cancelled to stop iteration from outside!
*/
public RequestFuture<?> forEachAsync(final Procedure<T> action)
{
return forEachAsync(action, (throwable) ->
{
if (RestAction.DEFAULT_FAILURE != null)
RestAction.DEFAULT_FAILURE.accept(throwable);
});
}
/**
* Iterates over all entities until the provided action returns {@code false}!
* <br>This operation is different from {@link #forEach(Consumer)} as it
* uses successive {@link #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.
*
* <p><b>This iteration will include already cached entities, in order to exclude cached
* entities use {@link #forEachRemainingAsync(Procedure, Consumer)}</b>
*
* <h1>Example</h1>
* <pre>{@code
* //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);
* }
* }</pre>
*
* @param action
* {@link net.dv8tion.jda.core.utils.Procedure Procedure} returning {@code true} if iteration should continue!
* @param failure
* {@link java.util.function.Consumer Consumer} that should handle any throwables from the action
*
* @throws java.lang.IllegalArgumentException
* If the provided Procedure or the failure Consumer is {@code null}
*
* @return {@link java.util.concurrent.Future Future} that can be cancelled to stop iteration from outside!
*/
public RequestFuture<?> forEachAsync(final Procedure<T> action, final Consumer<Throwable> failure)
{
Checks.notNull(action, "Procedure");
Checks.notNull(failure, "Failure Consumer");
final Promise<?> task = new Promise<>();
final Consumer<List<T>> acceptor = new ChainedConsumer(task, action, (throwable) ->
{
task.completeExceptionally(throwable);
failure.accept(throwable);
});
try
{
acceptor.accept(cached);
}
catch (Exception ex)
{
failure.accept(ex);
task.completeExceptionally(ex);
}
return task;
}
/**
* Iterates over all remaining entities until the provided action returns {@code false}!
* <br>This operation is different from {@link #forEachRemaining(Procedure)} as it
* uses successive {@link #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.
*
* <p><b>This iteration will exclude already cached entities, in order to include cached
* entities use {@link #forEachAsync(Procedure)}</b>
*
* <h1>Example</h1>
* <pre>{@code
* //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;
* });
* }
* }</pre>
*
* @param action
* {@link net.dv8tion.jda.core.utils.Procedure Procedure} returning {@code true} if iteration should continue!
*
* @throws java.lang.IllegalArgumentException
* If the provided Procedure is {@code null}
*
* @return {@link java.util.concurrent.Future Future} that can be cancelled to stop iteration from outside!
*/
public RequestFuture<?> forEachRemainingAsync(final Procedure<T> action)
{
return forEachRemainingAsync(action, (throwable) ->
{
if (RestAction.DEFAULT_FAILURE != null)
RestAction.DEFAULT_FAILURE.accept(throwable);
});
}
/**
* Iterates over all remaining entities until the provided action returns {@code false}!
* <br>This operation is different from {@link #forEachRemaining(Procedure)} as it
* uses successive {@link #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.
*
* <p><b>This iteration will exclude already cached entities, in order to include cached
* entities use {@link #forEachAsync(Procedure, Consumer)}</b>
*
* <h1>Example</h1>
* <pre>{@code
* //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);
* }
* }</pre>
*
* @param action
* {@link net.dv8tion.jda.core.utils.Procedure Procedure} returning {@code true} if iteration should continue!
* @param failure
* {@link java.util.function.Consumer Consumer} that should handle any throwables from the action
*
* @throws java.lang.IllegalArgumentException
* If the provided Procedure or the failure Consumer is {@code null}
*
* @return {@link java.util.concurrent.Future Future} that can be cancelled to stop iteration from outside!
*/
public RequestFuture<?> forEachRemainingAsync(final Procedure<T> action, final Consumer<Throwable> failure)
{
Checks.notNull(action, "Procedure");
Checks.notNull(failure, "Failure Consumer");
final Promise<?> task = new Promise<>();
final Consumer<List<T>> acceptor = new ChainedConsumer(task, action, (throwable) ->
{
task.completeExceptionally(throwable);
failure.accept(throwable);
});
try
{
//not starting with cache here unlike forEachAsync
acceptor.accept(Collections.emptyList());
}
catch (Exception ex)
{
failure.accept(ex);
task.completeExceptionally(ex);
}
return task;
}
/**
* Iterates over all remaining entities until the provided action returns {@code false}!
* <br>Skipping past already cached entities to iterate all remaining entities of this PaginationAction.
*
* <p><b>This is a blocking operation that might take a while to complete</b>
*
* @param action
* The {@link net.dv8tion.jda.core.utils.Procedure Procedure}
* which should return {@code true} to continue iterating
*/
public void forEachRemaining(final Procedure<T> action)
{
Checks.notNull(action, "Procedure");
Queue<T> queue = new LinkedList<>();
while (queue.addAll(getNextChunk()))
{
while (!queue.isEmpty())
{
if (!action.execute(queue.poll()))
return;
}
}
}
@Override
public Spliterator<T> spliterator()
{
return Spliterators.spliteratorUnknownSize(iterator(), Spliterator.IMMUTABLE);
}
/**
* A sequential {@link java.util.stream.Stream Stream} with this PaginationAction as its source.
*
* @return a sequential {@code Stream} over the elements in this PaginationAction
*/
public Stream<T> stream()
{
return StreamSupport.stream(spliterator(), false);
}
/**
* Returns a possibly parallel {@link java.util.stream.Stream Stream} with this PaginationAction as its
* source. It is allowable for this method to return a sequential stream.
*
* @return a sequential {@code Stream} over the elements in this PaginationAction
*/
public Stream<T> parallelStream()
{
return StreamSupport.stream(spliterator(), true);
}
protected abstract void handleResponse(Response response, Request<List<T>> request);
private List<T> getNextChunk()
{
List<T> items;
synchronized (limit)
{
final int current = limit.getAndSet(getMaxLimit());
items = complete();
limit.set(current);
}
return items;
}
/**
* Iterator implementation for a {@link net.dv8tion.jda.core.requests.restaction.pagination.PaginationAction PaginationAction}.
* <br>This iterator will first iterate over all currently cached entities and continue to retrieve new entities
* as needed.
*
* <p>To retrieve new entities after reaching the end of the current cache, this iterator will
* request a List of new entities through a call of {@link net.dv8tion.jda.core.requests.RestAction#complete() RestAction.complete()}.
* <br><b>It is recommended to use the highest possible limit for this task. (see {@link #limit(int)})</b>
*/
public class PaginationIterator implements Iterator<T>
{
protected Queue<T> items = new LinkedList<>(cached);
@Override
public boolean hasNext()
{
if (items == null)
return false;
if (!hitEnd())
return true;
if (items.addAll(getNextChunk()))
return true;
// null indicates that the real end has been reached
items = null;
return false;
}
@Override
public T next()
{
if (!hasNext())
throw new NoSuchElementException("Reached End of pagination task!");
return items.poll();
}
protected boolean hitEnd()
{
return items.isEmpty();
}
}
protected class ChainedConsumer implements Consumer<List<T>>
{
protected final CompletableFuture<?> task;
protected final Procedure<T> action;
protected final Consumer<Throwable> throwableConsumer;
protected boolean initial = true;
protected ChainedConsumer(final CompletableFuture<?> task, final Procedure<T> action,
final Consumer<Throwable> throwableConsumer)
{
this.task = task;
this.action = action;
this.throwableConsumer = throwableConsumer;
}
@Override
public void accept(final List<T> list)
{
if (list.isEmpty() && !initial)
{
task.complete(null);
return;
}
initial = false;
for (T it : list)
{
if (task.isCancelled())
return;
if (action.execute(it))
continue;
task.complete(null);
return;
}
synchronized (limit)
{
final int currentLimit = limit.getAndSet(maxLimit);
queue(this, throwableConsumer);
limit.set(currentLimit);
}
}
}
}