001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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> List<E> fromArray(E[] array) {
090                    if ((array == null) || (array.length == 0)) {
091                            return new ArrayList<E>();
092                    }
093    
094                    return new ArrayList<E>(Arrays.asList(array));
095            }
096    
097            @SuppressWarnings("rawtypes")
098            public static <E> List<E> fromCollection(Collection<E> c) {
099                    if ((c != null) && List.class.isAssignableFrom(c.getClass())) {
100                            return (List)c;
101                    }
102    
103                    if ((c == null) || c.isEmpty()) {
104                            return new ArrayList<E>();
105                    }
106    
107                    List<E> list = new ArrayList<E>(c.size());
108    
109                    list.addAll(c);
110    
111                    return list;
112            }
113    
114            public static <E> List<E> fromEnumeration(Enumeration<E> enu) {
115                    List<E> list = new ArrayList<E>();
116    
117                    while (enu.hasMoreElements()) {
118                            E obj = enu.nextElement();
119    
120                            list.add(obj);
121                    }
122    
123                    return list;
124            }
125    
126            public static List<String> fromFile(File file) throws IOException {
127                    if (!file.exists()) {
128                            return new ArrayList<String>();
129                    }
130    
131                    List<String> list = new ArrayList<String>();
132    
133                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
134                            new FileReader(file));
135    
136                    String s = StringPool.BLANK;
137    
138                    while ((s = unsyncBufferedReader.readLine()) != null) {
139                            list.add(s);
140                    }
141    
142                    unsyncBufferedReader.close();
143    
144                    return list;
145            }
146    
147            public static List<String> fromFile(String fileName) throws IOException {
148                    return fromFile(new File(fileName));
149            }
150    
151            public static <E> List<E> fromMapKeys(Map<E, ?> map) {
152                    if ((map == null) || map.isEmpty()) {
153                            return new ArrayList<E>();
154                    }
155    
156                    List<E> list = new ArrayList<E>(map.size());
157    
158                    for (Map.Entry<E, ?> entry : map.entrySet()) {
159                            list.add(entry.getKey());
160                    }
161    
162                    return list;
163            }
164    
165            public static <E> List<E> fromMapValues(Map<?, E> map) {
166                    if ((map == null) || map.isEmpty()) {
167                            return new ArrayList<E>();
168                    }
169    
170                    List<E> list = new ArrayList<E>(map.size());
171    
172                    for (Map.Entry<?, E> entry : map.entrySet()) {
173                            list.add(entry.getValue());
174                    }
175    
176                    return list;
177            }
178    
179            public static List<String> fromString(String s) {
180                    return fromArray(StringUtil.splitLines(s));
181            }
182    
183            public static List<String> fromString(String s, String delimiter) {
184                    return fromArray(StringUtil.split(s, delimiter));
185            }
186    
187            /**
188             * @deprecated As of 6.2.0
189             */
190            public static <E> boolean remove(List<E> list, E element) {
191                    Iterator<E> itr = list.iterator();
192    
193                    while (itr.hasNext()) {
194                            E curElement = itr.next();
195    
196                            if ((curElement == element) || curElement.equals(element)) {
197                                    itr.remove();
198    
199                                    return true;
200                            }
201                    }
202    
203                    return false;
204            }
205    
206            public static <E> List<E> remove(List<E> list, List<E> remove) {
207                    if ((list == null) || list.isEmpty() ||
208                            (remove == null)|| remove.isEmpty()) {
209    
210                            return list;
211                    }
212    
213                    list = copy(list);
214    
215                    for (E element : remove) {
216                            list.remove(element);
217                    }
218    
219                    return list;
220            }
221    
222            public static <E> List<E> sort(List<E> list) {
223                    return sort(list, null);
224            }
225    
226            public static <E> List<E> sort(
227                    List<E> list, Comparator<? super E> comparator) {
228    
229                    if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
230                            list = copy(list);
231                    }
232    
233                    Collections.sort(list, comparator);
234    
235                    return list;
236            }
237    
238            public static <E> List<E> subList(List<E> list, int start, int end) {
239                    List<E> newList = new ArrayList<E>();
240    
241                    int normalizedSize = list.size() - 1;
242    
243                    if ((start < 0) || (start > normalizedSize) || (end < 0) ||
244                            (start > end)) {
245    
246                            return newList;
247                    }
248    
249                    for (int i = start; (i < end) && (i <= normalizedSize); i++) {
250                            newList.add(list.get(i));
251                    }
252    
253                    return newList;
254            }
255    
256            public static List<Boolean> toList(boolean[] array) {
257                    if ((array == null) || (array.length == 0)) {
258                            return new ArrayList<Boolean>();
259                    }
260    
261                    List<Boolean> list = new ArrayList<Boolean>(array.length);
262    
263                    for (boolean value : array) {
264                            list.add(value);
265                    }
266    
267                    return list;
268            }
269    
270            public static List<Double> toList(double[] array) {
271                    if ((array == null) || (array.length == 0)) {
272                            return new ArrayList<Double>();
273                    }
274    
275                    List<Double> list = new ArrayList<Double>(array.length);
276    
277                    for (double value : array) {
278                            list.add(value);
279                    }
280    
281                    return list;
282            }
283    
284            public static <E> List<E> toList(E[] array) {
285                    if ((array == null) || (array.length == 0)) {
286                            return new ArrayList<E>();
287                    }
288    
289                    return new ArrayList<E>(Arrays.asList(array));
290            }
291    
292            public static List<Float> toList(float[] array) {
293                    if ((array == null) || (array.length == 0)) {
294                            return new ArrayList<Float>();
295                    }
296    
297                    List<Float> list = new ArrayList<Float>(array.length);
298    
299                    for (float value : array) {
300                            list.add(value);
301                    }
302    
303                    return list;
304            }
305    
306            public static List<Integer> toList(int[] array) {
307                    if ((array == null) || (array.length == 0)) {
308                            return new ArrayList<Integer>();
309                    }
310    
311                    List<Integer> list = new ArrayList<Integer>(array.length);
312    
313                    for (int value : array) {
314                            list.add(value);
315                    }
316    
317                    return list;
318            }
319    
320            public static List<Long> toList(long[] array) {
321                    if ((array == null) || (array.length == 0)) {
322                            return new ArrayList<Long>();
323                    }
324    
325                    List<Long> list = new ArrayList<Long>(array.length);
326    
327                    for (long value : array) {
328                            list.add(value);
329                    }
330    
331                    return list;
332            }
333    
334            public static List<Short> toList(short[] array) {
335                    if ((array == null) || (array.length == 0)) {
336                            return new ArrayList<Short>();
337                    }
338    
339                    List<Short> list = new ArrayList<Short>(array.length);
340    
341                    for (short value : array) {
342                            list.add(value);
343                    }
344    
345                    return list;
346            }
347    
348            /**
349             * @see ArrayUtil#toString(Object[], String)
350             */
351            public static String toString(List<?> list, String param) {
352                    return toString(list, param, StringPool.COMMA);
353            }
354    
355            /**
356             * @see ArrayUtil#toString(Object[], String, String)
357             */
358            public static String toString(
359                    List<?> list, String param, String delimiter) {
360    
361                    if ((list == null) || list.isEmpty()) {
362                            return StringPool.BLANK;
363                    }
364    
365                    StringBundler sb = new StringBundler(2 * list.size() - 1);
366    
367                    for (int i = 0; i < list.size(); i++) {
368                            Object bean = list.get(i);
369    
370                            Object value = BeanPropertiesUtil.getObject(bean, param);
371    
372                            if (value != null) {
373                                    sb.append(value);
374                            }
375    
376                            if ((i + 1) != list.size()) {
377                                    sb.append(delimiter);
378                            }
379                    }
380    
381                    return sb.toString();
382            }
383    
384            /**
385             * @see ArrayUtil#toString(Object[], Accessor)
386             */
387            public static <T, V> String toString(
388                    List<T> list, Accessor<T, V> accessor) {
389    
390                    return toString(list, accessor, StringPool.COMMA);
391            }
392    
393            /**
394             * @see ArrayUtil#toString(Object[], Accessor, String)
395             */
396            public static <T, V> String toString(
397                    List<T> list, Accessor<T, V> accessor, String delimiter) {
398    
399                    if ((list == null) || list.isEmpty()) {
400                            return StringPool.BLANK;
401                    }
402    
403                    StringBundler sb = new StringBundler(2 * list.size() - 1);
404    
405                    for (int i = 0; i < list.size(); i++) {
406                            T bean = list.get(i);
407    
408                            V value = accessor.get(bean);
409    
410                            if (value != null) {
411                                    sb.append(value);
412                            }
413    
414                            if ((i + 1) != list.size()) {
415                                    sb.append(delimiter);
416                            }
417                    }
418    
419                    return sb.toString();
420            }
421    
422    }