Advertisement
Guest User

PageSorter.java

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