Package nl.vpro.util

Class BatchedReceiver<T>

java.lang.Object
nl.vpro.util.BatchedReceiver<T>
All Implemented Interfaces:
Iterator<T>

public class BatchedReceiver<T> extends Object implements 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 a BiFunction, 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 a Supplier. 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