View Javadoc

1   package com.atlassian.user.search.page;
2   
3   import java.util.*;
4   
5   public class DefaultPager<T> implements Pager<T>
6   {
7       private final List<T> page = new ArrayList<T>();
8       private Iterator<T> iter;
9       private int index;
10  
11      public static <T> DefaultPager<T> emptyPager()
12      {
13          return new DefaultPager<T>(Collections.<T>emptyList());
14      }
15  
16      /** @deprecated since 2.0.3. Use the generic-friendly emptyPager() method */
17      public DefaultPager()
18      {
19          this(Collections.<T>emptyList());
20      }
21  
22      public DefaultPager(Collection<T> col)
23      {
24          if (col != null)
25          {
26              page.addAll(col);
27          }
28  
29          iter = page.iterator();
30      }
31  
32      public boolean isEmpty()
33      {
34          if (page == null) return true;
35  
36          return page.isEmpty();
37      }
38  
39      public Iterator<T> iterator()
40      {
41          return iter;
42      }
43  
44      /**
45       * @return a single, preloaded page.
46       */
47      public List<T> getCurrentPage()
48      {
49          return new ArrayList<T>(page);
50      }
51  
52      public void nextPage()
53      {
54          //do nothing, all contents are already within the currentPage
55      }
56  
57      public boolean onLastPage()
58      {
59          return true;
60      }
61  
62      public void skipTo(int index) throws PagerException
63      {
64          if (index < 0)
65              throw new PagerException("Cannot skipTo a negative amount [" + index + "]");
66  
67          int originalIndex = this.index;
68          int distance;
69  
70          if (index > page.size())
71          {
72              distance = page.size();
73              this.index = page.size();
74          }
75          else
76          {
77              distance = index - this.index;
78              this.index = index;
79          }
80  
81          for (int i = originalIndex; i < distance; i++)
82              iter.next();
83      }
84  
85      /**
86       * @return the current index position of the pager
87       */
88      public int getIndex()
89      {
90          return index;
91      }
92  
93      /**
94       *  This pager always has all its items in a single page
95       * @return zero, because this pager has all its items in a single page
96       */
97      public int getIndexOfFirstItemInCurrentPage()
98      {
99          return 0;
100     }
101 
102     public void remove()
103     {
104         throw new UnsupportedOperationException("This iterator does not support removal");
105     }
106 
107     public boolean hasNext()
108     {
109         return iter.hasNext();
110     }
111 
112     public T next()
113     {
114         T o = iter.next();
115         index++;
116 
117         return o;
118     }
119 }