KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > controller > model > PageIterator


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

15
16 package com.jdon.controller.model;
17
18 import java.util.*;
19 import java.io.Serializable JavaDoc;
20
21 /**
22  * All model's primary key(or identifier) collection of every page that will be
23  * displayed it carry the model's primary key collection from persistence lay to
24  * presentation lay .
25  *
26  * restriction : the class type pf your model primary key must be equal to the
27  * data type of primary key of the table.
28  *
29  *
30  *
31  * com.jdon.model.query.PageIteratorSolver supply a factory that create this
32  * class in persistence lay
33  *
34  * the class is stateful.
35  *
36  *
37  *
38  * @see ModelListAction ModelListForm PageIteratorSolver
39  * @version 1.4
40  */

41 public class PageIterator implements Iterator, Serializable JavaDoc, Cloneable JavaDoc {
42
43     public final static Object JavaDoc[] EMPTY = new Object JavaDoc[0];
44
45     /**
46      * the count of all models that fit for query condition
47      */

48     private int allCount = 0;
49
50     /**
51      * all database table's primary key colletion in current page the type of
52      * the primary key is the primary key's type in the database table schema.
53      *
54      * default elements's type should be Model's key (identifier)
55      * but we can put Model instances into the elements by seting elementsTypeIsKey to false
56      *
57      */

58     private Object JavaDoc[] elements = EMPTY;
59
60     /**
61      * when iterating current page, current record position.
62      */

63     private int currentIndex = -1;
64
65     /**
66      * next record (model)
67      */

68     private Object JavaDoc nextElement = null;
69
70     /**
71      * previous
72      */

73     private Object JavaDoc previousElement = null;
74
75     /**
76      * current page start
77      */

78     private int startIndex = -1;
79     
80     /**
81      * current page end
82      */

83     private int endIndex = -1;
84     
85     /**
86      * the line count in a page
87      */

88     private int count = 0;
89     
90     /**
91      * default elements's type should be Model's key (identifier)
92      * but we can put Model instances into the elements.
93      */

94     private boolean elementsTypeIsKey = true;
95     
96       
97     /**
98      * Block Construtor
99      * default construtor, this is a block construtor
100      * the keys's lengt is always greater than the page length that will be
101      * displayed.
102      *
103      * @param allCount the all count for all results.
104      * @param keys primary keys collection for every block
105      * @param start the start index in the keys.
106      * @param endIndex the end index in the keys
107      * @param actualCount the count of the primary keys collection.
108      */

109     public PageIterator(int allCount, Object JavaDoc[] keys, int startIndex, int endIndex, int count) {
110         this.allCount = allCount;
111         this.elements = keys;
112         if (startIndex >= 0) {
113             this.startIndex = startIndex;
114             this.currentIndex = startIndex - 1;
115         }
116         this.endIndex = endIndex;
117         this.count = count;
118     }
119
120   
121     /**
122      * Page Construtor
123      * this is user customization construtor,
124      * the keys's lengt is always equals the page length that will be displayed.
125      *
126      * @param allCount all count for all results
127      * @param keys primary keys collection for a page defined by client's count value.
128      */

129     public PageIterator(int allCount, Object JavaDoc[] keys) {
130         this.allCount = allCount;
131         this.elements = keys;
132         this.endIndex = keys.length;
133     }
134     
135     /**
136      * Page Construtor2
137      * this is for old version
138      * @param allCount all count for all results
139      * @param keys primary keys collection for a page defined by client's count value.
140      * @param startIndex the start index of in the primary keys collection
141      * @param hasNextPage if has next page.
142      */

143     public PageIterator(int allCount, Object JavaDoc[] keys, int startIndex, boolean hasNextPage) {
144         this(allCount, keys);
145     }
146     
147     /**
148      * Page Construtor
149      * this is for old version
150      * allCount must be enter later by setAllcount
151      *
152      */

153     public PageIterator(Object JavaDoc[] keys, int startIndex, boolean hasNextPage) {
154         this(0, keys);
155       }
156
157     /**
158      * empty construtor this construtor can ensure the jsp view page don't
159      * happened nullException!
160      *
161      */

162     public PageIterator() {
163         
164     }
165
166     
167     public int getAllCount() {
168         return allCount;
169     }
170
171     public void setAllCount(int allCount) {
172         this.allCount = allCount;
173     }
174
175     /**
176      * reset
177      *
178      */

179     public void reset() {
180         elements = EMPTY;
181         currentIndex = -1;
182         startIndex = -1;
183         endIndex = -1;
184         nextElement = null;
185         previousElement = null;
186         count = 0;
187         allCount = 0;
188         
189     }
190     
191     public void setIndex(int index){
192         if((index >= startIndex) || (index < endIndex))
193             currentIndex = index;
194         else
195             System.err.println("PageIterator error: setIndex error: index=" + index + " exceed the 0 or Max length=" + elements.length);
196         
197     }
198
199     /**
200      * Returns true if there are more elements in the iteration.
201      *
202      * @return true if the iterator has more elements.
203      */

204     public boolean hasNext() {
205         if (currentIndex == endIndex) {
206             return false;
207         }
208               
209         // Otherwise, see if nextElement is null. If so, try to load the next
210
// element to make sure it exists.
211
if (nextElement == null) {
212             nextElement = getNextElement();
213             if (nextElement == null) {
214                 return false;
215             }
216         }
217         return true;
218     }
219
220     /**
221      * Returns the next element of primary key collection.
222      *
223      * @return the next element.
224      * @throws NoSuchElementException
225      * if there are no more elements.
226      */

227     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
228         Object JavaDoc element = null;
229         if (nextElement != null) {
230             element = nextElement;
231             nextElement = null;
232         } else {
233             element = getNextElement();
234             if (element == null) {
235                 throw new java.util.NoSuchElementException JavaDoc();
236             }
237         }
238         return element;
239     }
240
241     /**
242      * Returns true if there are previous elements in the iteration.
243      *
244      * @return
245      */

