r2416 - in trunk/nuiton-utils/src: main/java/org/nuiton/util test/java/org/nuiton/util
Author: tchemit Date: 2012-09-14 11:48:24 +0200 (Fri, 14 Sep 2012) New Revision: 2416 Url: http://nuiton.org/repositories/revision/nuiton-utils/2416 Log: fixes #2312: Introduce PagerBean and PagerBeanUtil (replace PagerUtil) Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBean.java trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBeanUtil.java trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerBeanUtilTest.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBean.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBean.java (rev 0) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBean.java 2012-09-14 09:48:24 UTC (rev 2416) @@ -0,0 +1,106 @@ +package org.nuiton.util; + +/* + * #%L + * Nuiton Utils :: Nuiton Utils + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 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% + */ + +import java.io.Serializable; + +/** + * A pager bean. + * <p/> + * Was previously {@code org.nuiton.util.PagerUtil.PagerBean}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.7 + */ +public class PagerBean implements Serializable { + + private static final long serialVersionUID = 1L; + + /** Number of records (says numbers of rows displayable. */ + protected long records; + + /** Index of the first record in this page. */ + protected long recordStartIndex; + + /** Index of the last record in this page. */ + protected long recordEndIndex; + + /** Index of the page. */ + protected int pageIndex; + + /** Page size, says number of records in a page. */ + protected int pageSize; + + /** Number of pages. */ + protected long pagesNumber; + + public long getRecords() { + return records; + } + + public long getRecordStartIndex() { + return recordStartIndex; + } + + public long getRecordEndIndex() { + return recordEndIndex; + } + + public int getPageIndex() { + return pageIndex; + } + + public int getPageSize() { + return pageSize; + } + + public long getPagesNumber() { + return pagesNumber; + } + + public void setRecords(long records) { + this.records = records; + } + + public void setRecordStartIndex(long recordStartIndex) { + this.recordStartIndex = recordStartIndex; + } + + public void setRecordEndIndex(long recordEndIndex) { + this.recordEndIndex = recordEndIndex; + } + + public void setPageIndex(int pageIndex) { + this.pageIndex = pageIndex; + } + + public void setPageSize(int pageSize) { + this.pageSize = pageSize; + } + + public void setPagesNumber(long pagesNumber) { + this.pagesNumber = pagesNumber; + } +} Property changes on: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBean.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBeanUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBeanUtil.java (rev 0) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBeanUtil.java 2012-09-14 09:48:24 UTC (rev 2416) @@ -0,0 +1,133 @@ +package org.nuiton.util; + +/* + * #%L + * Nuiton Utils :: Nuiton Utils + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 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% + */ + +import org.apache.commons.lang3.tuple.Pair; + +import java.util.ArrayList; +import java.util.List; + +/** + * Useful method around {@link PagerBean}. + * <p/> + * Was previously {@code org.nuiton.util.PagerUtil}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.7 + */ +public class PagerBeanUtil { + + /** + * 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 long getTotalPage(long totalCount, int pageSize) { + long result = (long) 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<Long, Long> getPageBound(long totalCount, + int page, + int pageSize) { + + page = Math.max(page, 1); + pageSize = Math.max(pageSize, 1); + + long start = (page - 1l) * (long)pageSize; + long end = page * (long)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<Long, Long> 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<Long, Long> bound = getPageBound(elements.size(), page, pageSize); + + long start = bound.getLeft(); + long end = bound.getRight(); + + List<E> subList = elements.subList((int) start, (int) end); + List<E> result = new ArrayList<E>(subList); + + return result; + } + + /** + * Given a pagerBean, compute his {@link PagerBean#recordStartIndex}, + * {@link PagerBean#recordEndIndex} and {@link PagerBean#pagesNumber}. + * + * @param bean the pagerBean to fill + * @since 2.4.3 + */ + public static void computeRecordIndexesAndPagesNumber(PagerBean bean) { + long records = bean.getRecords(); + int pageSize = bean.getPageSize(); + + Pair<Long, Long> pageBound = + getPageBound(records, bean.getPageIndex(), pageSize); + bean.setRecordStartIndex(pageBound.getLeft()); + bean.setRecordEndIndex(pageBound.getRight()); + bean.setPagesNumber(getTotalPage(records, pageSize)); + } + + public static PagerBean newPagerBean() { + return new PagerBean(); + } +} Property changes on: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerBeanUtil.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java 2012-09-04 12:49:35 UTC (rev 2415) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/PagerUtil.java 2012-09-14 09:48:24 UTC (rev 2416) @@ -35,6 +35,7 @@ * * @author tchemit <chemit@codelutin.com> * @since 2.4 + * @deprecated since 2.7, prefer use now {@link PagerBeanUtil} */ public class PagerUtil { Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerBeanUtilTest.java =================================================================== --- trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerBeanUtilTest.java (rev 0) +++ trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerBeanUtilTest.java 2012-09-14 09:48:24 UTC (rev 2416) @@ -0,0 +1,68 @@ +package org.nuiton.util; + +/* + * #%L + * Nuiton Utils :: Nuiton Utils + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2012 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% + */ + +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test {@link PagerBeanUtil}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.7 + */ +public class PagerBeanUtilTest { + + @Test + public void getPageBound() throws Exception { + + Pair<Long, Long> actualBound; + + // page size is greater than total count, only returns that is available + actualBound = PagerBeanUtil.getPageBound((long) Integer.MAX_VALUE * 2l, 1, 11); + Assert.assertEquals(Pair.of(0l, 11l), actualBound); + + } + + @Test + public void getTotalPage() throws Exception { + long totalPage; + + totalPage = PagerBeanUtil.getTotalPage(9, 10); + Assert.assertEquals(1, totalPage); + + totalPage = PagerBeanUtil.getTotalPage(10, 10); + Assert.assertEquals(1, totalPage); + + totalPage = PagerBeanUtil.getTotalPage(11, 10); + Assert.assertEquals(2, totalPage); + + totalPage = PagerBeanUtil.getTotalPage( + (long) Integer.MAX_VALUE * 20, 10); + Assert.assertEquals(Integer.MAX_VALUE * 2l, totalPage); + + } +} Property changes on: trunk/nuiton-utils/src/test/java/org/nuiton/util/PagerBeanUtilTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.nuiton.org