2 * Copyright (C) 2012 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.api
.service
.pager
;
11 import java
.util
.List
;
13 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.AbstractPagerImpl
;
16 * @see also class <code>eu.etaxonomy.cdm.remote.controller.util.PagerParameter</code>
18 * @author a.kohlbecker
19 \* @since Dec 10, 2012
22 public class PagerUtils
{
25 * Returns the start index for the given page size and page index values
26 * by multiplying both values (startFor = pageSize * pageIndex).<BR>
27 * If page size is <code>null</code>, either <code>null</code> is returned
28 * or an exception is thrown if page index is > 0.
29 * If page index is null it is assumed as 0.
31 * @param pageSize the page size
32 * @param pageIndex the page index
33 * @return the start index
34 * @throws PagerRangeException if pageSize == null and pageIndex > 0
36 public static Integer
startFor(Integer pageSize
, Integer pageIndex
) throws PagerRangeException
{
37 if(pageIndex
== null) {
42 throw new PagerRangeException("Page index must be undefined or 0 for undefined page size but was " + pageIndex
);
47 return pageSize
* pageIndex
;
51 * Returns the limit value as used in SQL/HQL statements for the given
53 * Earlier, for <code>null</code> the value 0 was returned but as
54 * it was decided that pageSize = <code>null</code> should not set any limitation
55 * it was changed to return value <code>null</code>. This way the current implementation
56 * is the identity function and has no effect except for documentation.
57 * <code>null</code> should be handled in dao-s or specialized code such as
58 * {@link #pageList(List, Integer, Integer)}.
63 public static Integer
limitFor(Integer pageSize
) {
72 * The original method {@link AbstractPagerImpl#hasResultsInRange(Long, Integer, Integer)} is
73 * hard to find, therefore this method has been wired also into this Utility class
74 * this this is looked up more frequently.
76 * @param numberOfResults
81 public static boolean hasResultsInRange(Long numberOfResults
, Integer pageIndex
, Integer pageSize
) {
82 return AbstractPagerImpl
.hasResultsInRange(numberOfResults
, pageIndex
, pageSize
);
86 * Returns a synchronized (sub)list of fullList which contains only the items
89 * @param pageIndex page index
90 * @param pageSize page size
91 * @return a synchronized list holding the page items
93 public static <T
extends Object
> List
<T
> pageList(List
<T
> fullList
, Integer pageIndex
, Integer pageSize
){
94 Integer start
= startFor(pageSize
, pageIndex
);
98 Integer limit
= limitFor(pageSize
); //no effect and never null at this point due to startFor semantics
99 limit
= Math
.min(Integer
.MAX_VALUE
- start
, limit
);
101 if (fullList
.size() > start
) {
102 Integer toIndex
= Math
.min(fullList
.size(), start
+ limit
);
103 return fullList
.subList(start
, toIndex
);
105 return fullList
.subList(fullList
.size(), fullList
.size()); //empty list