Advertisement
Guest User

PageSorter.java

a guest
Apr 8th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 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.         this.values = values;
  32.         this.pageNum = pageNumber;
  33.         this.objectsPerPage = objectsPerPage;
  34.         // Ensure values are greater than 0, otherwise the class will break.
  35.         if(pageNumber <= 0) this.pageNum = 1;
  36.         if(objectsPerPage <= 0) this.objectsPerPage = 1;
  37.  
  38.         // Calculates the last page.  This will be automatically rounded up to the next page number to prevent objects being lost from pages.
  39.         double mp2 = (values.length / objectsPerPage) + .5;
  40.         this.totalPages = (int) Math.round(mp2);
  41.     }
  42.  
  43.     /**
  44.      * This function will sort the objects and return an array of objects for the provided page.  These objects must be converted back into
  45.      * their original forms to be utilized.
  46.      * @return Object[] (An array of any object)
  47.      */
  48.     public Object[] getObjectsForPage() {
  49.         int pageCount = (pageNum * objectsPerPage) - objectsPerPage;
  50.         int pageFinal = pageCount + objectsPerPage;
  51.         if(pageFinal > values.length)  pageFinal = values.length;
  52.         int counter = 0;
  53.  
  54.         Object[] output = new Object[objectsPerPage];
  55.  
  56.         for(int p = pageCount; p < pageFinal; p++)  {
  57.             output[counter] = values[p];
  58.             counter++;
  59.         }
  60.         if(output == null) { throw new NullPointerException("PageSorter objects returned are null!"); }
  61.         return output;
  62.     }
  63.  
  64.     /**
  65.      * Get the index for the last page in this page sorter.
  66.      * @return (int) Value of the last page index.
  67.      */
  68.     public int getTotalPages()    {
  69.         return this.totalPages;
  70.     }
  71.  
  72.     public int getPage()    {
  73.         return this.pageNum;
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement