Generic stateless paginator component for Java
After searching for a generic (without third-party dependencies) pagination component for several hours, I decided to start my own from scratch. After a few days I have a workable version of it:
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import com.github.timoteoponce.paginator.ListProvider;
import com.github.timoteoponce.paginator.Paginator;
import com.github.timoteoponce.paginator.Segment;
import com.github.timoteoponce.paginator.SegmentProvider;
/**
*
* @author Timoteo Ponce
*/
public class TestPaginator {
private static final Logger log = Logger.getAnonymousLogger();
public static void main(final String[] args) {
Paginator<String> paginator = new Paginator<String>(new MyListProvider(), 3);
// "Akira", "Edward", "Alphonse"
log.info("Page 1: " + paginator.getList());
paginator.goNextPage();
// "Mustang", "Hawkeye", "Lusty"
log.info("Page 2: " + paginator.getList());
}
private static class MyListProvider implements ListProvider<String> {
List<String> sourceList = Arrays.asList("Akira", "Edward", "Alphonse", "Mustang", "Hawkeye", "Lusty", "Gluton", "Envy", "Lust", "Wrath");
/*
* (non-Javadoc)
*
* @see org.timo.paginator.ListProvider#provideList(org.timo.paginator.SegmentProvider)
*/
@Override
public List<String> provideList(final SegmentProvider segmentProvider) {
// we need the total source list size for calculations and checkings
Segment segment = segmentProvider.getSegment(sourceList.size());
return sourceList.subList(segment.getFromIndex(), segment.getToIndex());
}
}
}
As you can see, it's very simple to use and you can extend it as much as you want to. The core of the component is the pages-size-limits calculation defined in the class PaginationData.
Maven setup:
<dependency>
<groupId>com.github.timoteoponce</groupId>
<artifactId>paginator</artifactId>
<version>0.0.5</version>
</dependency>
Examples:
JavaDoc