package eu.univento.core.api.items; import java.util.ArrayList; import java.util.List; import java.util.Optional; /*************************** * Pagifier by leNic * ***************************/ /** * @author leNic * @version 1.0 */ public class Pagifier { // Properties private int pageSize; // List of pages private List> pages; // Constructor public Pagifier(int pageSize){ this.pageSize = pageSize; this.pages = new ArrayList<>(); // Create first page this.pages.add(new ArrayList<>()); } /** * Add item to pages * (Creates a new page if the previous page is not existing or full) * @param item The item you want to add */ public void addItem(T item){ int pageNum = pages.size() - 1; List currentPage = this.pages.get(pageNum); // Add page if full if(currentPage.size() >= this.pageSize) { currentPage = new ArrayList<>(); this.pages.add(currentPage); pageNum++; } currentPage.add(item); } /** * Get the items of a page * @param pageNum Number of the page (beginning at 0) * @return Optional with a list of the page's items */ public Optional> getPage(int pageNum){ if(this.pages.size() == 0) return Optional.empty(); return Optional.of(this.pages.get(pageNum)); } /** * Get all pages * @return List containing all pages */ public List> getPages(){ return this.pages; } /** * Get the current set page size * @return The current page size */ public int getPageSize(){ return this.pageSize; } }