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