
org.nuiton.util.pagination.PaginationResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nuiton-utils Show documentation
Show all versions of nuiton-utils Show documentation
Library of usefull class to be used in any project.
package org.nuiton.util.pagination;
/*
* #%L
* Nuiton Utils
* %%
* Copyright (C) 2004 - 2014 CodeLutin
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
/**
* Represents the result of a pagination request. It contains the result elements together with the
* {@link PaginationParameter} used to compute it. The class also contains methods to
* navigate through the other pages.
*
* @author Arnaud Thimel (Code Lutin)
* @since 3.0
*/
public class PaginationResult implements Serializable {
private static final long serialVersionUID = 1L;
protected List elements;
protected long count;
protected PaginationParameter currentPage;
protected PaginationResult(List elements, long count, PaginationParameter currentPage) {
this.elements = elements;
this.count = count;
this.currentPage = currentPage;
}
/**
* Creates an instance using the already computed list of {code}elements{/code} and {code}count{/count}, together
* with the {code}currentPage{/code} {@link PaginationParameter} used to build it.
*
* @param elements the list of elements
* @param count the total number of elements (through all pages)
* @param currentPage the PaginationParameter used to build this paged result
* @param any object type
* @return the built instance of PaginationResult
*/
public static PaginationResult of(List elements, long count, PaginationParameter currentPage) {
PaginationResult result = new PaginationResult(elements, count, currentPage);
return result;
}
/**
* Creates an instance using the full list of elements ({code}fullList{/code}) and the {code}requestedPage{/code}
* {@link PaginationParameter}. The built instance of PaginationResult will contain a sub list of the given
* {code}fullList{/code} parameter.
*
* @param fullList the full list of elements
* @param requestedPage the PaginationParameter to use to build this paged result
* @param any object type
* @return the built instance of PaginationResult.
*/
public static PaginationResult fromFullList(List fullList, PaginationParameter requestedPage) {
List subList;
int startIndex = requestedPage.getStartIndex();
if (requestedPage.isAll()) {
// Full list requested, use the full list
subList = fullList;
} else if (startIndex >= fullList.size()) {
// If requested page is out of range, return an empty list
subList = new LinkedList();
} else {
int toIndex = Math.min(requestedPage.getEndIndex() + 1, fullList.size());
subList = fullList.subList(startIndex, toIndex);
}
PaginationResult result = PaginationResult.of(subList, fullList.size(), requestedPage);
return result;
}
public List getElements() {
return elements;
}
public long getCount() {
return count;
}
public PaginationParameter getCurrentPage() {
return currentPage;
}
public PaginationParameter getNextPage() {
int nextPageNumber = currentPage.getPageNumber() + 1;
int pageSize = currentPage.getPageSize();
List orderClauses = currentPage.getOrderClauses();
PaginationParameter result = PaginationParameter.
builder(nextPageNumber, pageSize).
addOrderClauses(orderClauses).
build();
return result;
}
public PaginationParameter getPreviousPage() {
// XXX AThimel 21/05/14 Maybe, do not fail, just return the first page ?
Preconditions.checkState(hasPreviousPage(), "You cannot get a previous page to the first one");
int previousPageNumber = currentPage.getPageNumber() - 1;
int pageSize = currentPage.getPageSize();
List orderClauses = currentPage.getOrderClauses();
PaginationParameter result = PaginationParameter.
builder(previousPageNumber, pageSize).
addOrderClauses(orderClauses).
build();
return result;
}
public PaginationParameter getFirstPage() {
int firstPageNumber = 0;
int pageSize = currentPage.getPageSize();
List orderClauses = currentPage.getOrderClauses();
PaginationParameter result = PaginationParameter.
builder(firstPageNumber, pageSize).
addOrderClauses(orderClauses).
build();
return result;
}
public PaginationParameter getLastPage() {
// AThimel 28/05/14 Math.max(0, ...) to make sure last page is working even if there is no result
int lastPageNumber = Math.max(0, getPageCount() - 1);
int pageSize = currentPage.getPageSize();
List orderClauses = currentPage.getOrderClauses();
PaginationParameter result = PaginationParameter.
builder(lastPageNumber, pageSize).
addOrderClauses(orderClauses).
build();
return result;
}
public int getPageCount() {
int pageCount = 1;
int pageSize = currentPage.getPageSize();
if (pageSize >= 1) {
double countDouble = Long.valueOf(count).doubleValue();
double pageSizeDouble = Integer.valueOf(pageSize).doubleValue();
double pageNumberDouble = Math.ceil(countDouble / pageSizeDouble);
pageCount = Double.valueOf(pageNumberDouble).intValue();
}
return pageCount;
}
public boolean hasNextPage() {
int lastPageNumber = getPageCount() - 1;
boolean result = currentPage.getPageNumber() < lastPageNumber;
return result;
}
public boolean hasPreviousPage() {
boolean result = currentPage.getPageNumber() > 0;
return result;
}
/**
* Creates an instance of PaginationResult transforming the current one using the given function
*/
public PaginationResult transform(Function super O, ? extends T> function) {
Iterable transformed = Iterables.transform(getElements(), function);
List transformedElements = Lists.newLinkedList(transformed);
PaginationResult result = PaginationResult.of(transformedElements, getCount(), getCurrentPage());
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy