1 /*
2 * Copyright (c) 2002-2025, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.util.html;
35
36 import java.util.List;
37
38 /**
39 * This paginator should be used with already paged list, and the items count. <strong>This implementation requires the items count and the page items</strong>
40 *
41 * @param <E>
42 * the type
43 */
44 public class DelegatePaginator<E> extends AbstractPaginator<E>
45 {
46 private static final long serialVersionUID = -3528471886593833180L;
47
48 /**
49 * Creates a new instance of Paginator.
50 *
51 * @param list
52 * The collection to paginate
53 * @param nItemPerPage
54 * Number of items to display per page
55 * @param strBaseUrl
56 * The base Url for build links on each page link
57 * @param strPageIndexParameterName
58 * The parameter name for the page index
59 * @param strPageIndex
60 * The current page index
61 * @param nItemsCount
62 * the item count
63 */
64 public DelegatePaginator( List<E> list, int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex, int nItemsCount )
65 {
66 _nItemsCount = nItemsCount;
67 _list = list;
68 _nItemPerPage = ( nItemPerPage <= 0 ) ? 1 : nItemPerPage;
69 _strBaseUrl = strBaseUrl;
70 _strPageIndexParameterName = strPageIndexParameterName;
71 _strItemsPerPageParameterName = PARAMETER_ITEMS_PER_PAGE;
72
73 try
74 {
75 _nPageCurrent = Integer.parseInt( strPageIndex );
76 }
77 catch( NumberFormatException e )
78 {
79 // strPageIndex invalid, use 1 as default page index.
80 _nPageCurrent = 1;
81 }
82
83 if ( _nPageCurrent > getPagesCount( ) )
84 {
85 _nPageCurrent = 1;
86 }
87 }
88
89 /**
90 * Creates a new instance of Paginator
91 *
92 * @param list
93 * The collection to paginate
94 * @param nItemPerPage
95 * Number of items to display per page
96 * @param strBaseUrl
97 * The base Url for build links on each page link
98 * @param strPageIndexParameterName
99 * The parameter name for the page index
100 * @param strPageIndex
101 * The current page index
102 * @param nItemsCount
103 * the item count
104 * @param strItemsPerPageParameterName
105 * The parameter name of the number items per page
106 */
107 public DelegatePaginator( List<E> list, int nItemPerPage, String strBaseUrl, String strPageIndexParameterName, String strPageIndex, int nItemsCount,
108 String strItemsPerPageParameterName )
109 {
110 this( list, nItemPerPage, strBaseUrl, strPageIndexParameterName, strPageIndex, nItemsCount );
111 _strItemsPerPageParameterName = strItemsPerPageParameterName;
112 }
113
114 /**
115 * Returns the List
116 *
117 * @return The List
118 */
119 @Override
120 public List<E> getPageItems( )
121 {
122 return _list;
123 }
124 }