246     public boolean hasPrevious() {
247         // If we are at the start of the list there are no previous elements.
248
if (currentIndex == startIndex) {
249             return false;
250         }
251         // Otherwise, see if previous Element is null. If so, try to load the
252
// previous element to make sure it exists.
253
if (previousElement == null) {
254             previousElement = getPreviousElement();
255             // If getting the previous element failed, return false.
256
if (previousElement == null) {
257                 return false;
258             }
259         }
260         return true;
261     }
262
263     /**
264      * * Returns the previous element of primary key collection.
265      *
266      * @return
267      */

268     public Object JavaDoc previous() {
269         Object JavaDoc element = null;
270         if (previousElement != null) {
271             element = previousElement;
272             previousElement = null;
273         } else {
274             element = getPreviousElement();
275             if (element == null) {
276                 throw new java.util.NoSuchElementException JavaDoc();
277             }
278         }
279         return element;
280     }
281
282     /**
283      * Returns the previous element, or null if there are no more elements to
284      * return.
285      *
286      * @return the previous element.
287      */

288     private Object JavaDoc getPreviousElement() {
289         Object JavaDoc element = null;
290         while (currentIndex >= startIndex && element == null) {
291             currentIndex--;
292             element = getElement();
293         }
294         return element;
295     }
296
297     /**
298      * Not supported for security reasons.
299      */

300     public void remove() throws UnsupportedOperationException JavaDoc {
301         throw new UnsupportedOperationException JavaDoc();
302     }
303
304     /**
305      * Returns the next available element of primary key collection, or null if
306      * there are no more elements to return.
307      *
308      * @return the next available element.
309      */

310     public Object JavaDoc getNextElement() {
311         Object JavaDoc element = null;
312         while (currentIndex+1 < endIndex && element == null) {
313             currentIndex++;
314             element = getElement();
315         }
316         return element;
317     }
318     
319     private Object JavaDoc getElement(){
320         Object JavaDoc element = null;
321         if ((currentIndex >=0 ) && (currentIndex < elements.length)){
322             element = elements[currentIndex];
323         }else
324             System.err.println("PageIterator error: currentIndex=" + currentIndex + " exceed the 0 or Max length=" + elements.length);
325         return element;
326     }
327
328     public int getSize() {
329         return elements.length;
330     }
331     
332     /**
333      * @return Returns the keys.
334      */

335     public Object JavaDoc[] getKeys() {
336         return elements;
337     }
338         
339     /**
340      * @param keys The keys to set.
341      */

342     public void setKeys(Object JavaDoc[] keys) {
343         this.elements = keys;
344     }
345     
346     
347     /**
348      * @return Returns the actualCount.
349      */

350     public int getCount() {
351         return count;
352     }
353
354     
355
356     public boolean isElementsTypeIsKey() {
357         return elementsTypeIsKey;
358     }
359
360
361     public void setElementsTypeIsKey(boolean elementsTypeIsKey) {
362         this.elementsTypeIsKey = elementsTypeIsKey;
363     }
364     
365     
366     
367 }
368
Popular Tags