Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package gov.va.med.pbm.ampl.utility.operations;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import gov.va.med.pbm.ampl.utility.CollectionUtility;
import gov.va.med.pbm.ampl.utility.PropertyReflection;

/**
* The PagingOperation class allows the API to perform paging on a given collection of objects, returning a filtered result set
* back to the client.
*
* @author Ian Meinert
*
* @param <T> The Generic type representing the class of the object which the paging will be performed.
*/
public class PagingOperation<T> extends PropertyReflection {
/**
* The application logger.
*/
static final Logger LOGGER = LoggerFactory.getLogger(PagingOperation.class);

/**
* The page size
*/
private int size;

/**
* The page number
*/
private int page;

/**
* Default constructor for the PagingOperation class.
*/
public PagingOperation() {
}

/**
* This method gets a page from within a given collection.
*
* @param collection a generic collection
* @return page from the provided collection
*/
public Collection<T> paginate(Collection<T> collection) {

return getPage((List<T>) collection);
}

/**
* Returns a view (not a new list) of the sourceList for the range based on page and pageSize.
*
* @param sourceList The source list to paginate
* @return a view of the source list
*/
public List<T> getPage(List<T> sourceList) {
// page number should start from 1
if (size <= 0 || page <= 0) {
throw new IllegalArgumentException("invalid page size: " + size);
}

int fromIndex = (page - 1) * size;

if (CollectionUtility.isNullOrEmpty(sourceList) || sourceList.size() < fromIndex) {
return Collections.emptyList();
}

// toIndex exclusive
return sourceList.subList(fromIndex, Math.min(fromIndex + size, sourceList.size()));
}

/**
* The getter for size.
*
* @return the size
*/
public int getSize() {
return size;
}

/**
* The setter for the size.
*
* @param size the size to set
*/
public void setSize(int size) {
this.size = size;
}

/**
* The getter for the page.
*
* @return the page
*/
public int getPage() {
return page;
}

/**
* The setter for the page.
*
* @param page the page to set
*/
public void setPage(int page) {
this.page = page;
}
}