View Javadoc

1   package net.sf.cantina;
2   
3   import java.util.List;
4   import java.util.Vector;
5   
6   /***
7    * @author Stephane JAIS
8    */
9   public abstract class ListIterator
10  {
11    private int index = 0;
12  
13    public abstract Object getElementAt(int index) throws Exception;
14  
15    public abstract int getSize();
16  
17    public Object next()
18      throws Exception
19    {
20      Object result = getElementAt(index);
21      index++;
22      return result;
23    }
24  
25    public Object previous()
26      throws Exception
27    {
28      Object result = getElementAt(--index);
29      return result;
30    }
31  
32    public void skipToPage(int pageSize, int pageNumber)
33    {
34      resetIndex();
35      index += pageSize * (pageNumber-1);
36    }
37  
38    public int getTotalPages(int pageSize)
39    {
40      int result = (int)Math.floor(getSize()/pageSize);
41      if (0 != ((getSize()+pageSize) % pageSize))
42        result++;
43      return result;
44    }
45  
46    protected List subList(int startIndex, int stopIndex)
47    {
48      List result = new Vector();
49      for (int i = startIndex; i < stopIndex; i++)
50      {
51        try
52        {
53          result.add(getElementAt(i));
54        } catch (Exception e)
55        {
56        }
57      }
58      return result;
59    }
60  
61    public List getPreviousElements(int count)
62    {
63      int startIndex = Math.max(0, index - count);
64      int stopIndex = index;
65      index = index - (stopIndex - startIndex);
66      return subList(startIndex, stopIndex);
67    }
68  
69    public List getNextElements(int count)
70    {
71      int startIndex = index;
72      int stopIndex = Math.min(getSize(), index + count);
73      index = index + (stopIndex - startIndex);
74      return subList(startIndex, stopIndex);
75    }
76  
77    public void resetIndex()
78    {
79      index = 0;
80    }
81  
82    public int getIndex()
83    {
84      return index;
85    }
86  
87    public boolean hasNext()
88    {
89      return index < getSize();
90    }
91  
92    public boolean hasPrevious()
93    {
94      return !(index == 0);
95    }
96  
97  }