View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
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 net.sf.oness.common.model.util;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.ListIterator;
22  
23  import net.sf.oness.common.all.BaseObject;
24  
25  /***
26   * <p>
27   * Class that represents a paginated List,
28   * with elements from firstElement and at maximum maxElements
29   * elements from the full list of size elements.
30   * </p>
31   * <p>Note that firstElement starts at 0</p>
32   * <p>Any attempt to access other than the current page
33   * will cause an error.</p>
34   * <p>This is a read only implementation and many of the 
35   * <code>List</code> methods are not implemented.</p>
36   * 
37   * @author Carlos Sanchez
38   * @version $Revision: 1.2 $
39   */
40  public class PaginatedList extends BaseObject implements List {
41  
42      private List list;
43      private int firstElement, maxElements, size;
44  
45      public PaginatedList() {
46      }
47  
48      public PaginatedList(List list, int firstElement, int maxElements, int size) {
49          this.list = list;
50          this.firstElement = firstElement;
51          this.maxElements = maxElements;
52          this.size = size;
53      }
54  
55      /***
56       * Get list with the elements of this page
57       * @return
58       */
59      public List getList() {
60          return list;
61      }
62  
63      public void setList(List list) {
64          this.list = list;
65      }
66  
67      /***
68       * First element of this page, starting at 0
69       * @return
70       */
71      public int getFirstElement() {
72          return firstElement;
73      }
74  
75      public void setFirstElement(int firstElement) {
76          this.firstElement = firstElement;
77      }
78  
79      /***
80       * Max number of elements in the page
81       * @return
82       */
83      public int getMaxElements() {
84          return maxElements;
85      }
86  
87      public void setMaxElements(int maxElements) {
88          this.maxElements = maxElements;
89      }
90  
91      /***
92       * Set the number of elements in all the pages 
93       */
94      public void setSize(int size) {
95          this.size = size;
96      }
97  
98      /***
99       * Number of elements in this page
100      * @return
101      */
102     public int getPageSize() {
103         return list.size();
104     }
105 
106     /***
107      * Calculate the page number, starting at 0
108      * @return
109      */
110     public int getPageNumber() {
111         return getFirstElement() / getMaxElements();
112     }
113 
114     /***
115      * Calculate the last page number, starting at 0
116      * @return
117      */
118     public int getLastPageNumber() {
119         return (size() - 1) / getMaxElements();
120     }
121 
122     // ---------------------------- List operations ----------------------------
123 
124     /***
125      * Unsupported operation
126      * @see java.util.Collection#iterator()
127      * @throws UnsupportedOperationException
128      */
129     public Iterator iterator() {
130         return new PaginatedListIterator(this);
131     }
132 
133     /***
134      * Number of elements in all the pages 
135      * @see java.util.Collection#size()
136      */
137     public int size() {
138         return size;
139     }
140 
141     /***
142      * Number of elements in all the pages 
143      * @see size()
144      */
145     public int getSize() {
146         return size();
147     }
148 
149     // ---------------------- Unsupported List operations ----------------------
150 
151     /***
152      * Unsupported operation
153      * @see java.util.List#add(int, java.lang.Object)
154      * @throws UnsupportedOperationException
155      */
156     public void add(int arg0, Object arg1) {
157         throw new UnsupportedOperationException();
158     }
159 
160     /***
161      * Unsupported operation
162      * @see java.util.Collection#add(java.lang.Object)
163      * @throws UnsupportedOperationException
164      */
165     public boolean add(Object arg0) {
166         throw new UnsupportedOperationException();
167     }
168 
169     /***
170      * Unsupported operation
171      * @see java.util.Collection#addAll(java.util.Collection)
172      * @throws UnsupportedOperationException
173      */
174     public boolean addAll(Collection arg0) {
175         throw new UnsupportedOperationException();
176     }
177 
178     /***
179      * Unsupported operation
180      * @see java.util.List#addAll(int, java.util.Collection)
181      * @throws UnsupportedOperationException
182      */
183     public boolean addAll(int arg0, Collection arg1) {
184         throw new UnsupportedOperationException();
185     }
186 
187     /***
188      * Unsupported operation
189      * @see java.util.Collection#clear()
190      * @throws UnsupportedOperationException
191      */
192     public void clear() {
193         throw new UnsupportedOperationException();
194 
195     }
196 
197     /***
198      * Unsupported operation
199      * @see java.util.Collection#contains(java.lang.Object)
200      * @throws UnsupportedOperationException
201      */
202     public boolean contains(Object arg0) {
203         throw new UnsupportedOperationException();
204     }
205 
206     /***
207      * Unsupported operation
208      * @see java.util.Collection#containsAll(java.util.Collection)
209      * @throws UnsupportedOperationException
210      */
211     public boolean containsAll(Collection arg0) {
212         throw new UnsupportedOperationException();
213     }
214 
215     /***
216      * Unsupported operation
217      * @see java.util.List#get(int)
218      * @throws UnsupportedOperationException
219      */
220     public Object get(int arg0) {
221         throw new UnsupportedOperationException();
222     }
223 
224     /***
225      * Unsupported operation
226      * @see java.util.List#indexOf(java.lang.Object)
227      * @throws UnsupportedOperationException
228      */
229     public int indexOf(Object arg0) {
230         throw new UnsupportedOperationException();
231     }
232 
233     /***
234      * Unsupported operation
235      * @see java.util.Collection#isEmpty()
236      * @throws UnsupportedOperationException
237      */
238     public boolean isEmpty() {
239         throw new UnsupportedOperationException();
240     }
241 
242     /***
243      * Unsupported operation
244      * @see java.util.List#lastIndexOf(java.lang.Object)
245      * @throws UnsupportedOperationException
246      */
247     public int lastIndexOf(Object arg0) {
248         throw new UnsupportedOperationException();
249     }
250 
251     /***
252      * Unsupported operation
253      * @see java.util.List#listIterator()
254      * @throws UnsupportedOperationException
255      */
256     public ListIterator listIterator() {
257         throw new UnsupportedOperationException();
258     }
259 
260     /***
261      * Unsupported operation
262      * @see java.util.List#listIterator(int)
263      * @throws UnsupportedOperationException
264      */
265     public ListIterator listIterator(int arg0) {
266         throw new UnsupportedOperationException();
267     }
268 
269     /***
270      * Unsupported operation
271      * @see java.util.List#remove(int)
272      * @throws UnsupportedOperationException
273      */
274     public Object remove(int arg0) {
275         throw new UnsupportedOperationException();
276     }
277 
278     /***
279      * Unsupported operation
280      * @see java.util.Collection#remove(java.lang.Object)
281      * @throws UnsupportedOperationException
282      */
283     public boolean remove(Object arg0) {
284         throw new UnsupportedOperationException();
285     }
286 
287     /***
288      * Unsupported operation
289      * @see java.util.Collection#removeAll(java.util.Collection)
290      * @throws UnsupportedOperationException
291      */
292     public boolean removeAll(Collection arg0) {
293         throw new UnsupportedOperationException();
294     }
295 
296     /***
297      * Unsupported operation
298      * @see java.util.Collection#retainAll(java.util.Collection)
299      * @throws UnsupportedOperationException
300      */
301     public boolean retainAll(Collection arg0) {
302         throw new UnsupportedOperationException();
303     }
304 
305     /***
306      * Unsupported operation
307      * @see java.util.List#set(int, java.lang.Object)
308      * @throws UnsupportedOperationException
309      */
310     public Object set(int arg0, Object arg1) {
311         throw new UnsupportedOperationException();
312     }
313 
314     /***
315      * Unsupported operation
316      * @see java.util.List#subList(int, int)
317      * @throws UnsupportedOperationException
318      */
319     public List subList(int arg0, int arg1) {
320         throw new UnsupportedOperationException();
321     }
322 
323     /***
324      * Unsupported operation
325      * @see java.util.Collection#toArray()
326      * @throws UnsupportedOperationException
327      */
328     public Object[] toArray() {
329         throw new UnsupportedOperationException();
330     }
331 
332     /***
333      * Unsupported operation
334      * @see java.util.Collection#toArray(java.lang.Object[])
335      * @throws UnsupportedOperationException
336      */
337     public Object[] toArray(Object[] arg0) {
338         throw new UnsupportedOperationException();
339     }
340 
341 }