001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
019
020 import java.io.File;
021 import java.io.FileReader;
022 import java.io.IOException;
023
024 import java.lang.reflect.Array;
025
026 import java.util.ArrayList;
027 import java.util.Arrays;
028 import java.util.Collection;
029 import java.util.Collections;
030 import java.util.Comparator;
031 import java.util.Enumeration;
032 import java.util.HashSet;
033 import java.util.Iterator;
034 import java.util.LinkedHashSet;
035 import java.util.LinkedList;
036 import java.util.List;
037 import java.util.ListIterator;
038 import java.util.Map;
039 import java.util.Set;
040
041
045 public class ListUtil {
046
047 public static <E> List<E> copy(List<? extends E> master) {
048 if (master == null) {
049 return null;
050 }
051
052 return new ArrayList<>(master);
053 }
054
055 public static <E> void copy(
056 List<? extends E> master, List<? super E> copy) {
057
058 if ((master == null) || (copy == null)) {
059 return;
060 }
061
062 copy.clear();
063
064 copy.addAll(master);
065 }
066
067 public static <E> int count(
068 List<? extends E> list, PredicateFilter<E> predicateFilter) {
069
070 if (isEmpty(list)) {
071 return 0;
072 }
073
074 int count = 0;
075
076 for (E element : list) {
077 if (predicateFilter.filter(element)) {
078 count++;
079 }
080 }
081
082 return count;
083 }
084
085 public static <E> void distinct(
086 List<? extends E> list, Comparator<E> comparator) {
087
088 if (isEmpty(list)) {
089 return;
090 }
091
092 Set<E> set = new HashSet<>();
093
094 Iterator<? extends E> itr = list.iterator();
095
096 while (itr.hasNext()) {
097 E obj = itr.next();
098
099 if (!set.add(obj)) {
100 itr.remove();
101 }
102 }
103
104 if (comparator != null) {
105 Collections.sort(list, comparator);
106 }
107 }
108
109 public static void distinct(List<?> list) {
110 distinct(list, null);
111 }
112
113 public static <E> boolean exists(
114 List<? extends E> list, PredicateFilter<E> predicateFilter) {
115
116 if (isEmpty(list)) {
117 return false;
118 }
119
120 for (E element : list) {
121 if (predicateFilter.filter(element)) {
122 return true;
123 }
124 }
125
126 return false;
127 }
128
129 public static <T> List<T> filter(
130 List<? extends T> inputList, List<T> outputList,
131 PredicateFilter<T> predicateFilter) {
132
133 for (T item : inputList) {
134 if (predicateFilter.filter(item)) {
135 outputList.add(item);
136 }
137 }
138
139 return outputList;
140 }
141
142 public static <T> List<T> filter(
143 List<? extends T> inputList, PredicateFilter<T> predicateFilter) {
144
145 return filter(
146 inputList, new ArrayList<T>(inputList.size()), predicateFilter);
147 }
148
149 public static <E> List<E> fromArray(E[] array) {
150 if (ArrayUtil.isEmpty(array)) {
151 return new ArrayList<>();
152 }
153
154 return new ArrayList<>(Arrays.asList(array));
155 }
156
157 @SuppressWarnings("rawtypes")
158 public static <E> List<E> fromCollection(Collection<? extends E> c) {
159 if ((c != null) && (c instanceof List)) {
160 return (List)c;
161 }
162
163 if ((c == null) || c.isEmpty()) {
164 return new ArrayList<>();
165 }
166
167 List<E> list = new ArrayList<>(c.size());
168
169 list.addAll(c);
170
171 return list;
172 }
173
174 public static <E> List<E> fromEnumeration(Enumeration<? extends E> enu) {
175 List<E> list = new ArrayList<>();
176
177 while (enu.hasMoreElements()) {
178 E obj = enu.nextElement();
179
180 list.add(obj);
181 }
182
183 return list;
184 }
185
186 public static List<String> fromFile(File file) throws IOException {
187 if (!file.exists()) {
188 return new ArrayList<>();
189 }
190
191 List<String> list = new ArrayList<>();
192
193 try (UnsyncBufferedReader unsyncBufferedReader =
194 new UnsyncBufferedReader(new FileReader(file))) {
195
196 String s = StringPool.BLANK;
197
198 while ((s = unsyncBufferedReader.readLine()) != null) {
199 list.add(s);
200 }
201 }
202
203 return list;
204 }
205
206 public static List<String> fromFile(String fileName) throws IOException {
207 return fromFile(new File(fileName));
208 }
209
210 public static <E> List<E> fromMapKeys(Map<? extends E, ?> map) {
211 if (MapUtil.isEmpty(map)) {
212 return new ArrayList<>();
213 }
214
215 List<E> list = new ArrayList<>(map.size());
216
217 for (Map.Entry<? extends E, ?> entry : map.entrySet()) {
218 list.add(entry.getKey());
219 }
220
221 return list;
222 }
223
224 public static <E> List<E> fromMapValues(Map<?, ? extends E> map) {
225 if (MapUtil.isEmpty(map)) {
226 return new ArrayList<>();
227 }
228
229 List<E> list = new ArrayList<>(map.size());
230
231 for (Map.Entry<?, ? extends E> entry : map.entrySet()) {
232 list.add(entry.getValue());
233 }
234
235 return list;
236 }
237
238 public static List<String> fromString(String s) {
239 return fromArray(StringUtil.splitLines(s));
240 }
241
242 public static List<String> fromString(String s, String delimiter) {
243 return fromArray(StringUtil.split(s, delimiter));
244 }
245
246 public static boolean isEmpty(List<?> list) {
247 if ((list == null) || list.isEmpty()) {
248 return true;
249 }
250
251 return false;
252 }
253
254 public static boolean isNotEmpty(List<?> list) {
255 return !isEmpty(list);
256 }
257
258 public static boolean isNotNull(List<?> list) {
259 return !isNull(list);
260 }
261
262 public static boolean isNull(List<?> list) {
263 if ((list == null) || list.isEmpty()) {
264 return true;
265 }
266
267 for (int i = 0; i < list.size(); i++) {
268 Object bean = list.get(i);
269
270 if (Validator.isNotNull(bean)) {
271 return false;
272 }
273 }
274
275 return true;
276 }
277
278 public static boolean isUnmodifiableList(List<?> list) {
279 return _unmodifiableListClass.isInstance(list);
280 }
281
282 public static <E> List<E> remove(List<E> list, List<? extends E> remove) {
283 if (isEmpty(list) || isEmpty(remove)) {
284 return list;
285 }
286
287 list = copy(list);
288
289 for (E element : remove) {
290 list.remove(element);
291 }
292
293 return list;
294 }
295
296 public static <E> Iterator<E> reverseIterator(List<E> list) {
297 final ListIterator<E> listIterator = list.listIterator(list.size());
298
299 return new Iterator<E>() {
300
301 @Override
302 public boolean hasNext() {
303 return listIterator.hasPrevious();
304 }
305
306 @Override
307 public E next() {
308 return listIterator.previous();
309 }
310
311 @Override
312 public void remove() {
313 listIterator.remove();
314 }
315
316 };
317 }
318
319 public static <E> List<E> sort(List<E> list) {
320 return sort(list, null);
321 }
322
323 public static <E> List<E> sort(
324 List<E> list, Comparator<? super E> comparator) {
325
326 if (isUnmodifiableList(list)) {
327 list = copy(list);
328 }
329
330 Collections.sort(list, comparator);
331
332 return list;
333 }
334
335 public static <E> List<E> subList(List<E> list, int start, int end) {
336 if (start < 0) {
337 start = 0;
338 }
339
340 if ((end < 0) || (end > list.size())) {
341 end = list.size();
342 }
343
344 if (start < end) {
345 return list.subList(start, end);
346 }
347
348 return Collections.emptyList();
349 }
350
351 public static <T, A> A[] toArray(
352 List<? extends T> list, Accessor<T, A> accessor) {
353
354 if (isEmpty(list)) {
355 return (A[])Array.newInstance(accessor.getAttributeClass(), 0);
356 }
357
358 A[] array = (A[])Array.newInstance(
359 accessor.getAttributeClass(), list.size());
360
361 for (int i = 0; i < list.size(); i++) {
362 T bean = list.get(i);
363
364 A attribute = accessor.get(bean);
365
366 array[i] = attribute;
367 }
368
369 return array;
370 }
371
372 public static List<Boolean> toList(boolean[] array) {
373 if (ArrayUtil.isEmpty(array)) {
374 return new ArrayList<>();
375 }
376
377 List<Boolean> list = new ArrayList<>(array.length);
378
379 for (boolean value : array) {
380 list.add(value);
381 }
382
383 return list;
384 }
385
386 public static List<Character> toList(char[] array) {
387 if (ArrayUtil.isEmpty(array)) {
388 return new ArrayList<>();
389 }
390
391 List<Character> list = new ArrayList<>(array.length);
392
393 for (char value : array) {
394 list.add(value);
395 }
396
397 return list;
398 }
399
400 public static List<Double> toList(double[] array) {
401 if (ArrayUtil.isEmpty(array)) {
402 return new ArrayList<>();
403 }
404
405 List<Double> list = new ArrayList<>(array.length);
406
407 for (double value : array) {
408 list.add(value);
409 }
410
411 return list;
412 }
413
414 public static <E> List<E> toList(E[] array) {
415 if (ArrayUtil.isEmpty(array)) {
416 return new ArrayList<>();
417 }
418
419 return new ArrayList<>(Arrays.asList(array));
420 }
421
422 public static List<Float> toList(float[] array) {
423 if (ArrayUtil.isEmpty(array)) {
424 return new ArrayList<>();
425 }
426
427 List<Float> list = new ArrayList<>(array.length);
428
429 for (float value : array) {
430 list.add(value);
431 }
432
433 return list;
434 }
435
436 public static List<Integer> toList(int[] array) {
437 if (ArrayUtil.isEmpty(array)) {
438 return new ArrayList<>();
439 }
440
441 List<Integer> list = new ArrayList<>(array.length);
442
443 for (int value : array) {
444 list.add(value);
445 }
446
447 return list;
448 }
449
450 public static <T, A> List<A> toList(List<T> list, Accessor<T, A> accessor) {
451 List<A> aList = new ArrayList<>(list.size());
452
453 for (T t : list) {
454 aList.add(accessor.get(t));
455 }
456
457 return aList;
458 }
459
460 public static <T, R> List<R> toList(List<T> list, Function<T, R> function) {
461 final List<R> result = new ArrayList<>(list.size());
462
463 for (T t : list) {
464 result.add(function.apply(t));
465 }
466
467 return result;
468 }
469
470 public static <T, V extends T> List<T> toList(List<V> vlist) {
471 return new ArrayList<T>(vlist);
472 }
473
474 public static List<Long> toList(long[] array) {
475 if (ArrayUtil.isEmpty(array)) {
476 return new ArrayList<>();
477 }
478
479 List<Long> list = new ArrayList<>(array.length);
480
481 for (long value : array) {
482 list.add(value);
483 }
484
485 return list;
486 }
487
488 public static List<Short> toList(short[] array) {
489 if (ArrayUtil.isEmpty(array)) {
490 return new ArrayList<>();
491 }
492
493 List<Short> list = new ArrayList<>(array.length);
494
495 for (short value : array) {
496 list.add(value);
497 }
498
499 return list;
500 }
501
502 public static <T> long[] toLongArray(
503 List<? extends T> list, Accessor<T, Long> accessor) {
504
505 if (isEmpty(list)) {
506 return (long[])Array.newInstance(long.class, 0);
507 }
508
509 long[] array = (long[])Array.newInstance(long.class, list.size());
510
511 for (int i = 0; i < list.size(); i++) {
512 T bean = list.get(i);
513
514 Long attribute = accessor.get(bean);
515
516 array[i] = attribute;
517 }
518
519 return array;
520 }
521
522
525 public static <T, A> String toString(
526 List<? extends T> list, Accessor<T, A> accessor) {
527
528 return toString(list, accessor, StringPool.COMMA);
529 }
530
531
534 public static <T, A> String toString(
535 List<? extends T> list, Accessor<T, A> accessor, String delimiter) {
536
537 if (isEmpty(list)) {
538 return StringPool.BLANK;
539 }
540
541 StringBundler sb = new StringBundler(2 * list.size() - 1);
542
543 for (int i = 0; i < list.size(); i++) {
544 T bean = list.get(i);
545
546 A attribute = accessor.get(bean);
547
548 if (attribute != null) {
549 sb.append(attribute);
550 }
551
552 if ((i + 1) != list.size()) {
553 sb.append(delimiter);
554 }
555 }
556
557 return sb.toString();
558 }
559
560
563 public static String toString(List<?> list, String param) {
564 return toString(list, param, StringPool.COMMA);
565 }
566
567
570 public static String toString(
571 List<?> list, String param, String delimiter) {
572
573 if (isEmpty(list)) {
574 return StringPool.BLANK;
575 }
576
577 StringBundler sb = new StringBundler(2 * list.size() - 1);
578
579 for (int i = 0; i < list.size(); i++) {
580 Object bean = list.get(i);
581
582 Object value = null;
583
584 if (Validator.isNull(param)) {
585 value = String.valueOf(bean);
586 }
587 else {
588 value = BeanPropertiesUtil.getObject(bean, param);
589 }
590
591 if (value != null) {
592 sb.append(value);
593 }
594
595 if ((i + 1) != list.size()) {
596 sb.append(delimiter);
597 }
598 }
599
600 return sb.toString();
601 }
602
603 public static <T> List<T> unique(List<T> list) {
604 Set<T> set = new LinkedHashSet<>();
605
606 set.addAll(list);
607
608 if (list.size() == set.size()) {
609 return list;
610 }
611
612 return new ArrayList<>(set);
613 }
614
615 private static final Class<? extends List<?>> _unmodifiableListClass;
616
617 static {
618 List<Object> unmodifiableList = Collections.<Object>unmodifiableList(
619 new LinkedList<Object>());
620
621 _unmodifiableListClass =
622 (Class<? extends List<?>>)unmodifiableList.getClass();
623 }
624
625 }