-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaginator.java
More file actions
162 lines (141 loc) · 4.69 KB
/
Paginator.java
File metadata and controls
162 lines (141 loc) · 4.69 KB
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
/*
* Copyright (c) 2022, iland Internet Solutions, Corp
*
* This software is licensed under the Terms and Conditions contained within the
* "LICENSE.txt" file that accompanied this software. Any inquiries concerning
* the scope or enforceability of the license should be addressed to:
*
* iland Internet Solutions, Corp
* 1235 North Loop West, Suite 800
* Houston, TX 77008
* USA
*
* http://www.iland.com
*/
package com.iland.coda.footprint.pagination;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import com.google.common.base.Stopwatch;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.Futures;
import net.codacloud.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An abstraction for the retrieval of paginated data from the SDK.
*
* @param <I> the paginated SDK type
* @param <V> the item value type
*/
public final class Paginator<I, V> implements AutoCloseable {
private static final Logger logger =
LoggerFactory.getLogger(Paginator.class);
private final ExecutorService service = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors());
private final PageFetcher<I> fetcher;
private final Function<I, Page<V>> pageMapper;
public Paginator(final PageFetcher<I> fetcher,
final ToIntFunction<I> pageNoMapper,
final ToIntFunction<I> totalPageMapper,
final ToIntFunction<I> totalCountMapper,
final Function<I, List<V>> itemsMapper) {
this.fetcher = fetcher;
this.pageMapper = i -> new Page<>(pageNoMapper.applyAsInt(i),
totalPageMapper.applyAsInt(i), totalCountMapper.applyAsInt(i),
itemsMapper.apply(i));
}
/**
* Fetch and return all items from all pages.
*
* @return a {@link List} of {@link V items} from all pages
* @throws ApiException ...
*/
public List<V> fetchAll() throws ApiException {
return fetchAll(false, ArrayList::new);
}
/**
* Fetch and return all items from all pages.
*
* @return a {@link Set} of {@link V items} from all pages
* @throws ApiException ...
*/
public Set<V> fetchAllAsync() throws ApiException {
return fetchAll(true, HashSet::new);
}
private <C extends Collection<V>> C fetchAll(final boolean parallel,
final Supplier<C> supplier) throws ApiException {
final I pageOfItems = fetcher.fetch(1);
final Page<V> firstPage = pageMapper.apply(pageOfItems);
final AtomicInteger count = new AtomicInteger(0);
try {
return Stream.concat(
Stream.of(pageOfItems).map(CompletableFuture::completedFuture),
IntStream.range(2, firstPage.getTotalPages() + 1)
.mapToObj(pageNo -> submit(parallel, pageNo, count)))
// collect here to act as a latch
.toList()
.stream()
.map(Futures::getUnchecked)
.map(pageMapper)
.map(Page::getItems)
.flatMap(List::stream)
.collect(Collectors.toCollection(supplier));
} catch (final RuntimeException e) {
Throwables.throwIfInstanceOf(e.getCause(), ApiException.class);
throw e;
}
}
private Future<I> submit(final boolean parallel, final int pageNo,
final AtomicInteger count) {
if (parallel) {
return service.submit(() -> fetch(pageNo, count));
}
try {
final I result = fetch(pageNo, count);
return CompletableFuture.completedFuture(result);
} catch (final ApiException e) {
return CompletableFuture.failedFuture(e);
}
}
private I fetch(final Integer pageNo, final AtomicInteger count)
throws ApiException {
final Stopwatch stopwatch = Stopwatch.createStarted();
final I fetch = fetcher.fetch(pageNo);
if (logger.isDebugEnabled()) {
final Page<V> page = pageMapper.apply(fetch);
final Integer totalPages = page.getTotalPages();
final String percent =
calculatePercentage(count.incrementAndGet(), totalPages);
logger.debug("Page {}/{} ({} items) retrieved in {} ({}%)", pageNo,
totalPages, page.getItems().size(), stopwatch, percent);
}
return fetch;
}
private static String calculatePercentage(final int a, final int b) {
return new BigDecimal(a).divide(BigDecimal.valueOf(b), 3,
RoundingMode.FLOOR)
.multiply(BigDecimal.valueOf(100))
.setScale(1, RoundingMode.UNNECESSARY)
.toString();
}
@Override
public void close() {
service.shutdown();
}
}