View Javadoc

1   package com.atlassian.core.util.collection;
2   
3   import com.atlassian.core.util.ObjectUtils;
4   import org.apache.commons.collections.CollectionUtils;
5   
6   import java.util.ArrayList;
7   import java.util.Collection;
8   import java.util.Collections;
9   import java.util.List;
10  
11  /**
12   * A replacement for UtilMisc.toList(). <p/> Most methods here are not null safe
13   *
14   * @deprecated use the appropriate methods on Guava's {@code ImmutableList}, {@code Lists}, or {@code Iterables}
15   */
16  @Deprecated
17  public class EasyList
18  {
19  
20      /**
21       * Creates a list with one null value. Occasionally useful.
22       * @return a list with one null value.
23       */
24      public static <T> List<T> buildNull()
25      {
26          List<T> list = createList(1);
27          list.add(null);
28          return list;
29      }
30  
31      public static <T> List<T> build(T[] array)
32      {
33          List<T> list = createList(array.length);
34  
35          Collections.addAll(list, array);
36  
37          return list;
38      }
39  
40      public static <T> List<T> buildNonNull(T[] array)
41      {
42          List<T> list;
43          if (array != null && array.length > 0)
44          {
45              list = createList(array.length);
46              for (T o : array)
47              {
48                  if (ObjectUtils.isNotEmpty(o))
49                  {
50                      list.add(o);
51                  }
52              }
53          }
54          else
55          {
56              list = Collections.emptyList();
57          }
58  
59          return list;
60      }
61  
62      public static <T> List<T> buildNonNull(Collection<T> c)
63      {
64          List<T> list;
65          if (c != null && !c.isEmpty())
66          {
67              list = build(CollectionUtils.predicatedCollection(c, ObjectUtils.getIsSetPredicate()));
68          }
69          else
70          {
71              list = Collections.emptyList();
72          }
73  
74          return list;
75      }
76  
77      public static <T> List<T> buildNonNull(T o)
78      {
79          if (ObjectUtils.isNotEmpty(o))
80          {
81              return build(o);
82          }
83          else
84          {
85              return build();
86          }
87      }
88  
89      public static <T> List<T> build()
90      {
91          return Collections.emptyList();
92      }
93  
94      public static <T> List<T> build(T o1)
95      {
96          List<T> list = createList(1);
97  
98          list.add(o1);
99  
100         return list;
101     }
102 
103     public static <A, B extends A> List<A> build(A o1, B o2)
104     {
105         List<A> list = createList(2);
106 
107         list.add(o1);
108         list.add(o2);
109 
110         return list;
111     }
112 
113     public static <A, B extends A, C extends A> List<A> build(A o1, B o2, C o3)
114     {
115         List<A> list = createList(3);
116 
117         list.add(o1);
118         list.add(o2);
119         list.add(o3);
120 
121         return list;
122     }
123 
124     public static <A, B extends A, C extends A, D extends A> List<A> build(A o1, B o2, C o3, D o4)
125     {
126         List<A> list = createList(4);
127 
128         list.add(o1);
129         list.add(o2);
130         list.add(o3);
131         list.add(o4);
132 
133         return list;
134     }
135 
136     public static <A, B extends A, C extends A, D extends A, E extends A> List<A> build(A o1, B o2, C o3, D o4, E... others)
137     {
138         List<A> list = createList(5);
139 
140         list.add(o1);
141         list.add(o2);
142         list.add(o3);
143         list.add(o4);
144         Collections.addAll(list, others);
145 
146         return list;
147     }
148 
149     public static <T> List<T> build(Collection<T> collection)
150     {
151         if (collection == null) return null;
152         return new ArrayList<T>(collection);
153     }
154 
155     public static <T> List<T> createList(int size)
156     {
157         return new ArrayList<T>(size);
158     }
159 
160     /**
161      * Merge a maximum of three lists.
162      * Null lists passed in will be ignored.
163      *
164      * @param a The first list.
165      * @param b The second list.
166      * @param c The third list.
167      * @return A merged list containing the objects of all three passed in lists.
168      * @deprecated use Guava's {@code Iterables.concat()}
169      */
170     public static <T, U extends T, V extends T> List<T> mergeLists(List<T> a, List<U> b, List<V> c)
171     {
172         List<T> d = EasyList.createList(0);
173         if (a != null)
174         {
175             d.addAll(a);
176         }
177         if (b != null)
178         {
179             d.addAll(b);
180         }
181         if (c != null)
182         {
183             d.addAll(c);
184         }
185         return d;
186     }
187 
188     /**
189      * Splits a list into a number of sublists of the correct length. Note this will create a 'shallow' split, in other
190      * words if you set/remove on the sublists, this will modify the parent list as well (same vice versa). Therefore,
191      * DO NOT publish the result of this method to clients as the you will be publishing read/write access to your
192      * underlying data and the results will be unpredictable, especially in a multi-threaded context.
193      * 
194      * @param list
195      *            The list to split
196      * @param sublength
197      *            Length of the sublists
198      * @return A list of lists of the correct length
199      * @deprecated use Guava's {@code Lists.partition()}
200      */
201     public static <T> List<List<T>> shallowSplit(List<T> list, int sublength)
202     {
203         // if we have any remainder, then we will add one to our division to correctly size the result list
204         int overflow = ((list.size() % sublength) > 0) ? 1 : 0;
205         List<List<T>> result = new ArrayList<List<T>>((list.size() / sublength) + overflow);
206         int i = 0;
207         while (i < list.size())
208         {
209             int endIndex = (i + sublength) > list.size() ? list.size() : i + sublength;
210             result.add(list.subList(i, endIndex));
211             i += sublength;
212         }
213 
214         return result;
215     }
216 }