001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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.util.ArrayList;
025    import java.util.Arrays;
026    import java.util.Collection;
027    import java.util.Collections;
028    import java.util.Comparator;
029    import java.util.Enumeration;
030    import java.util.HashSet;
031    import java.util.Iterator;
032    import java.util.List;
033    import java.util.Map;
034    import java.util.Set;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Shuyang Zhou
039     */
040    public class ListUtil {
041    
042            public static <E> List<E> copy(List<E> master) {
043                    if (master == null) {
044                            return null;
045                    }
046    
047                    return new ArrayList<E>(master);
048            }
049    
050            public static <E> void copy(List<E> master, List<? super E> copy) {
051                    if ((master == null) || (copy == null)) {
052                            return;
053                    }
054    
055                    copy.clear();
056    
057                    copy.addAll(master);
058            }
059    
060            public static void distinct(List<?> list) {
061                    distinct(list, null);
062            }
063    
064            public static <E> void distinct(List<E> list, Comparator<E> comparator) {
065                    if ((list == null) || list.isEmpty()) {
066                            return;
067                    }
068    
069                    Set<E> set = new HashSet<E>();
070    
071                    Iterator<E> itr = list.iterator();
072    
073                    while (itr.hasNext()) {
074                            E obj = itr.next();
075    
076                            if (set.contains(obj)) {
077                                    itr.remove();
078                            }
079                            else {
080                                    set.add(obj);
081                            }
082                    }
083    
084                    if (comparator != null) {
085                            Collections.sort(list, comparator);
086                    }
087            }
088    
089            public static <E> boolean exists(
090                    List<? extends E> list, PredicateFilter<E> predicateFilter) {
091    
092                    if (isEmpty(list)) {
093                            return false;
094                    }
095    
096                    for (E element : list) {
097                            if (predicateFilter.filter(element)) {
098                                    return true;
099                            }
100                    }
101    
102                    return false;
103            }
104    
105            public static <E> List<E> fromArray(E[] array) {
106                    if (ArrayUtil.isEmpty(array)) {
107                            return new ArrayList<E>();
108                    }
109    
110                    return new ArrayList<E>(Arrays.asList(array));
111            }
112    
113            @SuppressWarnings("rawtypes")
114            public static <E> List<E> fromCollection(Collection<E> c) {
115                    if ((c != null) && List.class.isAssignableFrom(c.getClass())) {
116                            return (List)c;
117                    }
118    
119                    if ((c == null) || c.isEmpty()) {
120                            return new ArrayList<E>();
121                    }
122    
123                    List<E> list = new ArrayList<E>(c.size());
124    
125                    list.addAll(c);
126    
127                    return list;
128            }
129    
130            public static <E> List<E> fromEnumeration(Enumeration<E> enu) {
131                    List<E> list = new ArrayList<E>();
132    
133                    while (enu.hasMoreElements()) {
134                            E obj = enu.nextElement();
135    
136                            list.add(obj);
137                    }
138    
139                    return list;
140            }
141    
142            public static List<String> fromFile(File file) throws IOException {
143                    if (!file.exists()) {
144                            return new ArrayList<String>();
145                    }
146    
147                    List<String> list = new ArrayList<String>();
148    
149                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
150                            new FileReader(file));
151    
152                    String s = StringPool.BLANK;
153    
154                    while ((s = unsyncBufferedReader.readLine()) != null) {
155                            list.add(s);
156                    }
157    
158                    unsyncBufferedReader.close();
159    
160                    return list;
161            }
162    
163            public static List<String> fromFile(String fileName) throws IOException {
164                    return fromFile(new File(fileName));
165            }
166    
167            public static <E> List<E> fromMapKeys(Map<E, ?> map) {
168                    if ((map == null) || map.isEmpty()) {
169                            return new ArrayList<E>();
170                    }
171    
172                    List<E> list = new ArrayList<E>(map.size());
173    
174                    for (Map.Entry<E, ?> entry : map.entrySet()) {
175                            list.add(entry.getKey());
176                    }
177    
178                    return list;
179            }
180    
181            public static <E> List<E> fromMapValues(Map<?, E> map) {
182                    if ((map == null) || map.isEmpty()) {
183                            return new ArrayList<E>();
184                    }
185    
186                    List<E> list = new ArrayList<E>(map.size());
187    
188                    for (Map.Entry<?, E> entry : map.entrySet()) {
189                            list.add(entry.getValue());
190                    }
191    
192                    return list;
193            }
194    
195            public static List<String> fromString(String s) {
196                    return fromArray(StringUtil.splitLines(s));
197            }
198    
199            public static List<String> fromString(String s, String delimiter) {
200                    return fromArray(StringUtil.split(s, delimiter));
201            }
202    
203            public static boolean isEmpty(List<?> list) {
204                    if ((list == null) || list.isEmpty()) {
205                            return true;
206                    }
207    
208                    return false;
209    }
210    
211            /**
212             * @deprecated As of 6.2.0
213             */
214            public static <E> boolean remove(List<E> list, E element) {
215                    Iterator<E> itr = list.iterator();
216    
217                    while (itr.hasNext()) {
218                            E curElement = itr.next();
219    
220                            if ((curElement == element) || curElement.equals(element)) {
221                                    itr.remove();
222    
223                                    return true;
224                            }
225                    }
226    
227                    return false;
228            }
229    
230            public static <E> List<E> remove(List<E> list, List<E> remove) {
231                    if ((list == null) || list.isEmpty() ||
232                            (remove == null)|| remove.isEmpty()) {
233    
234                            return list;
235                    }
236    
237                    list = copy(list);
238    
239                    for (E element : remove) {
240                            list.remove(element);
241                    }
242    
243                    return list;
244            }
245    
246            public static <E> List<E> sort(List<E> list) {
247                    return sort(list, null);
248            }
249    
250            public static <E> List<E> sort(
251                    List<E> list, Comparator<? super E> comparator) {
252    
253                    if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
254                            list = copy(list);
255                    }
256    
257                    Collections.sort(list, comparator);
258    
259                    return list;
260            }
261    
262            public static <E> List<E> subList(List<E> list, int start, int end) {
263                    if (start < 0) {
264                            start = 0;
265                    }
266    
267                    if ((end < 0) || (end > list.size())) {
268                            end = list.size();
269                    }
270    
271                    if (start < end) {
272                            return list.subList(start, end);
273                    }
274    
275                    return Collections.emptyList();
276            }
277    
278            public static List<Boolean> toList(boolean[] array) {
279                    if (ArrayUtil.isEmpty(array)) {
280                            return new ArrayList<Boolean>();
281                    }
282    
283                    List<Boolean> list = new ArrayList<Boolean>(array.length);
284    
285                    for (boolean value : array) {
286                            list.add(value);
287                    }
288    
289                    return list;
290            }
291    
292            public static List<Character> toList(char[] array) {
293                    if (ArrayUtil.isEmpty(array)) {
294                            return new ArrayList<Character>();
295                    }
296    
297                    List<Character> list = new ArrayList<Character>(array.length);
298    
299                    for (char value : array) {
300                            list.add(value);
301                    }
302    
303                    return list;
304            }
305    
306            public static List<Double> toList(double[] array) {
307                    if (ArrayUtil.isEmpty(array)) {
308                            return new ArrayList<Double>();
309                    }
310    
311                    List<Double> list = new ArrayList<Double>(array.length);
312    
313                    for (double value : array) {
314                            list.add(value);
315                    }
316    
317                    return list;
318            }
319    
320            public static <E> List<E> toList(E[] array) {
321                    if (ArrayUtil.isEmpty(array)) {
322                            return new ArrayList<E>();
323                    }
324    
325                    return new ArrayList<E>(Arrays.asList(array));
326            }
327    
328            public static List<Float> toList(float[] array) {
329                    if (ArrayUtil.isEmpty(array)) {
330                            return new ArrayList<Float>();
331                    }
332    
333                    List<Float> list = new ArrayList<Float>(array.length);
334    
335                    for (float value : array) {
336                            list.add(value);
337                    }
338    
339                    return list;
340            }
341    
342            public static List<Integer> toList(int[] array) {
343                    if (ArrayUtil.isEmpty(array)) {
344                            return new ArrayList<Integer>();
345                    }
346    
347                    List<Integer> list = new ArrayList<Integer>(array.length);
348    
349                    for (int value : array) {
350                            list.add(value);
351                    }
352    
353                    return list;
354            }
355    
356            public static List<Long> toList(long[] array) {
357                    if (ArrayUtil.isEmpty(array)) {
358                            return new ArrayList<Long>();
359                    }
360    
361                    List<Long> list = new ArrayList<Long>(array.length);
362    
363                    for (long value : array) {
364                            list.add(value);
365                    }
366    
367                    return list;
368            }
369    
370            public static List<Short> toList(short[] array) {
371                    if (ArrayUtil.isEmpty(array)) {
372                            return new ArrayList<Short>();
373                    }
374    
375                    List<Short> list = new ArrayList<Short>(array.length);
376    
377                    for (short value : array) {
378                            list.add(value);
379                    }
380    
381                    return list;
382            }
383    
384            /**
385             * @see ArrayUtil#toString(Object[], String)
386             */
387            public static String toString(List<?> list, String param) {
388                    return toString(list, param, StringPool.COMMA);
389            }
390    
391            /**
392             * @see ArrayUtil#toString(Object[], String, String)
393             */
394            public static String toString(
395                    List<?> list, String param, String delimiter) {
396    
397                    if ((list == null) || list.isEmpty()) {
398                            return StringPool.BLANK;
399                    }
400    
401                    StringBundler sb = new StringBundler(2 * list.size() - 1);
402    
403                    for (int i = 0; i < list.size(); i++) {
404                            Object bean = list.get(i);
405    
406                            Object value = null;
407    
408                            if (Validator.isNull(param)) {
409                                    value = String.valueOf(bean);
410                            }
411                            else {
412                                    value = BeanPropertiesUtil.getObject(bean, param);
413                            }
414    
415                            if (value != null) {
416                                    sb.append(value);
417                            }
418    
419                            if ((i + 1) != list.size()) {
420                                    sb.append(delimiter);
421                            }
422                    }
423    
424                    return sb.toString();
425            }
426    
427            /**
428             * @see ArrayUtil#toString(Object[], Accessor)
429             */
430            public static <T, V> String toString(
431                    List<T> list, Accessor<T, V> accessor) {
432    
433                    return toString(list, accessor, StringPool.COMMA);
434            }
435    
436            /**
437             * @see ArrayUtil#toString(Object[], Accessor, String)
438             */
439            public static <T, V> String toString(
440                    List<T> list, Accessor<T, V> accessor, String delimiter) {
441    
442                    if ((list == null) || list.isEmpty()) {
443                            return StringPool.BLANK;
444                    }
445    
446                    StringBundler sb = new StringBundler(2 * list.size() - 1);
447    
448                    for (int i = 0; i < list.size(); i++) {
449                            T bean = list.get(i);
450    
451                            V value = accessor.get(bean);
452    
453                            if (value != null) {
454                                    sb.append(value);
455                            }
456    
457                            if ((i + 1) != list.size()) {
458                                    sb.append(delimiter);
459                            }
460                    }
461    
462                    return sb.toString();
463            }
464    
465    }