Package nl.vpro.util
Class BatchedReceiver<T>
java.lang.Object
nl.vpro.util.BatchedReceiver<T>
- All Implemented Interfaces:
Iterator<T>
Given some API which supplies only 'batched' retrieval (so with offset and max/batchsize parameters),
access such an API as an iterator to visit all elements.
If an API provides access to huge set of elements, they often do it with some paging mechanism, or by some 'resumption token' formalism. With
BatchedReceiver
this can be morphed into a simple Iterator
.
Paging
The 'batchGetter' argument should be aBiFunction
, returning an iterator for the page described by given offset and batch size
Iterator<String> i = BatchedReceiver.<String>builder()
.batchGetter((offset, max) ->
apiClient.getPage(offset, max).iterator()
)
.batchSize(6)
.build();
i.forEachRemaining(string -> {
...<do stuff...>
});
Resumption token formalism
You simply provide aSupplier
. A lambda would probably not suffice because you might need the previous result the get the next one. E.g. this (using olingo code)
public Iterator<ClientEntity> iterate(URIBuilder ub) {
return BatchedReceiver.<ClientEntity>builder()
.batchGetter(new Supplier<Iterator<ClientEntity>>() {
ClientEntitySet result;
@Override
public Iterator<ClientEntity> get() {
if (result != null) {
result = query(result.getNext());
} else {
result = query(ub);
}
return result.getEntities().iterator();
}
})
.build();
}
- Since:
- 1.68
- Author:
- Michiel Meeuwissen
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionBatchedReceiver
(Integer batchSize, Long offset, BiFunction<Long, Integer, Iterator<T>> _batchGetter) -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining, remove