View Javadoc

1   package com.atlassian.plugins.rest.common.expand.entity;
2   
3   import com.atlassian.plugins.rest.common.expand.parameter.Indexes;
4   import com.google.common.collect.Lists;
5   
6   import java.util.List;
7   
8   public class ListWrapperCallBacks
9   {
10      public static <T> ListWrapperCallback<T> identity(final List<T> items)
11      {
12          return new ListWrapperCallback<T>()
13          {
14              public List<T> getItems(Indexes indexes)
15              {
16                  return items;
17              }
18          };
19      }
20  
21      public static <T> ListWrapperCallback<T> ofList(final List<T> items)
22      {
23          return ofList(items, Integer.MAX_VALUE);
24      }
25  
26      public static <T> ListWrapperCallback<T> ofList(final List<T> items, final int maxResults)
27      {
28          return new ListWrapperCallback<T>()
29          {
30              public List<T> getItems(Indexes indexes)
31              {
32                  final List<T> toReturn = Lists.newLinkedList();
33                  for (Integer i : indexes.getIndexes(items.size()))
34                  {
35                      if (i < items.size())
36                      {
37                          toReturn.add(items.get(i));
38                      }
39                      if (toReturn.size() == maxResults)
40                      {
41                          break;
42                      }
43                  }
44                  return toReturn;
45              }
46          };
47      }
48  }