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