1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.HashSet;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Set;
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 = new HashSet<E>();
70  
71          Iterator<E> itr = list.iterator();
72  
73          while (itr.hasNext()) {
74              E obj = itr.next();
75  
76              if (set.contains(obj)) {
77                  itr.remove();
78              }
79              else {
80                  set.add(obj);
81              }
82          }
83  
84          if (comparator != null) {
85              Collections.sort(list, comparator);
86          }
87      }
88  
89      public static <E> List<E> fromArray(E[] array) {
90          if ((array == null) || (array.length == 0)) {
91              return new ArrayList<E>();
92          }
93  
94          return new ArrayList<E>(Arrays.asList(array));
95      }
96  
97      @SuppressWarnings("rawtypes")
98      public static <E> List<E> fromCollection(Collection<E> c) {
99          if ((c != null) && (List.class.isAssignableFrom(c.getClass()))) {
100             return (List)c;
101         }
102 
103         if ((c == null) || c.isEmpty()) {
104             return new ArrayList<E>();
105         }
106 
107         List<E> list = new ArrayList<E>(c.size());
108 
109         list.addAll(c);
110 
111         return list;
112     }
113 
114     public static <E> List<E> fromEnumeration(Enumeration<E> enu) {
115         List<E> list = new ArrayList<E>();
116 
117         while (enu.hasMoreElements()) {
118             E obj = enu.nextElement();
119 
120             list.add(obj);
121         }
122 
123         return list;
124     }
125 
126     public static List<String> fromFile(File file) throws IOException {
127         List<String> list = new ArrayList<String>();
128 
129         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
130             new FileReader(file));
131 
132         String s = StringPool.BLANK;
133 
134         while ((s = unsyncBufferedReader.readLine()) != null) {
135             list.add(s);
136         }
137 
138         unsyncBufferedReader.close();
139 
140         return list;
141     }
142 
143     public static List<String> fromFile(String fileName) throws IOException {
144         return fromFile(new File(fileName));
145     }
146 
147     public static List<String> fromString(String s) {
148         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
149     }
150 
151     public static <E> boolean remove(List<E> list, E element) {
152         Iterator<E> itr = list.iterator();
153 
154         while (itr.hasNext()) {
155             E curElement = itr.next();
156 
157             if ((curElement == element) || curElement.equals(element)) {
158                 itr.remove();
159 
160                 return true;
161             }
162         }
163 
164         return false;
165     }
166 
167     public static <E> List<E> sort(List<E> list) {
168         return sort(list, null);
169     }
170 
171     public static <E> List<E> sort(
172         List<E> list, Comparator<? super E> comparator) {
173 
174         if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
175             list = copy(list);
176         }
177 
178         Collections.sort(list, comparator);
179 
180         return list;
181     }
182 
183     public static <E> List<E> subList(List<E> list, int start, int end) {
184         List<E> newList = new ArrayList<E>();
185 
186         int normalizedSize = list.size() - 1;
187 
188         if ((start < 0) || (start > normalizedSize) || (end < 0) ||
189             (start > end)) {
190 
191             return newList;
192         }
193 
194         for (int i = start; (i < end) && (i <= normalizedSize); i++) {
195             newList.add(list.get(i));
196         }
197 
198         return newList;
199     }
200 
201     public static List<Boolean> toList(boolean[] array) {
202         if ((array == null) || (array.length == 0)) {
203             return new ArrayList<Boolean>();
204         }
205 
206         List<Boolean> list = new ArrayList<Boolean>(array.length);
207 
208         for (boolean value : array) {
209             list.add(value);
210         }
211 
212         return list;
213     }
214 
215     public static List<Double> toList(double[] array) {
216         if ((array == null) || (array.length == 0)) {
217             return new ArrayList<Double>();
218         }
219 
220         List<Double> list = new ArrayList<Double>(array.length);
221 
222         for (double value : array) {
223             list.add(value);
224         }
225 
226         return list;
227     }
228 
229     public static <E> List<E> toList(E[] array) {
230         if ((array == null) || (array.length == 0)) {
231             return new ArrayList<E>();
232         }
233 
234         return new ArrayList<E>(Arrays.asList(array));
235     }
236 
237     public static List<Float> toList(float[] array) {
238         if ((array == null) || (array.length == 0)) {
239             return new ArrayList<Float>();
240         }
241 
242         List<Float> list = new ArrayList<Float>(array.length);
243 
244         for (float value : array) {
245             list.add(value);
246         }
247 
248         return list;
249     }
250 
251     public static List<Integer> toList(int[] array) {
252         if ((array == null) || (array.length == 0)) {
253             return new ArrayList<Integer>();
254         }
255 
256         List<Integer> list = new ArrayList<Integer>(array.length);
257 
258         for (int value : array) {
259             list.add(value);
260         }
261 
262         return list;
263     }
264 
265     public static List<Long> toList(long[] array) {
266         if ((array == null) || (array.length == 0)) {
267             return new ArrayList<Long>();
268         }
269 
270         List<Long> list = new ArrayList<Long>(array.length);
271 
272         for (long value : array) {
273             list.add(value);
274         }
275 
276         return list;
277     }
278 
279     public static List<Short> toList(short[] array) {
280         if ((array == null) || (array.length == 0)) {
281             return new ArrayList<Short>();
282         }
283 
284         List<Short> list = new ArrayList<Short>(array.length);
285 
286         for (short value : array) {
287             list.add(value);
288         }
289 
290         return list;
291     }
292 
293     public static String toString(List<?> list, String param) {
294         return toString(list, param, StringPool.COMMA);
295     }
296 
297     public static String toString(
298         List<?> list, String param, String delimiter) {
299 
300         StringBundler sb = null;
301 
302         if (list.isEmpty()) {
303             sb = new StringBundler();
304         }
305         else {
306             sb = new StringBundler(2 * list.size() - 1);
307         }
308 
309         for (int i = 0; i < list.size(); i++) {
310             Object bean = list.get(i);
311 
312             Object value = BeanPropertiesUtil.getObject(bean, param);
313 
314             if (value == null) {
315                 value = StringPool.BLANK;
316             }
317 
318             sb.append(value.toString());
319 
320             if ((i + 1) != list.size()) {
321                 sb.append(delimiter);
322             }
323         }
324 
325         return sb.toString();
326     }
327 
328 }