1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
26  
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileReader;
30  import java.io.IOException;
31  
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.Comparator;
36  import java.util.Enumeration;
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.Set;
40  import java.util.TreeSet;
41  
42  /**
43   * <a href="ListUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class ListUtil {
49  
50      public static List copy(List master) {
51          if (master == null) {
52              return null;
53          }
54  
55          return new ArrayList(master);
56      }
57  
58      public static void copy(List master, List copy) {
59          if ((master == null) || (copy == null)) {
60              return;
61          }
62  
63          copy.clear();
64  
65          Iterator itr = master.iterator();
66  
67          while (itr.hasNext()) {
68              Object obj = itr.next();
69  
70              copy.add(obj);
71          }
72      }
73  
74      public static void distinct(List list) {
75          distinct(list, null);
76      }
77  
78      public static void distinct(List list, Comparator comparator) {
79          if ((list == null) || (list.size() == 0)) {
80              return;
81          }
82  
83          Set<Object> set = null;
84  
85          if (comparator == null) {
86              set = new TreeSet<Object>();
87          }
88          else {
89              set = new TreeSet<Object>(comparator);
90          }
91  
92          Iterator<Object> itr = list.iterator();
93  
94          while (itr.hasNext()) {
95              Object obj = itr.next();
96  
97              if (set.contains(obj)) {
98                  itr.remove();
99              }
100             else {
101                 set.add(obj);
102             }
103         }
104     }
105 
106     public static List fromArray(Object[] array) {
107         if ((array == null) || (array.length == 0)) {
108             return new ArrayList();
109         }
110 
111         List list = new ArrayList(array.length);
112 
113         for (int i = 0; i < array.length; i++) {
114             list.add(array[i]);
115         }
116 
117         return list;
118     }
119 
120     public static List fromCollection(Collection c) {
121         if ((c != null) && (c instanceof List)) {
122             return (List)c;
123         }
124 
125         if ((c == null) || (c.size() == 0)) {
126             return new ArrayList();
127         }
128 
129         List list = new ArrayList(c.size());
130 
131         Iterator itr = c.iterator();
132 
133         while (itr.hasNext()) {
134             list.add(itr.next());
135         }
136 
137         return list;
138     }
139 
140     public static List fromEnumeration(Enumeration enu) {
141         List list = new ArrayList();
142 
143         while (enu.hasMoreElements()) {
144             Object obj = enu.nextElement();
145 
146             list.add(obj);
147         }
148 
149         return list;
150     }
151 
152     public static List fromFile(String fileName) throws IOException {
153         return fromFile(new File(fileName));
154     }
155 
156     public static List fromFile(File file) throws IOException {
157         List list = new ArrayList();
158 
159         BufferedReader br = new BufferedReader(new FileReader(file));
160 
161         String s = StringPool.BLANK;
162 
163         while ((s = br.readLine()) != null) {
164             list.add(s);
165         }
166 
167         br.close();
168 
169         return list;
170     }
171 
172     public static List fromString(String s) {
173         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
174     }
175 
176     public static List sort(List list) {
177         return sort(list, null);
178     }
179 
180     public static List sort(List list, Comparator comparator) {
181         if (list instanceof UnmodifiableList) {
182             list = copy(list);
183         }
184 
185         Collections.sort(list, comparator);
186 
187         return list;
188     }
189 
190     public static List subList(List list, int start, int end) {
191         List newList = new ArrayList();
192 
193         int normalizedSize = list.size() - 1;
194 
195         if ((start < 0) || (start > normalizedSize) || (end < 0) ||
196             (start > end)) {
197 
198             return newList;
199         }
200 
201         for (int i = start; i < end && i <= normalizedSize; i++) {
202             newList.add(list.get(i));
203         }
204 
205         return newList;
206     }
207 
208     public static List<Boolean> toList(boolean[] array) {
209         if ((array == null) || (array.length == 0)) {
210             return Collections.EMPTY_LIST;
211         }
212 
213         List<Boolean> list = new ArrayList<Boolean>(array.length);
214 
215         for (boolean value : array) {
216             list.add(value);
217         }
218 
219         return list;
220     }
221 
222     public static List<Double> toList(double[] array) {
223         if ((array == null) || (array.length == 0)) {
224             return Collections.EMPTY_LIST;
225         }
226 
227         List<Double> list = new ArrayList<Double>(array.length);
228 
229         for (double value : array) {
230             list.add(value);
231         }
232 
233         return list;
234     }
235 
236     public static List<Float> toList(float[] array) {
237         if ((array == null) || (array.length == 0)) {
238             return Collections.EMPTY_LIST;
239         }
240 
241         List<Float> list = new ArrayList<Float>(array.length);
242 
243         for (float value : array) {
244             list.add(value);
245         }
246 
247         return list;
248     }
249 
250     public static List<Integer> toList(int[] array) {
251         if ((array == null) || (array.length == 0)) {
252             return Collections.EMPTY_LIST;
253         }
254 
255         List<Integer> list = new ArrayList<Integer>(array.length);
256 
257         for (int value : array) {
258             list.add(value);
259         }
260 
261         return list;
262     }
263 
264     public static List<Long> toList(long[] array) {
265         if ((array == null) || (array.length == 0)) {
266             return Collections.EMPTY_LIST;
267         }
268 
269         List<Long> list = new ArrayList<Long>(array.length);
270 
271         for (long value : array) {
272             list.add(value);
273         }
274 
275         return list;
276     }
277 
278     public static List<Short> toList(short[] array) {
279         if ((array == null) || (array.length == 0)) {
280             return Collections.EMPTY_LIST;
281         }
282 
283         List<Short> list = new ArrayList<Short>(array.length);
284 
285         for (short value : array) {
286             list.add(value);
287         }
288 
289         return list;
290     }
291 
292     public static List<Boolean> toList(Boolean[] array) {
293         if ((array == null) || (array.length == 0)) {
294             return Collections.EMPTY_LIST;
295         }
296 
297         List<Boolean> list = new ArrayList<Boolean>(array.length);
298 
299         for (Boolean value : array) {
300             list.add(value);
301         }
302 
303         return list;
304     }
305 
306     public static List<Double> toList(Double[] array) {
307         if ((array == null) || (array.length == 0)) {
308             return Collections.EMPTY_LIST;
309         }
310 
311         List<Double> list = new ArrayList<Double>(array.length);
312 
313         for (Double value : array) {
314             list.add(value);
315         }
316 
317         return list;
318     }
319 
320     public static List<Float> toList(Float[] array) {
321         if ((array == null) || (array.length == 0)) {
322             return Collections.EMPTY_LIST;
323         }
324 
325         List<Float> list = new ArrayList<Float>(array.length);
326 
327         for (Float value : array) {
328             list.add(value);
329         }
330 
331         return list;
332     }
333 
334     public static List<Integer> toList(Integer[] array) {
335         if ((array == null) || (array.length == 0)) {
336             return Collections.EMPTY_LIST;
337         }
338 
339         List<Integer> list = new ArrayList<Integer>(array.length);
340 
341         for (Integer value : array) {
342             list.add(value);
343         }
344 
345         return list;
346     }
347 
348     public static List<Long> toList(Long[] array) {
349         if ((array == null) || (array.length == 0)) {
350             return Collections.EMPTY_LIST;
351         }
352 
353         List<Long> list = new ArrayList<Long>(array.length);
354 
355         for (Long value : array) {
356             list.add(value);
357         }
358 
359         return list;
360     }
361 
362     public static List<Short> toList(Short[] array) {
363         if ((array == null) || (array.length == 0)) {
364             return Collections.EMPTY_LIST;
365         }
366 
367         List<Short> list = new ArrayList<Short>(array.length);
368 
369         for (Short value : array) {
370             list.add(value);
371         }
372 
373         return list;
374     }
375 
376     public static List<String> toList(String[] array) {
377         if ((array == null) || (array.length == 0)) {
378             return Collections.EMPTY_LIST;
379         }
380 
381         List<String> list = new ArrayList<String>(array.length);
382 
383         for (String value : array) {
384             list.add(value);
385         }
386 
387         return list;
388     }
389 
390     public static String toString(List list, String param) {
391         return toString(list, param, StringPool.COMMA);
392     }
393 
394     public static String toString(List list, String param, String delimiter) {
395         StringBuilder sb = new StringBuilder();
396 
397         for (int i = 0; i < list.size(); i++) {
398             Object bean = list.get(i);
399 
400             Object value = BeanPropertiesUtil.getObject(bean, param);
401 
402             if (value == null) {
403                 value = StringPool.BLANK;
404             }
405 
406             sb.append(value.toString());
407 
408             if ((i + 1) != list.size()) {
409                 sb.append(delimiter);
410             }
411         }
412 
413         return sb.toString();
414     }
415 
416 }