/*
 * Copyright 2019 EPAM Systems
 *
 * 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 com.epam.ta.reportportal.job;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author <a href="mailto:ivan_budayeu@epam.com">Ivan Budayeu</a>
 */
public class PageUtil {

	private PageUtil() {
		//static only
	}

	private static final int DEFAULT_PAGE_SIZE = 50;

	/**
	 * Iterates over all pages found
	 *
	 * @param getFunc  Get Page function
	 * @param consumer Page processor
	 * @param <T>      Type of page entity
	 */
	public static <T> void iterateOverPages(Sort sort, Function<Pageable, Page<T>> getFunc, Consumer<List<T>> consumer) {
		iterateOverPages(DEFAULT_PAGE_SIZE, sort, getFunc, consumer);
	}

	/**
	 * Iterates over all pages found
	 *
	 * @param pageSize page size
	 * @param getFunc  Get Page function
	 * @param consumer Page processor
	 * @param <T>      Type of page entity
	 */
	public static <T> void iterateOverPages(int pageSize, Sort sort, Function<Pageable, Page<T>> getFunc, Consumer<List<T>> consumer) {
		//first page
		Page<T> pageData = getFunc.apply(PageRequest.of(0, pageSize, sort));
		List<T> content = pageData.getContent();
		consumer.accept(content);

		while (!pageData.isLast()) {
			pageData = getFunc.apply(pageData.nextPageable());
			consumer.accept(pageData.getContent());
		}
	}

}

@@ -2071,10 +2071,649 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * Iterates over all pages found%0A%09 *%0A%09 * @param pageSize page size%0A%09 * @param getFunc  Get %7B@link List%7D content function%0A%09 * @param consumer Page processor%0A%09 * @param %3CT%3E      Type of %7B@link List%7D entity%0A%09 */%0A%09public static %3CT%3E void iterateOverContent(int pageSize, Function%3CPageable, List%3CT%3E%3E getFunc, Consumer%3CList%3CT%3E%3E consumer) %7B%0A%09%09//first page%0A%09%09Pageable pageRequest = PageRequest.of(0, pageSize);%0A%09%09List%3CT%3E content = getFunc.apply(pageRequest);%0A%09%09consumer.accept(content);%0A%0A%09%09while (content.size() %3E= pageSize) %7B%0A%09%09%09pageRequest = pageRequest.next();%0A%09%09%09content = getFunc.apply(pageRequest);%0A%09%09%09consumer.accept(content);%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

return iterate over content