1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
package net.smartlab.web.page; |
25 |
|
|
26 |
|
import java.util.Iterator; |
27 |
|
import java.util.NoSuchElementException; |
28 |
|
|
29 |
|
import org.apache.commons.logging.Log; |
30 |
|
import org.apache.commons.logging.LogFactory; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
@author |
36 |
|
|
|
|
| 0% |
Uncovered Elements: 115 (115) |
Complexity: 45 |
Complexity Density: 0,75 |
|
37 |
|
public abstract class Paginator implements Iterator { |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
public final static int UNLIMITED_PAGES = 0; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
public final static int UNLIMITED_ITEMS = 0; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
public final static int UNDEFINED_PAGE = -1; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
protected Log logger = LogFactory.getLog(this.getClass()); |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
protected Object[] array; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private int index; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private int page; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
private int count; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private int[] pages; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
private int last; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
0
|
protected Paginator() {... |
97 |
0
|
this(0, 0); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@param |
104 |
|
@param |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
106 |
0
|
protected Paginator(int size, int pages) {... |
107 |
0
|
this.array = new Object[size]; |
108 |
0
|
this.pages = new int[pages]; |
109 |
0
|
this.page = UNDEFINED_PAGE; |
110 |
0
|
this.index = 0; |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
@return |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
0
|
public int getPage() {... |
119 |
0
|
return page; |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
@return |
126 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
127 |
0
|
public int[] getPages() {... |
128 |
0
|
return pages; |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
@return |
135 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
0
|
public int getPageCount() {... |
137 |
0
|
return last; |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
@param |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0,8 |
|
145 |
0
|
public void setPages(int size) {... |
146 |
0
|
if (array.length > 0) { |
147 |
0
|
this.last = (count / array.length) + (count % array.length == 0 ? 0 : 1); |
148 |
0
|
if (size == Paginator.UNLIMITED_PAGES || last <= size) { |
149 |
0
|
this.pages = new int[last]; |
150 |
|
} else { |
151 |
0
|
this.pages = new int[size]; |
152 |
|
} |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 10 |
Complexity Density: 0,71 |
|
161 |
0
|
public void setPage(int page) {... |
162 |
0
|
if (count > 0) { |
163 |
0
|
if (pages.length != UNLIMITED_PAGES && (page <= 0 || page > last)) { |
164 |
0
|
throw new IndexOutOfBoundsException("Invalid page " + page); |
165 |
|
} |
166 |
0
|
if (page < pages[0] || page > pages[pages.length - 1]) { |
167 |
0
|
for (int i = 0; i < pages.length; i++) { |
168 |
0
|
pages[i] = (((page - 1) / pages.length) * pages.length) + i + 1; |
169 |
|
} |
170 |
0
|
if (pages[pages.length - 1] > last) { |
171 |
0
|
int[] tmp = new int[last - pages[0] + 1]; |
172 |
0
|
System.arraycopy(pages, 0, tmp, 0, tmp.length); |
173 |
0
|
this.pages = tmp; |
174 |
|
} |
175 |
|
} |
176 |
0
|
this.index = 0; |
177 |
0
|
if (page != this.page) { |
178 |
0
|
this.page = page; |
179 |
0
|
this.setArray(); |
180 |
|
} |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
@return |
188 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 4 |
Complexity Density: 1,33 |
|
189 |
0
|
public int getNext() {... |
190 |
0
|
if (page >= last || count == 0 || pages[pages.length - 1] >= last) { |
191 |
0
|
return -1; |
192 |
|
} else { |
193 |
0
|
return pages[pages.length - 1] + 1; |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
@return |
201 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 4 |
Complexity Density: 1,33 |
|
202 |
0
|
public int getPrev() {... |
203 |
0
|
if (page <= 1 || pages[0] <= 1 || count == 0) { |
204 |
0
|
return -1; |
205 |
|
} else { |
206 |
0
|
return pages[0] - 1; |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
@param |
214 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0,6 |
|
215 |
0
|
public void setCount(int count) {... |
216 |
0
|
this.count = count; |
217 |
0
|
if (pages.length > 0) { |
218 |
0
|
this.setPages(pages.length); |
219 |
|
} |
220 |
0
|
if (array.length == UNLIMITED_ITEMS) { |
221 |
0
|
this.setPageSize(count); |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
@return |
229 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
230 |
0
|
public int getCount() {... |
231 |
0
|
return count; |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
@return |
238 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
239 |
0
|
public int getPageSize() {... |
240 |
0
|
return array.length; |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
@param |
247 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
248 |
0
|
public void setPageSize(int size) {... |
249 |
0
|
if (size == UNLIMITED_ITEMS) { |
250 |
0
|
this.array = new Object[size]; |
251 |
0
|
this.setPages(1); |
252 |
|
} else { |
253 |
0
|
this.array = new Object[size]; |
254 |
0
|
this.setPages(UNLIMITED_PAGES); |
255 |
|
} |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
@return |
262 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
263 |
0
|
public int getStart() {... |
264 |
0
|
if (pages.length > 0) { |
265 |
0
|
return ((page - 1) * array.length) + 1; |
266 |
|
} else { |
267 |
0
|
return 0; |
268 |
|
} |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
@return |
275 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0,6 |
|
276 |
0
|
public int getStop() {... |
277 |
0
|
if (pages.length > 0) { |
278 |
0
|
if (page * array.length < count) { |
279 |
0
|
return page * array.length; |
280 |
|
} else { |
281 |
0
|
return count; |
282 |
|
} |
283 |
|
} else { |
284 |
0
|
return 0; |
285 |
|
} |
286 |
|
} |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
abstract protected void setArray(); |
292 |
|
|
293 |
|
|
294 |
|
@see |
295 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
296 |
0
|
public boolean hasNext() {... |
297 |
0
|
if (page == UNDEFINED_PAGE) { |
298 |
0
|
this.setPage(1); |
299 |
|
} |
300 |
0
|
return index < array.length && array[index] != null; |
301 |
|
} |
302 |
|
|
303 |
|
|
304 |
|
@see |
305 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 2 |
Complexity Density: 1 |
|
306 |
0
|
public Object next() {... |
307 |
0
|
try { |
308 |
0
|
return array[index++]; |
309 |
|
} catch (Exception e) { |
310 |
|
throw new NoSuchElementException(); |
311 |
|
} |
312 |
|
} |
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
@see |
318 |
|
@throws |
319 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
320 |
0
|
public void remove() {... |
321 |
0
|
throw new UnsupportedOperationException(); |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
327 |
0
|
public void reset() {... |
328 |
0
|
this.index = 0; |
329 |
|
} |
330 |
|
} |