@@ -0,0 +1,2682 @@
+/*%0A * Copyright 2018 B2i Healthcare Pte Ltd, http://b2i.sg%0A * %0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage com.b2international.snowowl.core.request;%0A%0Aimport java.util.function.Function;%0A%0Aimport com.b2international.snowowl.core.domain.PageableCollectionResource;%0Aimport com.google.common.collect.AbstractIterator;%0A%0A/**%0A * @since 6.4%0A * %0A * @param %3CB%3E the search resource request builder type; an instance of this will be configured for scrolling%0A * @param %3CR%3E the collection resource response type; the iterator will return results as instances of this type%0A */%0Apublic final class SearchResourceRequestIterator%3C%0A%09%09B extends SearchResourceRequestBuilder%3CB, ?, R%3E, %0A%09%09R extends PageableCollectionResource%3C?%3E%3E extends AbstractIterator%3CR%3E %7B%0A%09%0A%09private final B searchRequestBuilder;%0A%09private final Function%3CB, R%3E executeHandler;%0A%0A%09private boolean firstRun = true;%0A%09private String scrollId;%0A%09private int visited;%0A%09private int total;%0A%09%0A%09/**%0A%09 * @param searchRequestBuilder%0A%09 *            the pre-configured request builder (should have a chunk limit and a scroll keepalive time set)%0A%09 * @param executeHandler%0A%09 *            a function that builds an appropriate request, executes it either%0A%09 *            through a request context or an event bus, and returns the results%0A%09 */%0A%09public SearchResourceRequestIterator(B searchRequestBuilder, Function%3CB, R%3E executeHandler) %7B%0A%09%09this.searchRequestBuilder = searchRequestBuilder;%0A%09%09this.executeHandler = executeHandler;%0A%09%7D%0A%09%0A%09@Override%0A%09protected R computeNext() %7B%0A%09%09// If it is not the first time we run the request, and all items have already been collected, exit%0A%09%09if (!firstRun && visited %3E= total) %7B%0A%09%09%09return endOfData();%0A%09%09%7D%0A%09%09%0A%09%09// Execute the request with the last recorded scrollId (can be null on first run) %0A%09%09searchRequestBuilder.setScrollId(scrollId);%0A%09%09R hits = executeHandler.apply(searchRequestBuilder);%0A%0A%09%09// Initialize total counter on first run%0A%09%09if (firstRun) %7B%0A%09%09%09firstRun = false;%0A%09%09%09total = hits.getTotal();%0A%09%09%09%0A%09%09%09if (total %3C 1) %7B%0A%09%09%09%09return endOfData();%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09// Update scrollId and visited counter%0A%09%09scrollId = hits.getScrollId();%0A%09%09visited += hits.getItems().size();%0A%0A%09%09return hits;%0A%09%7D%0A%7D%0A

Add helper class SearchResourceRequestIterator