KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > util > PaginatedArrayList


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.common.util;
17
18 import java.util.*;
19
20 /**
21  * Implementation of PaginatedList backed by an ArrayList
22  */

23 public class PaginatedArrayList implements PaginatedList {
24
25   private static final ArrayList EMPTY_LIST = new ArrayList(0);
26
27   private List list;
28   private List page;
29   private int pageSize;
30   private int index;
31
32   /**
33    * @param pageSize
34    */

35   public PaginatedArrayList(int pageSize) {
36     this.pageSize = pageSize;
37     this.index = 0;
38     this.list = new ArrayList();
39     repaginate();
40   }
41
42   /**
43    * Constructor to set the initial size and the page size
44    * @param initialCapacity - the initial size
45    * @param pageSize - the page size
46    */

47   public PaginatedArrayList(int initialCapacity, int pageSize) {
48     this.pageSize = pageSize;
49     this.index = 0;
50     this.list = new ArrayList(initialCapacity);
51     repaginate();
52   }
53
54   /**
55    * Constructor to create an instance using an existing collection
56    * @param c - the collection to build the instance with
57    * @param pageSize - the page size
58    */

59   public PaginatedArrayList(Collection c, int pageSize) {
60     this.pageSize = pageSize;
61     this.index = 0;
62     this.list = new ArrayList(c);
63     repaginate();
64   }
65
66   private void repaginate() {
67     if (list.isEmpty()) {
68       page = EMPTY_LIST;
69     } else {
70       int start = index * pageSize;
71       int end = start + pageSize - 1;
72       if (end >= list.size()) {
73         end = list.size() - 1;
74       }
75       if (start >= list.size()) {
76         index = 0;
77         repaginate();
78       } else if (start < 0) {
79         index = list.size() / pageSize;
80         if (list.size() % pageSize == 0) {
81           index--;
82         }
83         repaginate();
84       } else {
85         page = list.subList(start, end + 1);
86       }
87     }
88   }
89
90   /* List accessors (uses page) */
91
92   public int size() {
93     return page.size();
94   }
95
96   public boolean isEmpty() {
97     return page.isEmpty();
98   }
99
100   public boolean contains(Object JavaDoc o) {
101     return page.contains(o);
102   }
103
104   public Iterator iterator() {
105     return page.iterator();
106   }
107
108   public Object JavaDoc[] toArray() {
109     return page.toArray();
110   }
111
112   public Object JavaDoc[] toArray(Object JavaDoc a[]) {
113     return page.toArray(a);
114   }
115
116   public boolean containsAll(Collection c) {
117     return page.containsAll(c);
118   }
119
120   public Object JavaDoc get(int index) {
121     return page.get(index);
122   }
123
124   public int indexOf(Object JavaDoc o) {
125     return page.indexOf(o);
126   }
127
128   public int lastIndexOf(Object JavaDoc o) {
129     return page.lastIndexOf(o);
130   }
131
132   public ListIterator listIterator() {
133     return page.listIterator();
134   }
135
136   public ListIterator listIterator(int index) {
137     return page.listIterator(index);
138   }
139
140   public List subList(int fromIndex, int toIndex) {
141     return page.subList(fromIndex, toIndex);
142   }
143
144   /* List mutators (uses master list) */
145
146   public boolean add(Object JavaDoc o) {
147     boolean b = list.add(o);
148     repaginate();
149     return b;
150   }
151
152   public boolean remove(Object JavaDoc o) {
153     boolean b = list.remove(o);
154     repaginate();
155     return b;
156   }
157
158   public boolean addAll(Collection c) {
159     boolean b = list.addAll(c);
160     repaginate();
161     return b;
162   }
163
164   public boolean addAll(int index, Collection c) {
165     boolean b = list.addAll(index, c);
166     repaginate();
167     return b;
168   }
169
170   public boolean removeAll(Collection c) {
171     boolean b = list.removeAll(c);
172     repaginate();
173     return b;
174   }
175
176   public boolean retainAll(Collection c) {
177     boolean b = list.retainAll(c);
178     repaginate();
179     return b;
180   }
181
182   public void clear() {
183     list.clear();
184     repaginate();
185   }
186
187   public Object JavaDoc set(int index, Object JavaDoc element) {
188     Object JavaDoc o = list.set(index, element);
189     repaginate();
190     return o;
191   }
192
193   public void add(int index, Object JavaDoc element) {
194     list.add(index, element);
195     repaginate();
196   }
197
198   public Object JavaDoc remove(int index) {
199     Object JavaDoc o = list.remove(index);
200     repaginate();
201     return o;
202   }
203
204   /* Paginated List methods */
205
206   public int getPageSize() {
207     return pageSize;
208   }
209
210   public boolean isFirstPage() {
211     return index == 0;
212   }
213
214   public boolean isMiddlePage() {
215     return !(isFirstPage() || isLastPage());
216   }
217
218   public boolean isLastPage() {
219     return list.size() - ((index + 1) * pageSize) < 1;
220   }
221
222   public boolean isNextPageAvailable() {
223     return !isLastPage();
224   }
225
226   public boolean isPreviousPageAvailable() {
227     return !isFirstPage();
228   }
229
230   public boolean nextPage() {
231     if (isNextPageAvailable()) {
232       index++;
233       repaginate();
234       return true;
235     } else {
236       return false;
237     }
238   }
239
240   public boolean previousPage() {
241     if (isPreviousPageAvailable()) {
242       index--;
243       repaginate();
244       return true;
245     } else {
246       return false;
247     }
248   }
249
250   public void gotoPage(int pageNumber) {
251     index = pageNumber;
252     repaginate();
253   }
254
255   public int getPageIndex() {
256     return index;
257   }
258
259 }
260
Popular Tags