fr.ouestfrance.querydsl.postgrest.model.Page Maven / Gradle / Ivy
The newest version!
package fr.ouestfrance.querydsl.postgrest.model;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Representation of a Page, a page is an iterable item which have list of elements, pageable information
* and totalSizeElements and totalPage
*
* @param type of item
*/
public interface Page extends Iterable {
/**
* Create simple page from items
*
* @param items items
* @param type of items
* @return one page of items
*/
@SafeVarargs
static Page of(T... items) {
return new PageImpl<>(Arrays.asList(items), Pageable.unPaged(), items.length, 1);
}
/**
* Create an empty page
*
* @param type of items
* @return empty page
*/
static Page empty() {
return new PageImpl<>(List.of(), Pageable.unPaged(), 0, 0);
}
/**
* Get data for a page
*
* @return data for a page
*/
List getData();
/**
* Get page request infomations
*
* @return Pageable information with number of elements, number of the page and sort options
*/
Pageable getPageable();
/**
* Get size of page
*
* @return size of the data for the current page
*/
default int size() {
return getData().size();
}
/**
* Get total elements from the datasource
*
* @return total elements
*/
long getTotalElements();
/**
* Get the total pages
*
* @return total pages
*/
int getTotalPages();
/**
* Streaming from the page
*
* @return stream
*/
default Stream stream() {
return getData().stream();
}
@Override
default Iterator iterator() {
return getData().iterator();
}
/**
* Convert a page
*
* @param converter function that convert type to another
* @param type of returned object
* @return page converted
*/
default Page map(Function converter) {
return new PageImpl<>(stream().map(converter).toList(), getPageable(), getTotalElements(), getTotalPages());
}
/**
* Check that page has next page
*
* @return true if totalPages is greater than pageNumber
*/
default boolean hasNext() {
return getPageable().getPageNumber() + 1 < getTotalPages();
}
}