View Javadoc

1   package com.atlassian.plugins.rest.common.expand.entity;
2   
3   import com.atlassian.plugins.rest.common.expand.parameter.Indexes;
4   
5   import static javax.xml.bind.annotation.XmlAccessType.FIELD;
6   import javax.xml.bind.annotation.XmlAccessorType;
7   import javax.xml.bind.annotation.XmlAttribute;
8   import javax.xml.bind.annotation.XmlRootElement;
9   import java.util.List;
10  
11  /**
12   * An abstract list wrapper that provides support for paging information:
13   * <ul>
14   * <li>size</li>
15   * <li>max-results</li>
16   * <li>start-index</li>
17   * </ul>
18   * @param <T> the type of element in the list to wrap.
19   */
20  @XmlRootElement
21  @XmlAccessorType(FIELD)
22  public abstract class AbstractPagedListWrapper<T> implements ListWrapper<T>
23  {
24      @XmlAttribute
25      private final int size;
26  
27      @XmlAttribute(name = "max-results")
28      private final int maxResults;
29  
30      @XmlAttribute(name = "start-index")
31      private Integer startIndex;
32  
33      // this is for JAXB
34      private AbstractPagedListWrapper()
35      {
36          size = 0;
37          maxResults = 0;
38      }
39  
40      protected AbstractPagedListWrapper(int size, int maxResults)
41      {
42          this.size = size;
43          this.maxResults = maxResults;
44      }
45  
46      public Integer getStartIndex()
47      {
48          return startIndex;
49      }
50  
51      public int getSize()
52      {
53          return size;
54      }
55  
56      public int getMaxResults()
57      {
58          return maxResults;
59      }
60  
61      public void setStartIndex(int startIndex)
62      {
63          this.startIndex = startIndex;
64      }
65  
66      public final ListWrapperCallback<T> getCallback()
67      {
68          return new ListWrapperCallback<T>()
69          {
70              public List<T> getItems(Indexes indexes)
71              {
72                  final int startIndex = indexes.getMinIndex(size);
73                  if (startIndex != -1)
74                  {
75                      setStartIndex(startIndex);
76                  }
77                  return getPagingCallback().getItems(indexes);
78              }
79          };
80      }
81  
82      /**
83       * Gets the call back that will actually "retrieve" the necessary element to populate the List. Size, index and max
84       * result attributes are already set by this abstract class, no need to worry about them.
85       * @return the call back that does all the work.
86       */
87      public abstract ListWrapperCallback<T> getPagingCallback();
88  }