Project

General

Profile

Download (3.79 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
 */
22
public class PagerUtils {
23

    
24
    /**
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.
30
     *
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
35
     */
36
    public static Integer startFor(Integer pageSize, Integer pageIndex) throws PagerRangeException {
37
        if(pageIndex == null) {
38
            pageIndex = 0;
39
        }
40
        if(pageSize == null){
41
            if (pageIndex > 0 ){
42
                throw new PagerRangeException("Page index must be undefined or 0 for undefined page size but was " + pageIndex);
43
            }else{
44
                return null;
45
            }
46
        }
47
        return pageSize *  pageIndex;
48
    }
49

    
50
    /**
51
     * Returns the limit value as used in SQL/HQL statements for the given
52
     * page size.<BR>
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)}.
59
     *
60
     * @param pageSize
61
     * @return
62
     */
63
    public static Integer limitFor(Integer pageSize) {
64
        if(pageSize == null){
65
            return null;
66
        }else{
67
            return pageSize;
68
        }
69
    }
70

    
71
    /**
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.
75
     *
76
     * @param numberOfResults
77
     * @param pageIndex
78
     * @param pageSize
79
     * @return
80
     */
81
    public static boolean hasResultsInRange(Long numberOfResults, Integer pageIndex, Integer pageSize) {
82
        return AbstractPagerImpl.hasResultsInRange(numberOfResults, pageIndex, pageSize);
83
    }
84

    
85
    /**
86
     * Returns a synchronized (sub)list of fullList which contains only the items
87
     * for the given page
88
     * @param fullList
89
     * @param pageIndex page index
90
     * @param pageSize page size
91
     * @return a synchronized list holding the page items
92
     */
93
    public  static  <T extends Object> List<T> pageList(List<T> fullList, Integer pageIndex, Integer pageSize ){
94
        Integer start = startFor(pageSize, pageIndex);
95
        if (start == null){
96
            return fullList;
97
        }
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);
100

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

    
109
}
(3-3/3)