Project

General

Profile

Download (3.78 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2012 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.cdm.api.service.pager;
10

    
11
import java.util.List;
12

    
13
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
14

    
15
/**
16
 * @see also class <code>eu.etaxonomy.cdm.remote.controller.util.PagerParameter</code>
17
 *
18
 * @author a.kohlbecker
19
 * @since Dec 10, 2012
20
 */
21
public class PagerUtils {
22

    
23
    /**
24
     * Returns the start index for the given page size and page index values
25
     * by multiplying both values (startFor = pageSize * pageIndex).<BR>
26
     * If page size is <code>null</code>, either <code>null</code> is returned
27
     * or an exception is thrown if page index is > 0.
28
     * If page index is null it is assumed as 0.
29
     *
30
     * @param pageSize the page size
31
     * @param pageIndex the page index
32
     * @return the start index
33
     * @throws PagerRangeException if pageSize == null and pageIndex > 0
34
     */
35
    public static Integer startFor(Integer pageSize, Integer pageIndex) throws PagerRangeException {
36
        if(pageIndex == null) {
37
            pageIndex = 0;
38
        }
39
        if(pageSize == null){
40
            if (pageIndex > 0 ){
41
                throw new PagerRangeException("Page index must be undefined or 0 for undefined page size but was " + pageIndex);
42
            }else{
43
                return null;
44
            }
45
        }
46
        return pageSize * pageIndex;
47
    }
48

    
49
    /**
50
     * Returns the limit value as used in SQL/HQL statements for the given
51
     * page size.<BR>
52
     * Earlier, for <code>null</code> the value 0 was returned but as
53
     * it was decided that pageSize = <code>null</code> should not set any limitation
54
     * it was changed to return value <code>null</code>. This way the current implementation
55
     * is the identity function and has no effect except for documentation.
56
     * <code>null</code> should be handled in dao-s or specialized code such as
57
     * {@link #pageList(List, Integer, Integer)}.
58
     *
59
     * @param pageSize
60
     * @return
61
     */
62
    public static Integer limitFor(Integer pageSize) {
63
        if(pageSize == null){
64
            return null;
65
        }else{
66
            return pageSize;
67
        }
68
    }
69

    
70
    /**
71
     * The original method {@link AbstractPagerImpl#hasResultsInRange(Long, Integer, Integer)} is
72
     * hard to find, therefore this method has been wired also into this Utility class
73
     * this this is looked up more frequently.
74
     *
75
     * @param numberOfResults
76
     * @param pageIndex
77
     * @param pageSize
78
     * @return
79
     */
80
    public static boolean hasResultsInRange(Long numberOfResults, Integer pageIndex, Integer pageSize) {
81
        return AbstractPagerImpl.hasResultsInRange(numberOfResults, pageIndex, pageSize);
82
    }
83

    
84
    /**
85
     * Returns a synchronized (sub)list of fullList which contains only the items
86
     * for the given page
87
     * @param fullList
88
     * @param pageIndex page index
89
     * @param pageSize page size
90
     * @return a synchronized list holding the page items
91
     */
92
    public static <T extends Object> List<T> pageList(List<T> fullList, Integer pageIndex, Integer pageSize){
93
        Integer start = startFor(pageSize, pageIndex);
94
        if (start == null){
95
            return fullList;
96
        }
97
        Integer limit = limitFor(pageSize); //no effect and never null at this point due to startFor semantics
98
        limit = Math.min(Integer.MAX_VALUE - start, limit);
99

    
100
        if (fullList.size() > start) {
101
            Integer toIndex = Math.min(fullList.size(), start + limit);
102
            return fullList.subList(start, toIndex);
103
        }else{
104
            return fullList.subList(fullList.size(), fullList.size()); //empty list
105
        }
106
    }
107
}
(3-3/3)