1 16 package com.ibatis.common.util; 17 18 import java.util.*; 19 20 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 35 public PaginatedArrayList(int pageSize) { 36 this.pageSize = pageSize; 37 this.index = 0; 38 this.list = new ArrayList(); 39 repaginate(); 40 } 41 42 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 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 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 o) { 101 return page.contains(o); 102 } 103 104 public Iterator iterator() { 105 return page.iterator(); 106 } 107 108 public Object [] toArray() { 109 return page.toArray(); 110 } 111 112 public Object [] toArray(Object a[]) { 113 return page.toArray(a); 114 } 115 116 public boolean containsAll(Collection c) { 117 return page.containsAll(c); 118 } 119 120 public Object get(int index) { 121 return page.get(index); 122 } 123 124 public int indexOf(Object o) { 125 return page.indexOf(o); 126 } 127 128 public int lastIndexOf(Object 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 145 146 public boolean add(Object o) { 147 boolean b = list.add(o); 148 repaginate(); 149 return b; 150 } 151 152 public boolean remove(Object 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 set(int index, Object element) { 188 Object o = list.set(index, element); 189 repaginate(); 190 return o; 191 } 192 193 public void add(int index, Object element) { 194 list.add(index, element); 195 repaginate(); 196 } 197 198 public Object remove(int index) { 199 Object o = list.remove(index); 200 repaginate(); 201 return o; 202 } 203 204 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 |