1
19
20 package com.liferay.portal.kernel.util;
21
22 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.Enumeration;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.TreeSet;
38
39
45 public class ListUtil {
46
47 public static List copy(List master) {
48 if (master == null) {
49 return null;
50 }
51
52 return new ArrayList(master);
53 }
54
55 public static void copy(List master, List copy) {
56 if ((master == null) || (copy == null)) {
57 return;
58 }
59
60 copy.clear();
61
62 Iterator itr = master.iterator();
63
64 while (itr.hasNext()) {
65 Object obj = itr.next();
66
67 copy.add(obj);
68 }
69 }
70
71 public static void distinct(List list) {
72 distinct(list, null);
73 }
74
75 public static void distinct(List list, Comparator comparator) {
76 if ((list == null) || (list.size() == 0)) {
77 return;
78 }
79
80 Set set = null;
81
82 if (comparator == null) {
83 set = new TreeSet();
84 }
85 else {
86 set = new TreeSet(comparator);
87 }
88
89 Iterator itr = list.iterator();
90
91 while (itr.hasNext()) {
92 Object obj = itr.next();
93
94 if (set.contains(obj)) {
95 itr.remove();
96 }
97 else {
98 set.add(obj);
99 }
100 }
101 }
102
103 public static List fromArray(Object[] array) {
104 if ((array == null) || (array.length == 0)) {
105 return new ArrayList();
106 }
107
108 List list = new ArrayList(array.length);
109
110 for (int i = 0; i < array.length; i++) {
111 list.add(array[i]);
112 }
113
114 return list;
115 }
116
117 public static List fromCollection(Collection c) {
118 if ((c != null) && (c instanceof List)) {
119 return (List)c;
120 }
121
122 if ((c == null) || (c.size() == 0)) {
123 return new ArrayList();
124 }
125
126 List list = new ArrayList(c.size());
127
128 Iterator itr = c.iterator();
129
130 while (itr.hasNext()) {
131 list.add(itr.next());
132 }
133
134 return list;
135 }
136
137 public static List fromEnumeration(Enumeration enu) {
138 List list = new ArrayList();
139
140 while (enu.hasMoreElements()) {
141 Object obj = enu.nextElement();
142
143 list.add(obj);
144 }
145
146 return list;
147 }
148
149 public static List fromFile(String fileName) throws IOException {
150 return fromFile(new File(fileName));
151 }
152
153 public static List fromFile(File file) throws IOException {
154 List list = new ArrayList();
155
156 BufferedReader br = new BufferedReader(new FileReader(file));
157
158 String s = StringPool.BLANK;
159
160 while ((s = br.readLine()) != null) {
161 list.add(s);
162 }
163
164 br.close();
165
166 return list;
167 }
168
169 public static List fromString(String s) {
170 return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
171 }
172
173 public static List sort(List list) {
174 return sort(list, null);
175 }
176
177 public static List sort(List list, Comparator comparator) {
178 if (list instanceof UnmodifiableList) {
179 list = copy(list);
180 }
181
182 Collections.sort(list, comparator);
183
184 return list;
185 }
186
187 public static List subList(List list, int start, int end) {
188 List newList = new ArrayList();
189
190 int normalizedSize = list.size() - 1;
191
192 if ((start < 0) || (start > normalizedSize) || (end < 0) ||
193 (start > end)) {
194
195 return newList;
196 }
197
198 for (int i = start; i < end && i <= normalizedSize; i++) {
199 newList.add(list.get(i));
200 }
201
202 return newList;
203 }
204
205 public static List<Boolean> toList(boolean[] array) {
206 if ((array == null) || (array.length == 0)) {
207 return Collections.EMPTY_LIST;
208 }
209
210 List<Boolean> list = new ArrayList<Boolean>(array.length);
211
212 for (boolean value : array) {
213 list.add(value);
214 }
215
216 return list;
217 }
218
219 public static List<Double> toList(double[] array) {
220 if ((array == null) || (array.length == 0)) {
221 return Collections.EMPTY_LIST;
222 }
223
224 List<Double> list = new ArrayList<Double>(array.length);
225
226 for (double value : array) {
227 list.add(value);
228 }
229
230 return list;
231 }
232
233 public static List<Float> toList(float[] array) {
234 if ((array == null) || (array.length == 0)) {
235 return Collections.EMPTY_LIST;
236 }
237
238 List<Float> list = new ArrayList<Float>(array.length);
239
240 for (float value : array) {
241 list.add(value);
242 }
243
244 return list;
245 }
246
247 public static List<Integer> toList(int[] array) {
248 if ((array == null) || (array.length == 0)) {
249 return Collections.EMPTY_LIST;
250 }
251
252 List<Integer> list = new ArrayList<Integer>(array.length);
253
254 for (int value : array) {
255 list.add(value);
256 }
257
258 return list;
259 }
260
261 public static List<Long> toList(long[] array) {
262 if ((array == null) || (array.length == 0)) {
263 return Collections.EMPTY_LIST;
264 }
265
266 List<Long> list = new ArrayList<Long>(array.length);
267
268 for (long value : array) {
269 list.add(value);
270 }
271
272 return list;
273 }
274
275 public static List<Short> toList(short[] array) {
276 if ((array == null) || (array.length == 0)) {
277 return Collections.EMPTY_LIST;
278 }
279
280 List<Short> list = new ArrayList<Short>(array.length);
281
282 for (short value : array) {
283 list.add(value);
284 }
285
286 return list;
287 }
288
289 public static List<Boolean> toList(Boolean[] array) {
290 if ((array == null) || (array.length == 0)) {
291 return Collections.EMPTY_LIST;
292 }
293
294 List<Boolean> list = new ArrayList<Boolean>(array.length);
295
296 for (Boolean value : array) {
297 list.add(value);
298 }
299
300 return list;
301 }
302
303 public static List<Double> toList(Double[] array) {
304 if ((array == null) || (array.length == 0)) {
305 return Collections.EMPTY_LIST;
306 }
307
308 List<Double> list = new ArrayList<Double>(array.length);
309
310 for (Double value : array) {
311 list.add(value);
312 }
313
314 return list;
315 }
316
317 public static List<Float> toList(Float[] array) {
318 if ((array == null) || (array.length == 0)) {
319 return Collections.EMPTY_LIST;
320 }
321
322 List<Float> list = new ArrayList<Float>(array.length);
323
324 for (Float value : array) {
325 list.add(value);
326 }
327
328 return list;
329 }
330
331 public static List<Integer> toList(Integer[] array) {
332 if ((array == null) || (array.length == 0)) {
333 return Collections.EMPTY_LIST;
334 }
335
336 List<Integer> list = new ArrayList<Integer>(array.length);
337
338 for (Integer value : array) {
339 list.add(value);
340 }
341
342 return list;
343 }
344
345 public static List<Long> toList(Long[] array) {
346 if ((array == null) || (array.length == 0)) {
347 return Collections.EMPTY_LIST;
348 }
349
350 List<Long> list = new ArrayList<Long>(array.length);
351
352 for (Long value : array) {
353 list.add(value);
354 }
355
356 return list;
357 }
358
359 public static List<Short> toList(Short[] array) {
360 if ((array == null) || (array.length == 0)) {
361 return Collections.EMPTY_LIST;
362 }
363
364 List<Short> list = new ArrayList<Short>(array.length);
365
366 for (Short value : array) {
367 list.add(value);
368 }
369
370 return list;
371 }
372
373 public static List<String> toList(String[] array) {
374 if ((array == null) || (array.length == 0)) {
375 return Collections.EMPTY_LIST;
376 }
377
378 List<String> list = new ArrayList<String>(array.length);
379
380 for (String value : array) {
381 list.add(value);
382 }
383
384 return list;
385 }
386
387 public static String toString(List list, String param) {
388 return toString(list, param, StringPool.COMMA);
389 }
390
391 public static String toString(List list, String param, String delimiter) {
392 StringBuilder sb = new StringBuilder();
393
394 for (int i = 0; i < list.size(); i++) {
395 Object bean = list.get(i);
396
397 Object value = BeanPropertiesUtil.getObject(bean, param);
398
399 if (value == null) {
400 value = StringPool.BLANK;
401 }
402
403 sb.append(value.toString());
404
405 if ((i + 1) != list.size()) {
406 sb.append(delimiter);
407 }
408 }
409
410 return sb.toString();
411 }
412
413 }