Advertisement
Guest User

PageSorter.java

a guest
Apr 8th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. package com.Blackveiled.Diablic.Commands.Utils;
  2.  
  3. /**
  4.  * Class: PageSorter
  5.  * Author: Blackveiled (Adam Canfield)
  6.  */
  7.  
  8. public class PageSorter {
  9.  
  10.     /**
  11.      * The page sorter is a useful tool for sorting objects into collectible arrays, to be used to display pages, and values for those fields.
  12.      *
  13.      */
  14.  
  15.     Object[] values;
  16.     int pageNum;
  17.     int objectsPerPage;
  18.     int totalPages;
  19.  
  20.     /**
  21.      * Creates a page sorter for an array of objects (can be a list of Warps, Players, or anything else), and sorts them into pages.
  22.      *
  23.      * These pages can be returned individually, the array length of the objects returned are equal to the Objects Per Page declared
  24.      * in the constructor of the PageSorter.  This objects can be casted back into their original class.  Please note; the PageSorter
  25.      * should not be used for instances of multiple classes, only a single class type.
  26.      * @param values
  27.      * @param pageNumber
  28.      * @param objectsPerPage
  29.      */
  30.     public PageSorter(Object[] values, int pageNumber, int objectsPerPage)    {
  31.         if(values != null) this.values = values; else return; //
  32.         // Ensure values are greater than 0, otherwise the class will break.
  33.         if(pageNumber <= 0) this.pageNum = 1;
  34.         if(objectsPerPage <= 0) this.objectsPerPage = 1;
  35.  
  36.         // Calculates the last page.  This will be automatically rounded up to the next page number to prevent objects being lost from pages.
  37.         double mp2 = (values.length / objectsPerPage) + .5;
  38.         this.totalPages = (int) Math.round(mp2);
  39.     }
  40.  
  41.     /**
  42.      * This function will sort the objects and return an array of objects for the provided page.  These objects must be converted back into
  43.      * their original forms to be utilized.
  44.      * @return Object[] (An array of any object)
  45.      */
  46.     public Object[] getObjectsForPage() {
  47.         int pageCount = (pageNum * objectsPerPage) - objectsPerPage;
  48.         int pageFinal = pageCount + objectsPerPage;
  49.         if(pageFinal > values.length)  pageFinal = values.length;
  50.  
  51.         Object[] output = new Object[objectsPerPage];
  52.  
  53.         for(int p = pageCount; p < pageFinal; p++)  {
  54.             output[p] = values[p];
  55.         }
  56.         if(output == null) { throw new NullPointerException("PageSorter objects returned are null!"); }
  57.         return output;
  58.     }
  59.  
  60.     /**
  61.      * Get the index for the last page in this page sorter.
  62.      * @return (int) Value of the last page index.
  63.      */
  64.     public int getTotalPages()    {
  65.         return this.totalPages;
  66.     }
  67.  
  68.     public int getPage()    {
  69.         return this.pageNum;
  70.     }
  71.  
  72.     public int getObjectsPerPage()  {
  73.         return this.objectsPerPage;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement