View Javadoc
1   /*
2    * Copyright 2004-2026 the original author or authors.
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    *    https://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.sqlmap.engine.mapping.statement;
17  
18  import com.ibatis.common.util.PaginatedList;
19  import com.ibatis.sqlmap.client.SqlMapExecutor;
20  
21  import java.sql.SQLException;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.ListIterator;
27  
28  /**
29   * The Class PaginatedDataList.
30   *
31   * @deprecated All paginated list features have been deprecated
32   */
33  @Deprecated
34  public class PaginatedDataList implements PaginatedList {
35  
36    /** The sql map executor. */
37    private SqlMapExecutor sqlMapExecutor;
38  
39    /** The statement name. */
40    private String statementName;
41  
42    /** The parameter object. */
43    private Object parameterObject;
44  
45    /** The page size. */
46    private int pageSize;
47  
48    /** The index. */
49    private int index;
50  
51    /** The prev page list. */
52    private List prevPageList;
53  
54    /** The current page list. */
55    private List currentPageList;
56  
57    /** The next page list. */
58    private List nextPageList;
59  
60    /**
61     * Instantiates a new paginated data list.
62     *
63     * @param sqlMapExecutor
64     *          the sql map executor
65     * @param statementName
66     *          the statement name
67     * @param parameterObject
68     *          the parameter object
69     * @param pageSize
70     *          the page size
71     *
72     * @throws SQLException
73     *           the SQL exception
74     */
75    @Deprecated
76    public PaginatedDataList(SqlMapExecutor sqlMapExecutor, String statementName, Object parameterObject, int pageSize)
77        throws SQLException {
78      this.sqlMapExecutor = sqlMapExecutor;
79      this.statementName = statementName;
80      this.parameterObject = parameterObject;
81      this.pageSize = pageSize;
82      this.index = 0;
83      pageTo(0);
84    }
85  
86    /**
87     * Page forward.
88     */
89    private void pageForward() {
90      try {
91        prevPageList = currentPageList;
92        currentPageList = nextPageList;
93        nextPageList = getList(index + 1, pageSize);
94      } catch (SQLException e) {
95        throw new RuntimeException("Unexpected error while repaginating paged list.  Cause: " + e, e);
96      }
97    }
98  
99    /**
100    * Page back.
101    */
102   private void pageBack() {
103     try {
104       nextPageList = currentPageList;
105       currentPageList = prevPageList;
106       if (index > 0) {
107         prevPageList = getList(index - 1, pageSize);
108       } else {
109         prevPageList = new ArrayList<>();
110       }
111     } catch (SQLException e) {
112       throw new RuntimeException("Unexpected error while repaginating paged list.  Cause: " + e, e);
113     }
114   }
115 
116   /**
117    * Safe page to.
118    *
119    * @param idx
120    *          the idx
121    */
122   private void safePageTo(int idx) {
123     try {
124       pageTo(idx);
125     } catch (SQLException e) {
126       throw new RuntimeException("Unexpected error while repaginating paged list.  Cause: " + e, e);
127     }
128   }
129 
130   /**
131    * Page to.
132    *
133    * @param idx
134    *          the idx
135    *
136    * @throws SQLException
137    *           the SQL exception
138    */
139   @Deprecated
140   public void pageTo(int idx) throws SQLException {
141     index = idx;
142 
143     List list;
144 
145     if (idx < 1) {
146       list = getList(idx, pageSize * 2);
147     } else {
148       list = getList(idx - 1, pageSize * 3);
149     }
150 
151     if (list.size() < 1) {
152       prevPageList = new ArrayList<>(0);
153       currentPageList = new ArrayList<>(0);
154       nextPageList = new ArrayList<>(0);
155     } else if (idx < 1) {
156       prevPageList = new ArrayList<>(0);
157       if (list.size() <= pageSize) {
158         currentPageList = list.subList(0, list.size());
159         nextPageList = new ArrayList<>(0);
160       } else {
161         currentPageList = list.subList(0, pageSize);
162         nextPageList = list.subList(pageSize, list.size());
163       }
164     } else if (list.size() <= pageSize) {
165       prevPageList = list.subList(0, list.size());
166       currentPageList = new ArrayList<>(0);
167       nextPageList = new ArrayList<>(0);
168     } else if (list.size() <= pageSize * 2) {
169       prevPageList = list.subList(0, pageSize);
170       currentPageList = list.subList(pageSize, list.size());
171       nextPageList = new ArrayList<>(0);
172     } else {
173       prevPageList = list.subList(0, pageSize);
174       currentPageList = list.subList(pageSize, pageSize * 2);
175       nextPageList = list.subList(pageSize * 2, list.size());
176     }
177 
178   }
179 
180   /**
181    * Gets the list.
182    *
183    * @param idx
184    *          the idx
185    * @param localPageSize
186    *          the local page size
187    *
188    * @return the list
189    *
190    * @throws SQLException
191    *           the SQL exception
192    */
193   private List getList(int idx, int localPageSize) throws SQLException {
194     return sqlMapExecutor.queryForList(statementName, parameterObject, idx * pageSize, localPageSize);
195   }
196 
197   @Deprecated
198   @Override
199   public boolean nextPage() {
200     if (isNextPageAvailable()) {
201       index++;
202       pageForward();
203       return true;
204     }
205     return false;
206   }
207 
208   @Deprecated
209   @Override
210   public boolean previousPage() {
211     if (isPreviousPageAvailable()) {
212       index--;
213       pageBack();
214       return true;
215     }
216     return false;
217   }
218 
219   @Deprecated
220   @Override
221   public void gotoPage(int pageNumber) {
222     safePageTo(pageNumber);
223   }
224 
225   @Deprecated
226   @Override
227   public int getPageSize() {
228     return pageSize;
229   }
230 
231   @Deprecated
232   @Override
233   public boolean isFirstPage() {
234     return index == 0;
235   }
236 
237   @Deprecated
238   @Override
239   public boolean isMiddlePage() {
240     return !isFirstPage() && !isLastPage();
241   }
242 
243   @Deprecated
244   @Override
245   public boolean isLastPage() {
246     return nextPageList.size() < 1;
247   }
248 
249   @Deprecated
250   @Override
251   public boolean isNextPageAvailable() {
252     return nextPageList.size() > 0;
253   }
254 
255   @Deprecated
256   @Override
257   public boolean isPreviousPageAvailable() {
258     return prevPageList.size() > 0;
259   }
260 
261   @Deprecated
262   @Override
263   public int size() {
264     return currentPageList.size();
265   }
266 
267   @Deprecated
268   @Override
269   public boolean isEmpty() {
270     return currentPageList.isEmpty();
271   }
272 
273   @Deprecated
274   @Override
275   public boolean contains(Object o) {
276     return currentPageList.contains(o);
277   }
278 
279   @Deprecated
280   @Override
281   public Iterator iterator() {
282     return currentPageList.iterator();
283   }
284 
285   @Deprecated
286   @Override
287   public Object[] toArray() {
288     return currentPageList.toArray();
289   }
290 
291   @Deprecated
292   @Override
293   public Object[] toArray(Object a[]) {
294     return currentPageList.toArray(a);
295   }
296 
297   @Deprecated
298   @Override
299   public boolean containsAll(Collection c) {
300     return currentPageList.containsAll(c);
301   }
302 
303   @Deprecated
304   @Override
305   public Object get(int index) {
306     return currentPageList.get(index);
307   }
308 
309   @Deprecated
310   @Override
311   public int indexOf(Object o) {
312     return currentPageList.indexOf(o);
313   }
314 
315   @Deprecated
316   @Override
317   public int lastIndexOf(Object o) {
318     return currentPageList.lastIndexOf(o);
319   }
320 
321   @Deprecated
322   @Override
323   public ListIterator listIterator() {
324     return currentPageList.listIterator();
325   }
326 
327   @Deprecated
328   @Override
329   public ListIterator listIterator(int index) {
330     return currentPageList.listIterator(index);
331   }
332 
333   @Deprecated
334   @Override
335   public List subList(int fromIndex, int toIndex) {
336     return currentPageList.subList(fromIndex, toIndex);
337   }
338 
339   @Deprecated
340   @Override
341   public boolean add(Object o) {
342     return currentPageList.add(o);
343   }
344 
345   @Deprecated
346   @Override
347   public boolean remove(Object o) {
348     return currentPageList.remove(o);
349   }
350 
351   @Deprecated
352   @Override
353   public boolean addAll(Collection c) {
354     return currentPageList.addAll(c);
355   }
356 
357   @Deprecated
358   @Override
359   public boolean addAll(int index, Collection c) {
360     return currentPageList.addAll(index, c);
361   }
362 
363   @Deprecated
364   @Override
365   public boolean removeAll(Collection c) {
366     return currentPageList.removeAll(c);
367   }
368 
369   @Deprecated
370   @Override
371   public boolean retainAll(Collection c) {
372     return currentPageList.retainAll(c);
373   }
374 
375   @Deprecated
376   @Override
377   public void clear() {
378     currentPageList.clear();
379   }
380 
381   @Deprecated
382   @Override
383   public Object set(int index, Object element) {
384     return currentPageList.set(index, element);
385   }
386 
387   @Deprecated
388   @Override
389   public void add(int index, Object element) {
390     currentPageList.add(index, element);
391   }
392 
393   @Deprecated
394   @Override
395   public Object remove(int index) {
396     return currentPageList.remove(index);
397   }
398 
399   @Deprecated
400   @Override
401   public int getPageIndex() {
402     return index;
403   }
404 
405 }