001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.common.util;
017:
018: import java.util.*;
019:
020: /**
021: * Implementation of PaginatedList backed by an ArrayList
022: * @deprecated All paginated list features have been deprecated
023: */
024: public class PaginatedArrayList implements PaginatedList {
025:
026: private static final ArrayList EMPTY_LIST = new ArrayList(0);
027:
028: private List list;
029: private List page;
030: private int pageSize;
031: private int index;
032:
033: /**
034: * @param pageSize
035: */
036: public PaginatedArrayList(int pageSize) {
037: this .pageSize = pageSize;
038: this .index = 0;
039: this .list = new ArrayList();
040: repaginate();
041: }
042:
043: /**
044: * Constructor to set the initial size and the page size
045: * @param initialCapacity - the initial size
046: * @param pageSize - the page size
047: */
048: public PaginatedArrayList(int initialCapacity, int pageSize) {
049: this .pageSize = pageSize;
050: this .index = 0;
051: this .list = new ArrayList(initialCapacity);
052: repaginate();
053: }
054:
055: /**
056: * Constructor to create an instance using an existing collection
057: * @param c - the collection to build the instance with
058: * @param pageSize - the page size
059: */
060: public PaginatedArrayList(Collection c, int pageSize) {
061: this .pageSize = pageSize;
062: this .index = 0;
063: this .list = new ArrayList(c);
064: repaginate();
065: }
066:
067: private void repaginate() {
068: if (list.isEmpty()) {
069: page = EMPTY_LIST;
070: } else {
071: int start = index * pageSize;
072: int end = start + pageSize - 1;
073: if (end >= list.size()) {
074: end = list.size() - 1;
075: }
076: if (start >= list.size()) {
077: index = 0;
078: repaginate();
079: } else if (start < 0) {
080: index = list.size() / pageSize;
081: if (list.size() % pageSize == 0) {
082: index--;
083: }
084: repaginate();
085: } else {
086: page = list.subList(start, end + 1);
087: }
088: }
089: }
090:
091: /* List accessors (uses page) */
092:
093: public int size() {
094: return page.size();
095: }
096:
097: public boolean isEmpty() {
098: return page.isEmpty();
099: }
100:
101: public boolean contains(Object o) {
102: return page.contains(o);
103: }
104:
105: public Iterator iterator() {
106: return page.iterator();
107: }
108:
109: public Object[] toArray() {
110: return page.toArray();
111: }
112:
113: public Object[] toArray(Object a[]) {
114: return page.toArray(a);
115: }
116:
117: public boolean containsAll(Collection c) {
118: return page.containsAll(c);
119: }
120:
121: public Object get(int index) {
122: return page.get(index);
123: }
124:
125: public int indexOf(Object o) {
126: return page.indexOf(o);
127: }
128:
129: public int lastIndexOf(Object o) {
130: return page.lastIndexOf(o);
131: }
132:
133: public ListIterator listIterator() {
134: return page.listIterator();
135: }
136:
137: public ListIterator listIterator(int index) {
138: return page.listIterator(index);
139: }
140:
141: public List subList(int fromIndex, int toIndex) {
142: return page.subList(fromIndex, toIndex);
143: }
144:
145: /* List mutators (uses master list) */
146:
147: public boolean add(Object o) {
148: boolean b = list.add(o);
149: repaginate();
150: return b;
151: }
152:
153: public boolean remove(Object o) {
154: boolean b = list.remove(o);
155: repaginate();
156: return b;
157: }
158:
159: public boolean addAll(Collection c) {
160: boolean b = list.addAll(c);
161: repaginate();
162: return b;
163: }
164:
165: public boolean addAll(int index, Collection c) {
166: boolean b = list.addAll(index, c);
167: repaginate();
168: return b;
169: }
170:
171: public boolean removeAll(Collection c) {
172: boolean b = list.removeAll(c);
173: repaginate();
174: return b;
175: }
176:
177: public boolean retainAll(Collection c) {
178: boolean b = list.retainAll(c);
179: repaginate();
180: return b;
181: }
182:
183: public void clear() {
184: list.clear();
185: repaginate();
186: }
187:
188: public Object set(int index, Object element) {
189: Object o = list.set(index, element);
190: repaginate();
191: return o;
192: }
193:
194: public void add(int index, Object element) {
195: list.add(index, element);
196: repaginate();
197: }
198:
199: public Object remove(int index) {
200: Object o = list.remove(index);
201: repaginate();
202: return o;
203: }
204:
205: /* Paginated List methods */
206:
207: public int getPageSize() {
208: return pageSize;
209: }
210:
211: public boolean isFirstPage() {
212: return index == 0;
213: }
214:
215: public boolean isMiddlePage() {
216: return !(isFirstPage() || isLastPage());
217: }
218:
219: public boolean isLastPage() {
220: return list.size() - ((index + 1) * pageSize) < 1;
221: }
222:
223: public boolean isNextPageAvailable() {
224: return !isLastPage();
225: }
226:
227: public boolean isPreviousPageAvailable() {
228: return !isFirstPage();
229: }
230:
231: public boolean nextPage() {
232: if (isNextPageAvailable()) {
233: index++;
234: repaginate();
235: return true;
236: } else {
237: return false;
238: }
239: }
240:
241: public boolean previousPage() {
242: if (isPreviousPageAvailable()) {
243: index--;
244: repaginate();
245: return true;
246: } else {
247: return false;
248: }
249: }
250:
251: public void gotoPage(int pageNumber) {
252: index = pageNumber;
253: repaginate();
254: }
255:
256: public int getPageIndex() {
257: return index;
258: }
259:
260: }
|