1   /**
2    * Copyright (c) 2000-2008 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          List copy = new ArrayList(master.size());
56  
57          copy(master, copy);
58  
59          return copy;
60      }
61  
62      public static void copy(List master, List copy) {
63          if ((master == null) || (copy == null)) {
64              return;
65          }
66  
67          copy.clear();
68  
69          Iterator itr = master.iterator();
70  
71          while (itr.hasNext()) {
72              Object obj = itr.next();
73  
74              copy.add(obj);
75          }
76      }
77  
78      public static void distinct(List list) {
79          distinct(list, null);
80      }
81  
82      public static void distinct(List list, Comparator comparator) {
83          if ((list == null) || (list.size() == 0)) {
84              return;
85          }
86  
87          Set set = null;
88  
89          if (comparator == null) {
90              set = new TreeSet();
91          }
92          else {
93              set = new TreeSet(comparator);
94          }
95  
96          Iterator itr = list.iterator();
97  
98          while (itr.hasNext()) {
99              Object obj = itr.next();
100 
101             if (set.contains(obj)) {
102                 itr.remove();
103             }
104             else {
105                 set.add(obj);
106             }
107         }
108     }
109 
110     public static List fromArray(Object[] array) {
111         if ((array == null) || (array.length == 0)) {
112             return new ArrayList();
113         }
114 
115         List list = new ArrayList(array.length);
116 
117         for (int i = 0; i < array.length; i++) {
118             list.add(array[i]);
119         }
120 
121         return list;
122     }
123 
124     public static List fromCollection(Collection c) {
125         if ((c != null) && (c instanceof List)) {
126             return (List)c;
127         }
128 
129         if ((c == null) || (c.size() == 0)) {
130             return new ArrayList();
131         }
132 
133         List list = new ArrayList(c.size());
134 
135         Iterator itr = c.iterator();
136 
137         while (itr.hasNext()) {
138             list.add(itr.next());
139         }
140 
141         return list;
142     }
143 
144     public static List fromEnumeration(Enumeration enu) {
145         List list = new ArrayList();
146 
147         while (enu.hasMoreElements()) {
148             Object obj = enu.nextElement();
149 
150             list.add(obj);
151         }
152 
153         return list;
154     }
155 
156     public static List fromFile(String fileName) throws IOException {
157         return fromFile(new File(fileName));
158     }
159 
160     public static List fromFile(File file) throws IOException {
161         List list = new ArrayList();
162 
163         BufferedReader br = new BufferedReader(new FileReader(file));
164 
165         String s = StringPool.BLANK;
166 
167         while ((s = br.readLine()) != null) {
168             list.add(s);
169         }
170 
171         br.close();
172 
173         return list;
174     }
175 
176     public static List fromString(String s) {
177         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
178     }
179 
180     public static List subList(List list, int start, int end) {
181         List newList = new ArrayList();
182 
183         int normalizedSize = list.size() - 1;
184 
185         if ((start < 0) || (start > normalizedSize) || (end < 0) ||
186             (start > end)) {
187 
188             return newList;
189         }
190 
191         for (int i = start; i < end && i <= normalizedSize; i++) {
192             newList.add(list.get(i));
193         }
194 
195         return newList;
196     }
197 
198     public static String toString(List list, String param) {
199         return toString(list, param, StringPool.COMMA);
200     }
201 
202     public static String toString(List list, String param, String delimiter) {
203         StringBuilder sb = new StringBuilder();
204 
205         for (int i = 0; i < list.size(); i++) {
206             Object bean = list.get(i);
207 
208             Object value = BeanPropertiesUtil.getObject(bean, param);
209 
210             if (value == null) {
211                 value = StringPool.BLANK;
212             }
213 
214             sb.append(value.toString());
215 
216             if ((i + 1) != list.size()) {
217                 sb.append(delimiter);
218             }
219         }
220 
221         return sb.toString();
222     }
223 
224     public static List<Boolean> toList(Boolean[] list) {
225         if ((list == null) || (list.length == 0)) {
226             return Collections.EMPTY_LIST;
227         }
228 
229         List<Boolean> newList = new ArrayList<Boolean>(list.length);
230 
231         for (Boolean value : list) {
232             newList.add(value);
233         }
234 
235         return newList;
236     }
237 
238     public static List<Double> toList(Double[] list) {
239         if ((list == null) || (list.length == 0)) {
240             return Collections.EMPTY_LIST;
241         }
242 
243         List<Double> newList = new ArrayList<Double>(list.length);
244 
245         for (Double value : list) {
246             newList.add(value);
247         }
248 
249         return newList;
250     }
251 
252     public static List<Float> toList(Float[] list) {
253         if ((list == null) || (list.length == 0)) {
254             return Collections.EMPTY_LIST;
255         }
256 
257         List<Float> newList = new ArrayList<Float>(list.length);
258 
259         for (Float value : list) {
260             newList.add(value);
261         }
262 
263         return newList;
264     }
265 
266     public static List<Integer> toList(Integer[] list) {
267         if ((list == null) || (list.length == 0)) {
268             return Collections.EMPTY_LIST;
269         }
270 
271         List<Integer> newList = new ArrayList<Integer>(list.length);
272 
273         for (Integer value : list) {
274             newList.add(value);
275         }
276 
277         return newList;
278     }
279 
280     public static List<Long> toList(Long[] list) {
281         if ((list == null) || (list.length == 0)) {
282             return Collections.EMPTY_LIST;
283         }
284 
285         List<Long> newList = new ArrayList<Long>(list.length);
286 
287         for (Long value : list) {
288             newList.add(value);
289         }
290 
291         return newList;
292     }
293 
294     public static List<Short> toList(Short[] list) {
295         if ((list == null) || (list.length == 0)) {
296             return Collections.EMPTY_LIST;
297         }
298 
299         List<Short> newList = new ArrayList<Short>(list.length);
300 
301         for (Short value : list) {
302             newList.add(value);
303         }
304 
305         return newList;
306     }
307 
308     public static List<String> toList(String[] list) {
309         if ((list == null) || (list.length == 0)) {
310             return Collections.EMPTY_LIST;
311         }
312 
313         List<String> newList = new ArrayList<String>(list.length);
314 
315         for (String value : list) {
316             newList.add(value);
317         }
318 
319         return newList;
320     }
321 
322 }