Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
38   222   23   2,92
20   90   0,61   13
13     1,77  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  Pageable       Line # 41 38 0% 23 1 98,6% 0.9859155
 
  (22)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
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 are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * 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 OWNER 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    package net.sourceforge.pebble.util;
33   
34    import java.util.List;
35   
36    /**
37    * Helper class that implements paging over a collection.
38    *
39    * @author Simon Brown
40    */
 
41    public class Pageable<T> {
42   
43    /** the default page size */
44    public static final int DEFAULT_PAGE_SIZE = 10;
45   
46    private static final int PAGE_WINDOW = 10;
47   
48    /** the list over which this class is paging */
49    private List<T> list;
50   
51    /** the page size */
52    private int pageSize = DEFAULT_PAGE_SIZE;
53   
54    /** the current page */
55    private int page;
56   
57    /** the starting index */
58    private int startingIndex;
59   
60    /** the ending index */
61    private int endingIndex;
62   
63    /** the maximum number of pages */
64    private int maxPages;
65   
66    /**
67    * Creates a new instance with the specified list.
68    *
69    * @param list a List
70    */
 
71  22 toggle public Pageable(List<T> list) {
72  22 this.list = list;
73  22 this.page = 1;
74  22 this.maxPages = 1;
75   
76  22 calculatePages();
77    }
78   
 
79  42 toggle private void calculatePages() {
80  42 if (pageSize > 0) {
81    // calculate how many pages there are
82  42 if (list.size() % pageSize == 0) {
83  34 maxPages = list.size() / pageSize;
84    } else {
85  8 maxPages = (list.size() / pageSize) + 1;
86    }
87    }
88    }
89   
90    /**
91    * Gets the list that this instance is paging over.
92    *
93    * @return a List
94    */
 
95  6 toggle public List<T> getList() {
96  6 return this.list;
97    }
98   
99    /**
100    * Gets the subset of the list for the current page.
101    *
102    * @return a List
103    */
 
104  20 toggle public List<T> getListForPage() {
105  20 return list.subList(startingIndex, endingIndex);
106    }
107   
108    /**
109    * Gets the page size.
110    *
111    * @return the page size as an int
112    */
 
113  2 toggle public int getPageSize() {
114  2 return this.pageSize;
115    }
116   
117    /**
118    * Sets the page size.
119    *
120    * @param pageSize the page size as an int
121    */
 
122  20 toggle public void setPageSize(int pageSize) {
123  20 this.pageSize = pageSize;
124  20 calculatePages();
125    }
126   
127    /**
128    * Gets the page.
129    *
130    * @return the page as an int
131    */
 
132  60 toggle public int getPage() {
133  60 return this.page;
134    }
135   
136    /**
137    * Sets the page size.
138    *
139    * @param p the page as an int
140    */
 
141  52 toggle public void setPage(int p) {
142  52 if (p >= maxPages) {
143  18 this.page = maxPages;
144  34 } else if (p <= 1) {
145  12 this.page = 1;
146    } else {
147  22 this.page = p;
148    }
149   
150    // now work out where the sub-list should start and end
151  52 startingIndex = pageSize * (page-1);
152  52 if (startingIndex < 0) {
153  4 startingIndex = 0;
154    }
155  52 endingIndex = startingIndex + pageSize;
156  52 if (endingIndex > list.size()) {
157  10 endingIndex = list.size();
158    }
159    }
160   
161    /**
162    * Gets the maximum number of pages.
163    *
164    * @return the maximum number of pages as an int
165    */
 
166  42 toggle public int getMaxPages() {
167  42 return this.maxPages;
168    }
169   
170    /**
171    * Determines whether there is a previous page and gets the page number.
172    *
173    * @return the previous page number, or zero
174    */
 
175  22 toggle public int getPreviousPage() {
176  22 if (page > 1) {
177  6 return page-1;
178    } else {
179  16 return 0;
180    }
181    }
182   
183    /**
184    * Determines whether there is a next page and gets the page number.
185    *
186    * @return the next page number, or 0
187    */
 
188  22 toggle public int getNextPage() {
189  22 if (page < maxPages) {
190  8 return page+1;
191    } else {
192  14 return 0;
193    }
194    }
195   
196    /**
197    * Gets the minimum page in the window.
198    *
199    * @return the page number
200    */
 
201  14 toggle public int getMinPageRange() {
202  14 if (getPage() > PAGE_WINDOW) {
203  6 return getPage() - PAGE_WINDOW;
204    } else {
205  8 return 1;
206    }
207    }
208   
209    /**
210    * Gets the maximum page in the window.
211    *
212    * @return the page number
213    */
 
214  12 toggle public int getMaxPageRange() {
215  12 if (getPage() < (getMaxPages() - PAGE_WINDOW)) {
216  4 return getPage() + PAGE_WINDOW;
217    } else {
218  8 return getMaxPages();
219    }
220    }
221   
222    }