Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Blackveiled.Diablic.Commands.Utils;
- /**
- * Class: PageSorter
- * Author: Blackveiled (Adam Canfield)
- */
- public class PageSorter {
- /**
- * The page sorter is a useful tool for sorting objects into collectible arrays, to be used to display pages, and values for those fields.
- *
- */
- Object[] values;
- int pageNum;
- int objectsPerPage;
- int totalPages;
- /**
- * Creates a page sorter for an array of objects (can be a list of Warps, Players, or anything else), and sorts them into pages.
- *
- * These pages can be returned individually, the array length of the objects returned are equal to the Objects Per Page declared
- * in the constructor of the PageSorter. This objects can be casted back into their original class. Please note; the PageSorter
- * should not be used for instances of multiple classes, only a single class type.
- * @param values
- * @param pageNumber
- * @param objectsPerPage
- */
- public PageSorter(Object[] values, int pageNumber, int objectsPerPage) {
- this.values = values;
- this.pageNum = pageNumber;
- this.objectsPerPage = objectsPerPage;
- // Ensure values are greater than 0, otherwise the class will break.
- if(pageNumber <= 0) this.pageNum = 1;
- if(objectsPerPage <= 0) this.objectsPerPage = 1;
- // Calculates the last page. This will be automatically rounded up to the next page number to prevent objects being lost from pages.
- double mp2 = (values.length / objectsPerPage) + .5;
- this.totalPages = (int) Math.round(mp2);
- }
- /**
- * This function will sort the objects and return an array of objects for the provided page. These objects must be converted back into
- * their original forms to be utilized.
- * @return Object[] (An array of any object)
- */
- public Object[] getObjectsForPage() {
- int pageCount = (pageNum * objectsPerPage) - objectsPerPage;
- int pageFinal = pageCount + objectsPerPage;
- if(pageFinal > values.length) pageFinal = values.length;
- int counter = 0;
- Object[] output = new Object[objectsPerPage];
- for(int p = pageCount; p < pageFinal; p++) {
- output[counter] = values[p];
- counter++;
- }
- if(output == null) { throw new NullPointerException("PageSorter objects returned are null!"); }
- return output;
- }
- /**
- * Get the index for the last page in this page sorter.
- * @return (int) Value of the last page index.
- */
- public int getTotalPages() {
- return this.totalPages;
- }
- public int getPage() {
- return this.pageNum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement