1 package com.atlassian.plugins.rest.doclet.generators.schema.beans.attachment;
2
3 import com.atlassian.plugins.rest.common.expand.entity.ListWrapper;
4 import com.atlassian.plugins.rest.common.expand.entity.ListWrapperCallback;
5 import com.atlassian.plugins.rest.common.expand.parameter.Indexes;
6 import com.google.common.collect.ImmutableList;
7 import com.google.common.collect.Lists;
8
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.List;
12 import javax.xml.bind.annotation.XmlAttribute;
13 import javax.xml.bind.annotation.XmlElement;
14 import javax.xml.bind.annotation.XmlRootElement;
15 import javax.xml.bind.annotation.XmlTransient;
16
17 import static java.util.Collections.emptyList;
18
19
20
21
22
23
24
25
26
27
28
29 @XmlRootElement (name = "list")
30 public class SimpleListWrapper<T> implements ListWrapper<T>
31 {
32
33
34
35
36
37
38 public static <T> SimpleListWrapper<T> empty()
39 {
40 return of(Collections.<T>emptyList());
41 }
42
43
44
45
46
47
48
49
50 public static <T> SimpleListWrapper<T> of(T... elements)
51 {
52 return of(Arrays.asList(elements));
53 }
54
55
56
57
58
59
60
61
62 public static <T> SimpleListWrapper<T> of(List<T> list)
63 {
64 return of(list, null);
65 }
66
67
68
69
70
71
72
73
74
75
76 public static <T> SimpleListWrapper<T> of(List<T> list, Integer maxResults)
77 {
78 return new SimpleListWrapper<T>(list, maxResults, list.size());
79 }
80
81
82
83
84
85
86
87
88
89
90
91 public static <T> SimpleListWrapper<T> of(List<T> list, Integer maxResults, int size)
92 {
93 return new SimpleListWrapper<T>(list, maxResults, size);
94 }
95
96
97
98
99 @XmlAttribute (name = "size")
100 private int size;
101
102
103
104
105 @XmlAttribute (name = "max-results")
106 private Integer maxResults;
107
108
109
110
111
112
113
114 @XmlElement (name = "items")
115 private List<T> items = Collections.emptyList();
116
117
118
119
120 @XmlTransient
121 private ListWrapperCallback<T> pagingCallback;
122
123
124
125
126 private SimpleListWrapper()
127 {
128
129 }
130
131
132
133
134
135
136
137
138 protected SimpleListWrapper(List<T> list, Integer maxResults, int size)
139 {
140 this.size = size;
141 this.maxResults = maxResults;
142 this.pagingCallback = ofList(ImmutableList.copyOf(list), maxResults != null ? maxResults : Integer.MAX_VALUE);
143 }
144
145
146
147
148
149
150 public ListWrapperCallback<T> getPagingCallback()
151 {
152 return pagingCallback;
153 }
154
155 public int getSize()
156 {
157 return size;
158 }
159
160 public int getMaxResults()
161 {
162 return maxResults;
163 }
164
165 public final ListWrapperCallback<T> getCallback()
166 {
167 return new ListWrapperCallback<T>()
168 {
169 public List<T> getItems(Indexes indexes)
170 {
171 return getPagingCallback().getItems(indexes);
172 }
173 };
174 }
175
176 @Override
177 public String toString()
178 {
179 return "SimpleListWrapper{" +
180 "size=" + size +
181 ", maxResults=" + maxResults +
182 ", pagingCallback=" + pagingCallback +
183 ", items=" + items +
184 '}';
185 }
186
187
188
189
190
191
192
193
194
195
196 static <T> ListWrapperCallback<T> ofList(final List<T> items, final int maxResults)
197 {
198 if (items == null) { throw new NullPointerException("items"); }
199 if (maxResults < 0) { throw new IllegalArgumentException("maxResults must be non-negative: " + maxResults); }
200
201 return new ListWrapperCallback<T>()
202 {
203 public List<T> getItems(Indexes indexes)
204 {
205 if (maxResults == 0)
206 {
207 return emptyList();
208 }
209
210 int remainingResults = maxResults;
211 final List<T> toReturn = Lists.newArrayListWithCapacity(Math.min(items.size(), maxResults));
212
213 for (Integer i : indexes.getIndexes(items.size()))
214 {
215 if (remainingResults-- == 0) { break; }
216 toReturn.add(items.get(i));
217 }
218
219 return toReturn;
220 }
221 };
222 }
223 }