Skip to content

Instantly share code, notes, and snippets.

@dalibor-drgon
Created April 18, 2017 10:39
Show Gist options
  • Select an option

  • Save dalibor-drgon/5631b2a10c8d66a0b266f14a32cc145d to your computer and use it in GitHub Desktop.

Select an option

Save dalibor-drgon/5631b2a10c8d66a0b266f14a32cc145d to your computer and use it in GitHub Desktop.
Residence PageInfo.java temporal fix
package com.bekvon.bukkit.residence.text.help;
public class PageInfo {
private int totalEntries = 0;
private int totalPages = 0; //ceil(totalEntries / perPage)
private int start = 0; //start index in array
private int end = 0; //last accessed index in array (in loop use <=)
private int currentPage = 0;
private int perPage = 6; //entries per page
public PageInfo(int perPage, int totalEntries, int currentPage) {
this.perPage = perPage;
this.totalEntries = totalEntries;
this.currentPage = currentPage;
calculate();
}
public int getPositionForOutput(int place) {
return this.start + place + 1;
}
private void calculate() {
//start = index of array
//end = start + perPage - 1 (in loop use <=)
this.start = (this.currentPage - 1) * this.perPage;
this.end = this.start + this.perPage - 1;
//clamp end
if (this.end + 1 > this.totalEntries)
this.end = this.totalEntries - 1;
//totalPages = ceil(totalEntries / perPage)
this.totalPages = (int)
Math.ceil((double) this.totalEntries / (double) this.perPage);
//there is always at least one page
//even if there are no entries
//TODO This is kind of temporal fix?
if(this.totalPages == 0)
this.totalPages = 1;
}
public boolean isInRange(int place) {
if (place >= start && place <= end)
return true;
return false;
}
public boolean isPageOk() {
return isPageOk(this.currentPage);
}
//return true when page is in range:
// 0 < page && page <= totalPages
//if page is 1, this is always true
//if page is not 1 and true is returned,
//this indicates that given page is not empty
public boolean isPageOk(int page) {
if (this.totalPages < page)
return false;
if (page < 1)
return false;
return true;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getTotalEntries() {
return totalEntries;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment