[Major] refactorings of Paging

This commit is contained in:
Robert von Burg 2015-07-15 10:49:47 +02:00
parent 01c9da446c
commit 2b9d09632c
1 changed files with 35 additions and 23 deletions

View File

@ -22,32 +22,33 @@ import java.util.List;
*/ */
public class Paging<T> { public class Paging<T> {
private int resultsPerPage; private int pageSize;
private int indexOfPageToReturn; private int pageToReturn;
private int nrOfPages; private int nrOfPages;
private int nrOfElements;
private List<T> input; private List<T> input;
private List<T> page; private List<T> page;
private Paging(int resultsPerPage, int indexOfPageToReturn) { private Paging(int pageSize, int indexOfPageToReturn) {
this.resultsPerPage = resultsPerPage; this.pageSize = pageSize;
this.indexOfPageToReturn = indexOfPageToReturn; this.pageToReturn = indexOfPageToReturn;
} }
public int getResultsPerPage() { public int getPageSize() {
return this.resultsPerPage; return this.pageSize;
} }
public void setResultsPerPage(int resultsPerPage) { public void setPageSize(int pageSize) {
this.resultsPerPage = resultsPerPage; this.pageSize = pageSize;
} }
public int getIndexOfPageToReturn() { public int getPageToReturn() {
return this.indexOfPageToReturn; return this.pageToReturn;
} }
public void setIndexOfPageToReturn(int indexOfPageToReturn) { public void setPageToReturn(int pageToReturn) {
this.indexOfPageToReturn = indexOfPageToReturn; this.pageToReturn = pageToReturn;
} }
public int getNrOfPages() { public int getNrOfPages() {
@ -58,6 +59,14 @@ public class Paging<T> {
this.nrOfPages = nrOfPages; this.nrOfPages = nrOfPages;
} }
public int getNrOfElements() {
return this.nrOfElements;
}
public void setNrOfElements(int nrOfElements) {
this.nrOfElements = nrOfElements;
}
public List<T> getInput() { public List<T> getInput() {
return this.input; return this.input;
} }
@ -71,19 +80,22 @@ public class Paging<T> {
* *
* @param list * @param list
* the list to paginate * the list to paginate
* @param resultsPerPage * @param pageSize
* The number of items to return in each page * The number of items to return in each page
* @param page * @param page
* the page to return - start index is 1 * the page to return - start index is 1
* *
* @return a {@link Paging} instance from which the selected page (list) can be retrieved * @return a {@link Paging} instance from which the selected page (list) can be retrieved
*/ */
public static <T> Paging<T> asPage(List<T> list, int resultsPerPage, int page) { public static <T> Paging<T> asPage(List<T> list, int pageSize, int page) {
Paging<T> paging = new Paging<T>(resultsPerPage, page); Paging<T> paging = new Paging<T>(pageSize, page);
paging.nrOfElements = list.size();
if (paging.resultsPerPage < 0 || paging.indexOfPageToReturn < 0) { if (paging.pageSize <= 0 || paging.pageToReturn <= 0) {
paging.nrOfPages = -1; paging.nrOfPages = 1;
paging.pageSize = list.size();
paging.pageToReturn = 1;
paging.input = list; paging.input = list;
paging.page = list; paging.page = list;
return paging; return paging;
@ -92,16 +104,16 @@ public class Paging<T> {
int size = list.size(); int size = list.size();
// calculate maximum number of pages // calculate maximum number of pages
paging.nrOfPages = size / paging.resultsPerPage; paging.nrOfPages = size / paging.pageSize;
if (size % paging.resultsPerPage != 0) if (size % paging.pageSize != 0)
paging.nrOfPages++; paging.nrOfPages++;
// and from this validate requested page // and from this validate requested page
paging.indexOfPageToReturn = Math.min(paging.indexOfPageToReturn, paging.nrOfPages); paging.pageToReturn = Math.min(paging.pageToReturn, paging.nrOfPages);
// now we can calculate the start and end of the page // now we can calculate the start and end of the page
int start = Math.max(0, paging.resultsPerPage * paging.indexOfPageToReturn - paging.resultsPerPage); int start = Math.max(0, paging.pageSize * paging.pageToReturn - paging.pageSize);
int end = Math.min(size, paging.resultsPerPage * paging.indexOfPageToReturn); int end = Math.min(size, paging.pageSize * paging.pageToReturn);
// and return the list // and return the list
paging.page = list.subList(start, end); paging.page = list.subList(start, end);