Author: athimel Date: 2014-05-22 10:43:16 +0200 (Thu, 22 May 2014) New Revision: 3127 Url: http://forge.nuiton.org/projects/topia/repository/revisions/3127 Log: refs #3208 Pagination* classes moved to nuiton-utils Removed: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationOrder.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationParameter.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationResult.java trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/pager/PaginationParameterTest.java trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/pager/PaginationResultTest.java Modified: trunk/pom.xml Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2014-05-21 22:10:11 UTC (rev 3126) +++ trunk/pom.xml 2014-05-22 08:43:16 UTC (rev 3127) @@ -240,7 +240,7 @@ <!-- libs version --> <eugeneVersion>2.9</eugeneVersion> - <nuitonUtilsVersion>3.0-rc-2</nuitonUtilsVersion> + <nuitonUtilsVersion>3.0-SNAPSHOT</nuitonUtilsVersion> <nuitonCsvVersion>3.0-rc-1</nuitonCsvVersion> <nuitonDecoratorVersion>3.0-alpha-3</nuitonDecoratorVersion> <nuitonI18nVersion>3.0</nuitonI18nVersion> Deleted: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationOrder.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationOrder.java 2014-05-21 22:10:11 UTC (rev 3126) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationOrder.java 2014-05-22 08:43:16 UTC (rev 3127) @@ -1,63 +0,0 @@ -package org.nuiton.topia.persistence.pager; - -/* - * #%L - * ToPIA :: Persistence - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2014 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; - -/** - * This class represents an 'order' information : order clause and asc/desc - * - * @author Arnaud Thimel (Code Lutin) - * @since 3.0 - */ -public class PaginationOrder implements Serializable { - - private static final long serialVersionUID = -1222944258030026951L; - - protected String clause; - protected boolean desc; - - public PaginationOrder(String clause, boolean desc) { - this.clause = clause; - this.desc = desc; - } - - public String getClause() { - return clause; - } - - public void setClause(String clause) { - this.clause = clause; - } - - public boolean isDesc() { - return desc; - } - - public void setDesc(boolean desc) { - this.desc = desc; - } - -} Deleted: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationParameter.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationParameter.java 2014-05-21 22:10:11 UTC (rev 3126) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationParameter.java 2014-05-22 08:43:16 UTC (rev 3127) @@ -1,182 +0,0 @@ -package org.nuiton.topia.persistence.pager; - -/* - * #%L - * ToPIA :: Persistence - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2014 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; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; - -/** - * This class represents the necessary information to do pagination (page number, size, ...). - * - * @author Arnaud Thimel (Code Lutin) - * @since 3.0 - */ -public class PaginationParameter implements Serializable { - - private static final long serialVersionUID = -2564463476779099397L; - - protected int pageNumber; - protected int pageSize; - protected List<PaginationOrder> orderClauses; - - private PaginationParameter(int pageNumber, int pageSize) { - this(pageNumber, pageSize, new LinkedList<PaginationOrder>()); - } - - private PaginationParameter(int pageNumber, int pageSize, List<PaginationOrder> orderClauses) { - this.pageNumber = pageNumber; - this.pageSize = pageSize; - this.orderClauses = Collections.unmodifiableList(orderClauses); - } - - public static PaginationParameter of(int pageNumber, int pageSize) { - PaginationParameter result = new PaginationParameter(pageNumber, pageSize); - return result; - } - - public static PaginationParameter of(int pageNumber, int pageSize, - String orderClause1, boolean orderDesc1) { - return builder(pageNumber, pageSize) - .addOrder(orderClause1, orderDesc1) - .build(); - } - - public static PaginationParameter of(int pageNumber, int pageSize, - String orderClause1, boolean orderDesc1, - String orderClause2, boolean orderDesc2) { - return builder(pageNumber, pageSize) - .addOrder(orderClause1, orderDesc1) - .addOrder(orderClause2, orderDesc2) - .build(); - } - - public static PaginationParameter of(int pageNumber, int pageSize, - String orderClause1, boolean orderDesc1, - String orderClause2, boolean orderDesc2, - String orderClause3, boolean orderDesc3) { - return builder(pageNumber, pageSize) - .addOrder(orderClause1, orderDesc1) - .addOrder(orderClause2, orderDesc2) - .addOrder(orderClause3, orderDesc3) - .build(); - } - - public static PaginationParameter.Builder builder(int pageNumber, int pageSize) { - Builder result = new Builder(pageNumber, pageSize); - return result; - } - - public int getPageNumber() { - return pageNumber; - } - - public int getPageSize() { - return pageSize; - } - - public List<PaginationOrder> getOrderClauses() { - return orderClauses; - } - - public int getStartIndex() { - if (pageNumber != 0) { - Preconditions.checkState(pageSize != -1, "This is non-sense to have pageNumber>1 if pageSize==-1"); - } - int startIndex = pageNumber * pageSize; - return startIndex; - } - - public int getEndIndex() { - int endIndex = Integer.MAX_VALUE; - if (pageSize != -1) { - endIndex = getStartIndex() + pageSize - 1; - } - return endIndex; - } - - /** - * Class used to build an instance of PaginationParameter - */ - public static class Builder { - - protected int pageNumber; - protected int pageSize; - protected List<PaginationOrder> orderClauses; - - public Builder(int pageNumber, int pageSize) { - this.pageNumber = pageNumber; - this.pageSize = pageSize; - } - - public Builder addOrder(String clause, boolean desc) { - if (orderClauses == null) { - orderClauses = Lists.newLinkedList(); - } - PaginationOrder paginationOrder = new PaginationOrder(clause, desc); - orderClauses.add(paginationOrder); - return this; - } - - public Builder addAscOrder(String clause) { - return addOrder(clause, false); - } - - public Builder addDescOrder(String clause) { - return addOrder(clause, true); - } - - public Builder addOrder(String clause) { - boolean desc = false; - String cleanedClause = clause; - int spaceIndex = clause.indexOf(' '); - if (spaceIndex != -1) { - cleanedClause = clause.substring(0, spaceIndex).trim(); - desc = "desc".equalsIgnoreCase(clause.substring(spaceIndex + 1).trim()); - } - return addOrder(cleanedClause, desc); - } - - public Builder addOrderClauses(Iterable<PaginationOrder> clauses) { - if (orderClauses == null) { - orderClauses = Lists.newLinkedList(); - } - if (clauses != null) { - Iterables.addAll(orderClauses, clauses); - } - return this; - } - - public PaginationParameter build() { - PaginationParameter result = new PaginationParameter(pageNumber, pageSize, orderClauses); - return result; - } - } -} Deleted: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationResult.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationResult.java 2014-05-21 22:10:11 UTC (rev 3126) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/pager/PaginationResult.java 2014-05-22 08:43:16 UTC (rev 3127) @@ -1,127 +0,0 @@ -package org.nuiton.topia.persistence.pager; - -/* - * #%L - * ToPIA :: Persistence - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2014 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; -import java.util.List; - -import com.google.common.base.Preconditions; - -/** - * Represents the result of a pagination request. It contains the result elements together with the - * {code}PaginationParameter{/code} used to compute it. - * - * @author Arnaud Thimel (Code Lutin) - * @since 3.0 - */ -public class PaginationResult<K> implements Serializable { - - private static final long serialVersionUID = -8794603203860852003L; - - protected List<K> elements; - protected long count; - protected PaginationParameter currentPage; - - private PaginationResult(List<K> elements, long count, PaginationParameter currentPage) { - this.elements = elements; - this.count = count; - this.currentPage = currentPage; - } - - public static <E> PaginationResult<E> of(List<E> elements, long count, PaginationParameter currentPage) { - PaginationResult<E> result = new PaginationResult<E>(elements, count, currentPage); - return result; - } - - public List<K> getElements() { - return elements; - } - - public long getCount() { - return count; - } - - public PaginationParameter getCurrentPage() { - return currentPage; - } - - public PaginationParameter getNextPage() { - int nextPageNumber = currentPage.getPageNumber() + 1; - int pageSize = currentPage.getPageSize(); - List<PaginationOrder> orderClauses = currentPage.getOrderClauses(); - PaginationParameter result = PaginationParameter. - builder(nextPageNumber, pageSize). - addOrderClauses(orderClauses). - build(); - return result; - } - - public PaginationParameter getPreviousPage() { - // XXX AThimel 21/05/14 Maybe, do not fail, just return the first page ? - Preconditions.checkState(currentPage.getPageNumber() > 0, "You cannot get a previous page to the first one"); - int previousPageNumber = currentPage.getPageNumber() - 1; - int pageSize = currentPage.getPageSize(); - List<PaginationOrder> orderClauses = currentPage.getOrderClauses(); - PaginationParameter result = PaginationParameter. - builder(previousPageNumber, pageSize). - addOrderClauses(orderClauses). - build(); - return result; - } - - public PaginationParameter getFirstPage() { - int firstPageNumber = 0; - int pageSize = currentPage.getPageSize(); - List<PaginationOrder> orderClauses = currentPage.getOrderClauses(); - PaginationParameter result = PaginationParameter. - builder(firstPageNumber, pageSize). - addOrderClauses(orderClauses). - build(); - return result; - } - - public PaginationParameter getLastPage() { - int lastPageNumber = getPageCount() - 1; - int pageSize = currentPage.getPageSize(); - List<PaginationOrder> orderClauses = currentPage.getOrderClauses(); - PaginationParameter result = PaginationParameter. - builder(lastPageNumber, pageSize). - addOrderClauses(orderClauses). - build(); - return result; - } - - public int getPageCount() { - int pageCount = 1; - int pageSize = currentPage.getPageSize(); - if (pageSize > 1) { - double countDouble = Long.valueOf(count).doubleValue(); - double pageSizeDouble = Integer.valueOf(pageSize).doubleValue(); - double pageNumberDouble = Math.ceil(countDouble / pageSizeDouble); - pageCount = Double.valueOf(pageNumberDouble).intValue(); - } - return pageCount; - } -} Deleted: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/pager/PaginationParameterTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/pager/PaginationParameterTest.java 2014-05-21 22:10:11 UTC (rev 3126) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/pager/PaginationParameterTest.java 2014-05-22 08:43:16 UTC (rev 3127) @@ -1,75 +0,0 @@ -package org.nuiton.topia.persistence.pager; - -/* - * #%L - * ToPIA :: Persistence - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2014 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.junit.Assert; -import org.junit.Test; - -/** - * @author Arnaud Thimel (Code Lutin) - */ -public class PaginationParameterTest { - - @Test - public void testIndexes() { - { - PaginationParameter paginationParameter = PaginationParameter.of(0, 50); - Assert.assertEquals(0, paginationParameter.getStartIndex()); - Assert.assertEquals(49, paginationParameter.getEndIndex()); - } - { - PaginationParameter paginationParameter = PaginationParameter.of(2, 50); - Assert.assertEquals(100, paginationParameter.getStartIndex()); - Assert.assertEquals(149, paginationParameter.getEndIndex()); - } - { - PaginationParameter paginationParameter = PaginationParameter.of(0, -1); - Assert.assertEquals(0, paginationParameter.getStartIndex()); - Assert.assertEquals(Integer.MAX_VALUE, paginationParameter.getEndIndex()); - } - { - PaginationParameter paginationParameter = PaginationParameter.of(0, 19); - Assert.assertEquals(0, paginationParameter.getStartIndex()); - Assert.assertEquals(18, paginationParameter.getEndIndex()); - } - { - PaginationParameter paginationParameter = PaginationParameter.of(1, 19); - Assert.assertEquals(19, paginationParameter.getStartIndex()); - Assert.assertEquals(37, paginationParameter.getEndIndex()); - } - { - PaginationParameter paginationParameter = PaginationParameter.of(2, 19); - Assert.assertEquals(38, paginationParameter.getStartIndex()); - Assert.assertEquals(56, paginationParameter.getEndIndex()); - } - } - - @Test(expected = IllegalStateException.class) - public void testInvalidIndex() { - PaginationParameter paginationParameter = PaginationParameter.of(2, -1); - paginationParameter.getStartIndex(); - } - -} Deleted: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/pager/PaginationResultTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/pager/PaginationResultTest.java 2014-05-21 22:10:11 UTC (rev 3126) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/pager/PaginationResultTest.java 2014-05-22 08:43:16 UTC (rev 3127) @@ -1,90 +0,0 @@ -package org.nuiton.topia.persistence.pager; - -/* - * #%L - * ToPIA :: Persistence - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2014 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.util.List; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.google.common.collect.Lists; - -/** - * @author Arnaud Thimel (Code Lutin) - */ -public class PaginationResultTest { - - protected PaginationResult<Object> paginationResult; - - @Before - public void init() { - List<Object> elements = Lists.newArrayList(); - PaginationParameter page = PaginationParameter.of(2, 50); - paginationResult = PaginationResult.of(elements, 204, page); - } - - @Test - public void testGetFirstPage() { - Assert.assertEquals(0, paginationResult.getFirstPage().getPageNumber()); - Assert.assertEquals(50, paginationResult.getFirstPage().getPageSize()); - Assert.assertEquals(0, paginationResult.getFirstPage().getStartIndex()); - Assert.assertEquals(49, paginationResult.getFirstPage().getEndIndex()); - } - - @Test - public void testGetPreviousPage() { - Assert.assertEquals(1, paginationResult.getPreviousPage().getPageNumber()); - Assert.assertEquals(50, paginationResult.getPreviousPage().getPageSize()); - Assert.assertEquals(50, paginationResult.getPreviousPage().getStartIndex()); - Assert.assertEquals(99, paginationResult.getPreviousPage().getEndIndex()); - } - - @Test - public void testCurrentPage() { - Assert.assertEquals(5, paginationResult.getPageCount()); - Assert.assertEquals(2, paginationResult.getCurrentPage().getPageNumber()); - Assert.assertEquals(50, paginationResult.getCurrentPage().getPageSize()); - Assert.assertEquals(100, paginationResult.getCurrentPage().getStartIndex()); - Assert.assertEquals(149, paginationResult.getCurrentPage().getEndIndex()); - } - - @Test - public void testGeNextPage() { - Assert.assertEquals(3, paginationResult.getNextPage().getPageNumber()); - Assert.assertEquals(50, paginationResult.getNextPage().getPageSize()); - Assert.assertEquals(150, paginationResult.getNextPage().getStartIndex()); - Assert.assertEquals(199, paginationResult.getNextPage().getEndIndex()); - } - - @Test - public void testGetLastPage() { - Assert.assertEquals(4, paginationResult.getLastPage().getPageNumber()); - Assert.assertEquals(50, paginationResult.getLastPage().getPageSize()); - Assert.assertEquals(200, paginationResult.getLastPage().getStartIndex()); - Assert.assertEquals(249, paginationResult.getLastPage().getEndIndex()); - } - -}