javastrava.util.PagingUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javastrava-api Show documentation
Show all versions of javastrava-api Show documentation
Java implementation of the Strava API
package javastrava.util;
import java.util.ArrayList;
import java.util.List;
import javastrava.config.Messages;
import javastrava.config.StravaConfig;
/**
*
* Utilities for handling Strava paging
*
* @author Dan Shannon
*
*/
public class PagingUtils {
/**
*
* Utility method - give it any paging instruction and it will return a list of paging instructions that will work with the Strava API (i.e. that conform to
* maximum page sizes etc.)
*
*
* @param inputPaging
* The paging instruction to be converted
* @return List of Strava paging instructions that can be given to the Strava engine
*/
public static List convertToStravaPaging(final Paging inputPaging) {
PagingUtils.validatePagingArguments(inputPaging);
final List stravaPaging = new ArrayList();
if (inputPaging == null) {
stravaPaging.add(new Paging(Integer.valueOf(1), StravaConfig.DEFAULT_PAGE_SIZE));
return stravaPaging;
}
if (inputPaging.getPage().intValue() == 0) {
inputPaging.setPage(Integer.valueOf(1));
}
if (inputPaging.getPageSize().intValue() == 0) {
inputPaging.setPageSize(StravaConfig.DEFAULT_PAGE_SIZE);
}
// If it's already valid for Strava purposes, just use that
if (inputPaging.getPageSize().intValue() <= StravaConfig.MAX_PAGE_SIZE.intValue()) {
stravaPaging.add(inputPaging);
return stravaPaging;
}
// Calculate the first and last elements to be returned
final int lastElement = (inputPaging.getPage().intValue() * inputPaging.getPageSize().intValue()) - inputPaging.getIgnoreLastN();
final int firstElement = ((inputPaging.getPage().intValue() - 1) * inputPaging.getPageSize().intValue()) + inputPaging.getIgnoreFirstN() + 1;
// If the last element fits in one page, return one page
if (lastElement <= StravaConfig.MAX_PAGE_SIZE.intValue()) {
stravaPaging.add(new Paging(Integer.valueOf(1), Integer.valueOf(lastElement), inputPaging.getIgnoreFirstN(), 0));
return stravaPaging;
}
// Otherwise, return a series of pages
int currentPage = 0;
for (int i = 1; i <= lastElement; i = i + StravaConfig.MAX_PAGE_SIZE.intValue()) {
currentPage++;
if (((currentPage * StravaConfig.MAX_PAGE_SIZE.intValue())) >= firstElement) {
final int ignoreLastN = Math.max(0, (currentPage * StravaConfig.MAX_PAGE_SIZE.intValue()) - lastElement);
final int ignoreFirstN = Math.max(0, firstElement - ((currentPage - 1) * StravaConfig.MAX_PAGE_SIZE.intValue()) - 1);
stravaPaging.add(new Paging(Integer.valueOf(currentPage), StravaConfig.MAX_PAGE_SIZE, ignoreFirstN, ignoreLastN));
}
}
return stravaPaging;
}
/**
*
* Removes the first N items from a list
*
*
* @param list
* List of items
* @param ignoreFirstN
* Number of items to remove
* @param
* The class of object in the list
* @return The resulting list
*/
public static List ignoreFirstN(final List list, final int ignoreFirstN) {
if (ignoreFirstN < 0) {
throw new IllegalArgumentException(Messages.string("PagingUtils.cannotRemove") + ignoreFirstN + Messages.string("PagingUtils.itemsFromAList")); //$NON-NLS-1$ //$NON-NLS-2$
}
if (list == null) {
return null;
}
if (ignoreFirstN == 0) {
return list;
}
if (ignoreFirstN >= list.size()) {
return new ArrayList();
}
// return list.subList(ignoreFirstN, list.size() - 1);
final ArrayList returnList = new ArrayList();
for (int i = ignoreFirstN; i < list.size(); i++) {
returnList.add(list.get(i));
}
return returnList;
}
/**
*
* Removes the last ignoreLastN items from the list
*
*
* @param list
* List of items
* @param ignoreLastN
* Number of items to remove
* @param
* The class of the objects contained in the list
* @return The resulting list
*/
public static List ignoreLastN(final List list, final int ignoreLastN) {
if (ignoreLastN < 0) {
throw new IllegalArgumentException(Messages.string("PagingUtils.cannotRemove") + ignoreLastN + Messages.string("PagingUtils.itemsFromAList")); //$NON-NLS-1$ //$NON-NLS-2$
}
if (list == null) {
return null;
}
if (ignoreLastN == 0) {
return list;
}
if (ignoreLastN >= list.size()) {
return new ArrayList();
}
return list.subList(0, list.size() - ignoreLastN);
}
/**
*
* Throw an IllegalArgumentException if the page or perPage parameters are set but are invalid
*
*
* @param pagingInstruction
* The page to be returned
*/
public static void validatePagingArguments(final Paging pagingInstruction) {
if (pagingInstruction == null) {
return;
}
if (pagingInstruction.getPage().intValue() < 0) {
throw new IllegalArgumentException(Messages.string("PagingUtils.pageArgumentTooLow")); //$NON-NLS-1$
}
if (pagingInstruction.getPageSize().intValue() < 0) {
throw new IllegalArgumentException(Messages.string("PagingUtils.perPageArgumentTooLow")); //$NON-NLS-1$
}
if ((pagingInstruction.getIgnoreLastN() > 0) && (pagingInstruction.getIgnoreLastN() > pagingInstruction.getPageSize().intValue())) {
throw new IllegalArgumentException(Messages.string("PagingUtils.IgnoreTooHigh")); //$NON-NLS-1$
}
if ((pagingInstruction.getIgnoreFirstN() > 0) && (pagingInstruction.getIgnoreFirstN() > pagingInstruction.getPageSize().intValue())) {
throw new IllegalArgumentException(Messages.string("PagingUtils.IgnoreTooHigh")); //$NON-NLS-1$
}
}
}