1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.common.util;
17
18 import java.util.*;
19
20
21
22
23
24
25 public class PaginatedArrayList implements PaginatedList {
26
27
28 private static final ArrayList EMPTY_LIST = new ArrayList(0);
29
30
31 private List list;
32
33
34 private List page;
35
36
37 private int pageSize;
38
39
40 private int index;
41
42
43
44
45
46
47
48 public PaginatedArrayList(int pageSize) {
49 this.pageSize = pageSize;
50 this.index = 0;
51 this.list = new ArrayList();
52 repaginate();
53 }
54
55
56
57
58
59
60
61
62
63 public PaginatedArrayList(int initialCapacity, int pageSize) {
64 this.pageSize = pageSize;
65 this.index = 0;
66 this.list = new ArrayList(initialCapacity);
67 repaginate();
68 }
69
70
71
72
73
74
75
76
77
78 public PaginatedArrayList(Collection c, int pageSize) {
79 this.pageSize = pageSize;
80 this.index = 0;
81 this.list = new ArrayList(c);
82 repaginate();
83 }
84
85
86
87
88 private void repaginate() {
89 if (list.isEmpty()) {
90 page = EMPTY_LIST;
91 } else {
92 int start = index * pageSize;
93 int end = start + pageSize - 1;
94 if (end >= list.size()) {
95 end = list.size() - 1;
96 }
97 if (start >= list.size()) {
98 index = 0;
99 repaginate();
100 } else if (start < 0) {
101 index = list.size() / pageSize;
102 if (list.size() % pageSize == 0) {
103 index--;
104 }
105 repaginate();
106 } else {
107 page = list.subList(start, end + 1);
108 }
109 }
110 }
111
112
113
114 public int size() {
115 return page.size();
116 }
117
118 public boolean isEmpty() {
119 return page.isEmpty();
120 }
121
122 public boolean contains(Object o) {
123 return page.contains(o);
124 }
125
126 public Iterator iterator() {
127 return page.iterator();
128 }
129
130 public Object[] toArray() {
131 return page.toArray();
132 }
133
134 public Object[] toArray(Object a[]) {
135 return page.toArray(a);
136 }
137
138 public boolean containsAll(Collection c) {
139 return page.containsAll(c);
140 }
141
142 public Object get(int index) {
143 return page.get(index);
144 }
145
146 public int indexOf(Object o) {
147 return page.indexOf(o);
148 }
149
150 public int lastIndexOf(Object o) {
151 return page.lastIndexOf(o);
152 }
153
154 public ListIterator listIterator() {
155 return page.listIterator();
156 }
157
158 public ListIterator listIterator(int index) {
159 return page.listIterator(index);
160 }
161
162 public List subList(int fromIndex, int toIndex) {
163 return page.subList(fromIndex, toIndex);
164 }
165
166
167
168 public boolean add(Object o) {
169 boolean b = list.add(o);
170 repaginate();
171 return b;
172 }
173
174 public boolean remove(Object o) {
175 boolean b = list.remove(o);
176 repaginate();
177 return b;
178 }
179
180 public boolean addAll(Collection c) {
181 boolean b = list.addAll(c);
182 repaginate();
183 return b;
184 }
185
186 public boolean addAll(int index, Collection c) {
187 boolean b = list.addAll(index, c);
188 repaginate();
189 return b;
190 }
191
192 public boolean removeAll(Collection c) {
193 boolean b = list.removeAll(c);
194 repaginate();
195 return b;
196 }
197
198 public boolean retainAll(Collection c) {
199 boolean b = list.retainAll(c);
200 repaginate();
201 return b;
202 }
203
204 public void clear() {
205 list.clear();
206 repaginate();
207 }
208
209 public Object set(int index, Object element) {
210 Object o = list.set(index, element);
211 repaginate();
212 return o;
213 }
214
215 public void add(int index, Object element) {
216 list.add(index, element);
217 repaginate();
218 }
219
220 public Object remove(int index) {
221 Object o = list.remove(index);
222 repaginate();
223 return o;
224 }
225
226
227
228 public int getPageSize() {
229 return pageSize;
230 }
231
232 public boolean isFirstPage() {
233 return index == 0;
234 }
235
236 public boolean isMiddlePage() {
237 return !(isFirstPage() || isLastPage());
238 }
239
240 public boolean isLastPage() {
241 return list.size() - ((index + 1) * pageSize) < 1;
242 }
243
244 public boolean isNextPageAvailable() {
245 return !isLastPage();
246 }
247
248 public boolean isPreviousPageAvailable() {
249 return !isFirstPage();
250 }
251
252 public boolean nextPage() {
253 if (isNextPageAvailable()) {
254 index++;
255 repaginate();
256 return true;
257 } else {
258 return false;
259 }
260 }
261
262 public boolean previousPage() {
263 if (isPreviousPageAvailable()) {
264 index--;
265 repaginate();
266 return true;
267 } else {
268 return false;
269 }
270 }
271
272 public void gotoPage(int pageNumber) {
273 index = pageNumber;
274 repaginate();
275 }
276
277 public int getPageIndex() {
278 return index;
279 }
280
281 }