View Javadoc

1   /*
2    * Created by IntelliJ IDEA.
3    * User: mike
4    * Date: Jan 22, 2002
5    * Time: 11:31:29 AM
6    * To change template for new class use
7    * Code Style | Class Templates options (Tools | IDE Options).
8    */
9   package com.atlassian.core.util.bean;
10  
11  import java.io.Serializable;
12  import java.util.ArrayList;
13  import java.util.Collection;
14  import java.util.Collections;
15  import java.util.List;
16  
17  /**
18   * This is a super class that implements paging for browsers.
19   *
20   * Most other filters (which want paging ability) will extend this.
21   */
22  public class PagerFilter implements Serializable
23  {
24      int max = 20;
25      int start = 0;
26  
27      /**
28       * Get the current page out of a list of items - won't work well on any collection that isn't a list
29       */
30      public Collection getCurrentPage(Collection itemsCol)
31      {
32          List items = null;
33  
34          if (itemsCol instanceof List)
35              items = (List) itemsCol;
36          else
37              items = new ArrayList(itemsCol); // should never call this but just incase!
38  
39          if (items == null || items.size() == 0)
40          {
41              start = 0;
42              return Collections.EMPTY_LIST;
43          }
44  
45          // now return the appropriate page of issues
46          // now make sure that the start is valid
47          if (start > items.size())
48          {
49              start = 0;
50              return items.subList(0, max);
51          }
52          else
53          {
54              return items.subList(start, Math.min(start + max, items.size()));
55          }
56      }
57  
58      public int getMax()
59      {
60          return max;
61      }
62  
63      public void setMax(int max)
64      {
65          this.max = max;
66      }
67  
68      public int getStart()
69      {
70          return start;
71      }
72  
73      public void setStart(int start)
74      {
75          this.start = start;
76      }
77  
78      public int getEnd()
79      {
80          return start + max;
81      }
82  
83      public int getNextStart()
84      {
85          return start + max;
86      }
87  
88      public int getPreviousStart()
89      {
90          return Math.max(0, start - max);
91      }
92  }