1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
18  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
19  
20  import java.io.File;
21  import java.io.FileReader;
22  import java.io.IOException;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.Comparator;
29  import java.util.Enumeration;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Set;
33  import java.util.TreeSet;
34  
35  /**
36   * <a href="ListUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class ListUtil {
41  
42      public static <E> List<E> copy(List<E> master) {
43          if (master == null) {
44              return null;
45          }
46  
47          return new ArrayList<E>(master);
48      }
49  
50      public static <E> void copy(List<E> master, List<? super E> copy) {
51          if ((master == null) || (copy == null)) {
52              return;
53          }
54  
55          copy.clear();
56  
57          copy.addAll(master);
58      }
59  
60      public static void distinct(List<?> list) {
61          distinct(list, null);
62      }
63  
64      public static <E> void distinct(List<E> list, Comparator<E> comparator) {
65          if ((list == null) || list.isEmpty()) {
66              return;
67          }
68  
69          Set<E> set = null;
70  
71          if (comparator == null) {
72              set = new TreeSet<E>();
73          }
74          else {
75              set = new TreeSet<E>(comparator);
76          }
77  
78          Iterator<E> itr = list.iterator();
79  
80          while (itr.hasNext()) {
81              E obj = itr.next();
82  
83              if (set.contains(obj)) {
84                  itr.remove();
85              }
86              else {
87                  set.add(obj);
88              }
89          }
90      }
91  
92      public static <E> List<E> fromArray(E[] array) {
93          if ((array == null) || (array.length == 0)) {
94              return new ArrayList<E>();
95          }
96  
97          return new ArrayList<E>(Arrays.asList(array));
98      }
99  
100     @SuppressWarnings("unchecked")
101     public static <E> List<E> fromCollection(Collection<E> c) {
102         if ((c != null) && (List.class.isAssignableFrom(c.getClass()))) {
103             return (List)c;
104         }
105 
106         if ((c == null) || c.isEmpty()) {
107             return new ArrayList<E>();
108         }
109 
110         List<E> list = new ArrayList<E>(c.size());
111 
112         list.addAll(c);
113 
114         return list;
115     }
116 
117     public static <E> List<E> fromEnumeration(Enumeration<E> enu) {
118         List<E> list = new ArrayList<E>();
119 
120         while (enu.hasMoreElements()) {
121             E obj = enu.nextElement();
122 
123             list.add(obj);
124         }
125 
126         return list;
127     }
128 
129     public static List<String> fromFile(File file) throws IOException {
130         List<String> list = new ArrayList<String>();
131 
132         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
133             new FileReader(file));
134 
135         String s = StringPool.BLANK;
136 
137         while ((s = unsyncBufferedReader.readLine()) != null) {
138             list.add(s);
139         }
140 
141         unsyncBufferedReader.close();
142 
143         return list;
144     }
145 
146     public static List<String> fromFile(String fileName) throws IOException {
147         return fromFile(new File(fileName));
148     }
149 
150     public static List<String> fromString(String s) {
151         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
152     }
153 
154     public static <E> List<E> sort(List<E> list) {
155         return sort(list, null);
156     }
157 
158     public static <E> List<E> sort(
159         List<E> list, Comparator<? super E> comparator) {
160 
161         if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
162             list = copy(list);
163         }
164 
165         Collections.sort(list, comparator);
166 
167         return list;
168     }
169 
170     public static <E> List<E> subList(List<E> list, int start, int end) {
171         List<E> newList = new ArrayList<E>();
172 
173         int normalizedSize = list.size() - 1;
174 
175         if ((start < 0) || (start > normalizedSize) || (end < 0) ||
176             (start > end)) {
177 
178             return newList;
179         }
180 
181         for (int i = start; i < end && i <= normalizedSize; i++) {
182             newList.add(list.get(i));
183         }
184 
185         return newList;
186     }
187 
188     public static List<Boolean> toList(boolean[] array) {
189         if ((array == null) || (array.length == 0)) {
190             return Collections.emptyList();
191         }
192 
193         List<Boolean> list = new ArrayList<Boolean>(array.length);
194 
195         for (boolean value : array) {
196             list.add(value);
197         }
198 
199         return list;
200     }
201 
202     public static List<Double> toList(double[] array) {
203         if ((array == null) || (array.length == 0)) {
204             return Collections.emptyList();
205         }
206 
207         List<Double> list = new ArrayList<Double>(array.length);
208 
209         for (double value : array) {
210             list.add(value);
211         }
212 
213         return list;
214     }
215 
216     public static <E> List<E> toList(E[] array) {
217         if ((array == null) || (array.length == 0)) {
218             return Collections.emptyList();
219         }
220 
221         return new ArrayList<E>(Arrays.asList(array));
222     }
223 
224     public static List<Float> toList(float[] array) {
225         if ((array == null) || (array.length == 0)) {
226             return Collections.emptyList();
227         }
228 
229         List<Float> list = new ArrayList<Float>(array.length);
230 
231         for (float value : array) {
232             list.add(value);
233         }
234 
235         return list;
236     }
237 
238     public static List<Integer> toList(int[] array) {
239         if ((array == null) || (array.length == 0)) {
240             return Collections.emptyList();
241         }
242 
243         List<Integer> list = new ArrayList<Integer>(array.length);
244 
245         for (int value : array) {
246             list.add(value);
247         }
248 
249         return list;
250     }
251 
252     public static List<Long> toList(long[] array) {
253         if ((array == null) || (array.length == 0)) {
254             return Collections.emptyList();
255         }
256 
257         List<Long> list = new ArrayList<Long>(array.length);
258 
259         for (long value : array) {
260             list.add(value);
261         }
262 
263         return list;
264     }
265 
266     public static List<Short> toList(short[] array) {
267         if ((array == null) || (array.length == 0)) {
268             return Collections.emptyList();
269         }
270 
271         List<Short> list = new ArrayList<Short>(array.length);
272 
273         for (short value : array) {
274             list.add(value);
275         }
276 
277         return list;
278     }
279 
280     public static String toString(List<?> list, String param) {
281         return toString(list, param, StringPool.COMMA);
282     }
283 
284     public static String toString(
285         List<?> list, String param, String delimiter) {
286 
287         StringBundler sb = null;
288 
289         if (list.isEmpty()) {
290             sb = new StringBundler();
291         }
292         else {
293             sb = new StringBundler(2 * list.size() - 1);
294         }
295 
296         for (int i = 0; i < list.size(); i++) {
297             Object bean = list.get(i);
298 
299             Object value = BeanPropertiesUtil.getObject(bean, param);
300 
301             if (value == null) {
302                 value = StringPool.BLANK;
303             }
304 
305             sb.append(value.toString());
306 
307             if ((i + 1) != list.size()) {
308                 sb.append(delimiter);
309             }
310         }
311 
312         return sb.toString();
313     }
314 
315 }