1
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
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 List<Boolean> toList(boolean[] array) {
199 if ((array == null) || (array.length == 0)) {
200 return Collections.EMPTY_LIST;
201 }
202
203 List<Boolean> list = new ArrayList<Boolean>(array.length);
204
205 for (boolean value : array) {
206 list.add(value);
207 }
208
209 return list;
210 }
211
212 public static List<Double> toList(double[] array) {
213 if ((array == null) || (array.length == 0)) {
214 return Collections.EMPTY_LIST;
215 }
216
217 List<Double> list = new ArrayList<Double>(array.length);
218
219 for (double value : array) {
220 list.add(value);
221 }
222
223 return list;
224 }
225
226 public static List<Float> toList(float[] array) {
227 if ((array == null) || (array.length == 0)) {
228 return Collections.EMPTY_LIST;
229 }
230
231 List<Float> list = new ArrayList<Float>(array.length);
232
233 for (float value : array) {
234 list.add(value);
235 }
236
237 return list;
238 }
239
240 public static List<Integer> toList(int[] array) {
241 if ((array == null) || (array.length == 0)) {
242 return Collections.EMPTY_LIST;
243 }
244
245 List<Integer> list = new ArrayList<Integer>(array.length);
246
247 for (int value : array) {
248 list.add(value);
249 }
250
251 return list;
252 }
253
254 public static List<Long> toList(long[] array) {
255 if ((array == null) || (array.length == 0)) {
256 return Collections.EMPTY_LIST;
257 }
258
259 List<Long> list = new ArrayList<Long>(array.length);
260
261 for (long value : array) {
262 list.add(value);
263 }
264
265 return list;
266 }
267
268 public static List<Short> toList(short[] array) {
269 if ((array == null) || (array.length == 0)) {
270 return Collections.EMPTY_LIST;
271 }
272
273 List<Short> list = new ArrayList<Short>(array.length);
274
275 for (short value : array) {
276 list.add(value);
277 }
278
279 return list;
280 }
281
282 public static List<Boolean> toList(Boolean[] array) {
283 if ((array == null) || (array.length == 0)) {
284 return Collections.EMPTY_LIST;
285 }
286
287 List<Boolean> list = new ArrayList<Boolean>(array.length);
288
289 for (Boolean value : array) {
290 list.add(value);
291 }
292
293 return list;
294 }
295
296 public static List<Double> toList(Double[] array) {
297 if ((array == null) || (array.length == 0)) {
298 return Collections.EMPTY_LIST;
299 }
300
301 List<Double> list = new ArrayList<Double>(array.length);
302
303 for (Double value : array) {
304 list.add(value);
305 }
306
307 return list;
308 }
309
310 public static List<Float> toList(Float[] array) {
311 if ((array == null) || (array.length == 0)) {
312 return Collections.EMPTY_LIST;
313 }
314
315 List<Float> list = new ArrayList<Float>(array.length);
316
317 for (Float value : array) {
318 list.add(value);
319 }
320
321 return list;
322 }
323
324 public static List<Integer> toList(Integer[] array) {
325 if ((array == null) || (array.length == 0)) {
326 return Collections.EMPTY_LIST;
327 }
328
329 List<Integer> list = new ArrayList<Integer>(array.length);
330
331 for (Integer value : array) {
332 list.add(value);
333 }
334
335 return list;
336 }
337
338 public static List<Long> toList(Long[] array) {
339 if ((array == null) || (array.length == 0)) {
340 return Collections.EMPTY_LIST;
341 }
342
343 List<Long> list = new ArrayList<Long>(array.length);
344
345 for (Long value : array) {
346 list.add(value);
347 }
348
349 return list;
350 }
351
352 public static List<Short> toList(Short[] array) {
353 if ((array == null) || (array.length == 0)) {
354 return Collections.EMPTY_LIST;
355 }
356
357 List<Short> list = new ArrayList<Short>(array.length);
358
359 for (Short value : array) {
360 list.add(value);
361 }
362
363 return list;
364 }
365
366 public static List<String> toList(String[] array) {
367 if ((array == null) || (array.length == 0)) {
368 return Collections.EMPTY_LIST;
369 }
370
371 List<String> list = new ArrayList<String>(array.length);
372
373 for (String value : array) {
374 list.add(value);
375 }
376
377 return list;
378 }
379
380 public static String toString(List list, String param) {
381 return toString(list, param, StringPool.COMMA);
382 }
383
384 public static String toString(List list, String param, String delimiter) {
385 StringBuilder sb = new StringBuilder();
386
387 for (int i = 0; i < list.size(); i++) {
388 Object bean = list.get(i);
389
390 Object value = BeanPropertiesUtil.getObject(bean, param);
391
392 if (value == null) {
393 value = StringPool.BLANK;
394 }
395
396 sb.append(value.toString());
397
398 if ((i + 1) != list.size()) {
399 sb.append(delimiter);
400 }
401 }
402
403 return sb.toString();
404 }
405
406 }