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.Iterator;
19  import java.util.NoSuchElementException;
20  
21  import net.sf.oness.common.all.BaseObject;
22  
23  /***
24   * @author Carlos Sanchez
25   * @version $Revision: 1.2 $
26   */
27  public class PaginatedListIterator extends BaseObject implements Iterator {
28  
29      private PaginatedList list;
30      private int i = 0;
31      private Iterator iterator;
32  
33      /***
34       * @param list
35       */
36      public PaginatedListIterator(PaginatedList list) {
37          this.list = list;
38      }
39  
40      /***
41       * @see java.util.Iterator#hasNext()
42       */
43      public boolean hasNext() {
44          return i < list.size();
45      }
46  
47      /***
48       * This method follows the rules of Iterator.next() except
49       * that it returns null when requesting an element that it's
50       * not in the current page.
51       * @see java.util.Iterator#next()
52       */
53      public Object next() {
54          if (i == list.getFirstElement()) {
55              iterator = list.getList().iterator();
56          }
57  
58          if ((i >= list.getFirstElement())
59              && (i < list.getFirstElement() + list.getMaxElements())) {
60              i++;
61              return iterator.next();
62          }
63  
64          if (hasNext()) {
65              i++;
66              return null;
67          } else {
68              throw new NoSuchElementException();
69          }
70      }
71  
72      /***
73       * Unsupported operation
74       * @see java.util.Iterator#remove()
75       * @throws UnsupportedOperationException
76       */
77      public void remove() {
78          throw new UnsupportedOperationException();
79      }
80  
81  }