r2223 - in trunk/nuiton-utils/src: main/java/org/nuiton/util test/java/org/nuiton/util
Author: tchemit Date: 2011-11-09 12:19:23 +0100 (Wed, 09 Nov 2011) New Revision: 2223 Url: http://nuiton.org/repositories/revision/nuiton-utils/2223 Log: Evolution #1799: Add a PagerUtil class Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerUtilTest.java Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java (rev 0) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java 2011-11-09 11:19:23 UTC (rev 2223) @@ -0,0 +1,109 @@ +/* + * #%L + * Nuiton Utils :: Nuiton Utils + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2011 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.util; + +import org.apache.commons.lang3.tuple.Pair; + +import java.util.ArrayList; +import java.util.List; + +/** + * Common methods to deal with a pager. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.4 + */ +public class PagerUtil { + + /** + * Given a total count of elements and a page size, compute the number of + * pages available. + * + * @param totalCount total count of elements in the pager + * @param pageSize size of a page + * @return the number of available pages + */ + public static int getTotalPage(int totalCount, int pageSize) { + int result = (int) Math.ceil((double) totalCount / (double) pageSize); + return result; + } + + /** + * Compute the pager bound given his datas: + * <ul> + * <li>{@code totalCount}: count of all elements of the pager</li> + * <li>{@code pageSize}: number of elements in a page</li> + * <li>{@code page}: the requested page number starting at {@code 1}</li> + * </ul> + * + * @param totalCount total count of elements in the pager + * @param page pager page number (starting at {@code 1}) + * @param pageSize number of elements in a page + * @return the bound of start and end index of the requested elements of the pager + */ + public static Pair<Integer, Integer> getPageBound(int totalCount, + int page, + int pageSize) { + + page = Math.max(page, 1); + pageSize = Math.max(pageSize, 1); + + int start = (page - 1) * pageSize; + int end = page * pageSize; // End in subList is exclusive + + end = Math.min(end, totalCount); //To be sure that end will not be out of bound in subList + + Pair<Integer, Integer> result = Pair.of(start, end); + return result; + } + + /** + * Get the elements of the lists using the pager datas: + * <p/> + * <ul> + * <li>{@code elements}:all elements of the pager</li> + * <li>{@code pageSize}: number of elements in a page</li> + * <li>{@code page}: the requested page number starting at {@code 1}</li> + * </ul> + * + * @param elements all pager elements + * @param page pager page number (starting at {@code 1}) + * @param pageSize number of elements in a page + * @param <E> n'importe quel type + * @return la liste une fois filtrée + */ + public static <E> List<E> getPage(List<E> elements, int page, int pageSize) { + + Pair<Integer, Integer> bound = getPageBound(elements.size(), page, pageSize); + + int start = bound.getLeft(); + int end = bound.getRight(); + + List<E> subList = elements.subList(start, end); + List<E> result = new ArrayList<E>(subList); + + return result; + } +} Property changes on: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerUtilTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerUtilTest.java (rev 0) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerUtilTest.java 2011-11-09 11:19:23 UTC (rev 2223) @@ -0,0 +1,63 @@ +/* + * #%L + * Nuiton Utils :: Nuiton Utils + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2011 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 + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.util; + +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests the {@link PagerUtil}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.4 + */ +public class PagerUtilTest { + + @Test + public void getPageBound() throws Exception { + + Pair<Integer, Integer> actualBound; + + // page size is greater than total count, only returns that is available + actualBound = PagerUtil.getPageBound(10, 1, 11); + Assert.assertEquals(Pair.of(0, 10), actualBound); + + } + + @Test + public void getTotalPage() throws Exception { + int totalPage; + + totalPage = PagerUtil.getTotalPage(9, 10); + Assert.assertEquals(1, totalPage); + + totalPage = PagerUtil.getTotalPage(10, 10); + Assert.assertEquals(1, totalPage); + + totalPage = PagerUtil.getTotalPage(11, 10); + Assert.assertEquals(2, totalPage); + } +} Property changes on: trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerUtilTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.nuiton.org