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            /**
184             * @deprecated
185             */
186            public static <E> boolean remove(List<E> list, E element) {
187                    Iterator<E> itr = list.iterator();
188    
189                    while (itr.hasNext()) {
190                            E curElement = itr.next();
191    
192                            if ((curElement == element) || curElement.equals(element)) {
193                                    itr.remove();
194    
195                                    return true;
196                            }
197                    }
198    
199                    return false;
200            }
201    
202            public static <E> List<E> sort(List<E> list) {
203                    return sort(list, null);
204            }
205    
206            public static <E> List<E> sort(
207                    List<E> list, Comparator<? super E> comparator) {
208    
209                    if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
210                            list = copy(list);
211                    }
212    
213                    Collections.sort(list, comparator);
214    
215                    return list;
216            }
217    
218            public static <E> List<E> subList(List<E> list, int start, int end) {
219                    List<E> newList = new ArrayList<E>();
220    
221                    int normalizedSize = list.size() - 1;
222    
223                    if ((start < 0) || (start > normalizedSize) || (end < 0) ||
224                            (start > end)) {
225    
226                            return newList;
227                    }
228    
229                    for (int i = start; (i < end) && (i <= normalizedSize); i++) {
230                            newList.add(list.get(i));
231                    }
232    
233                    return newList;
234            }
235    
236            public static List<Boolean> toList(boolean[] array) {
237                    if ((array == null) || (array.length == 0)) {
238                            return new ArrayList<Boolean>();
239                    }
240    
241                    List<Boolean> list = new ArrayList<Boolean>(array.length);
242    
243                    for (boolean value : array) {
244                            list.add(value);
245                    }
246    
247                    return list;
248            }
249    
250            public static List<Character> toList(char[] array) {
251                    if ((array == null) || (array.length == 0)) {
252                            return new ArrayList<Character>();
253                    }
254    
255                    List<Character> list = new ArrayList<Character>(array.length);
256    
257                    for (char value : array) {
258                            list.add(value);
259                    }
260    
261                    return list;
262            }
263    
264            public static List<Double> toList(double[] array) {
265                    if ((array == null) || (array.length == 0)) {
266                            return new ArrayList<Double>();
267                    }
268    
269                    List<Double> list = new ArrayList<Double>(array.length);
270    
271                    for (double value : array) {
272                            list.add(value);
273                    }
274    
275                    return list;
276            }
277    
278            public static <E> List<E> toList(E[] array) {
279                    if ((array == null) || (array.length == 0)) {
280                            return new ArrayList<E>();
281                    }
282    
283                    return new ArrayList<E>(Arrays.asList(array));
284            }
285    
286            public static List<Float> toList(float[] array) {
287                    if ((array == null) || (array.length == 0)) {
288                            return new ArrayList<Float>();
289                    }
290    
291                    List<Float> list = new ArrayList<Float>(array.length);
292    
293                    for (float value : array) {
294                            list.add(value);
295                    }
296    
297                    return list;
298            }
299    
300            public static List<Integer> toList(int[] array) {
301                    if ((array == null) || (array.length == 0)) {
302                            return new ArrayList<Integer>();
303                    }
304    
305                    List<Integer> list = new ArrayList<Integer>(array.length);
306    
307                    for (int value : array) {
308                            list.add(value);
309                    }
310    
311                    return list;
312            }
313    
314            public static List<Long> toList(long[] array) {
315                    if ((array == null) || (array.length == 0)) {
316                            return new ArrayList<Long>();
317                    }
318    
319                    List<Long> list = new ArrayList<Long>(array.length);
320    
321                    for (long value : array) {
322                            list.add(value);
323                    }
324    
325                    return list;
326            }
327    
328            public static List<Short> toList(short[] array) {
329                    if ((array == null) || (array.length == 0)) {
330                            return new ArrayList<Short>();
331                    }
332    
333                    List<Short> list = new ArrayList<Short>(array.length);
334    
335                    for (short value : array) {
336                            list.add(value);
337                    }
338    
339                    return list;
340            }
341    
342            /**
343             * @see {@link ArrayUtil#toString(Object[], String)}
344             */
345            public static String toString(List<?> list, String param) {
346                    return toString(list, param, StringPool.COMMA);
347            }
348    
349            /**
350             * @see {@link ArrayUtil#toString(Object[], String, String)}
351             */
352            public static String toString(
353                    List<?> list, String param, String delimiter) {
354    
355                    if ((list == null) || list.isEmpty()) {
356                            return StringPool.BLANK;
357                    }
358    
359                    StringBundler sb = new StringBundler(2 * list.size() - 1);
360    
361                    for (int i = 0; i < list.size(); i++) {
362                            Object bean = list.get(i);
363    
364                            Object value = BeanPropertiesUtil.getObject(bean, param);
365    
366                            if (value != null) {
367                                    sb.append(value);
368                            }
369    
370                            if ((i + 1) != list.size()) {
371                                    sb.append(delimiter);
372                            }
373                    }
374    
375                    return sb.toString();
376            }
377    
378            /**
379             * @see {@link ArrayUtil#toString(Object[], Accessor)}
380             */
381            public static <T, V> String toString(
382                    List<T> list, Accessor<T, V> accessor) {
383    
384                    return toString(list, accessor, StringPool.COMMA);
385            }
386    
387            /**
388             * @see {@link ArrayUtil#toString(Object[], Accessor, String)}
389             */
390            public static <T, V> String toString(
391                    List<T> list, Accessor<T, V> accessor, String delimiter) {
392    
393                    if ((list == null) || list.isEmpty()) {
394                            return StringPool.BLANK;
395                    }
396    
397                    StringBundler sb = new StringBundler(2 * list.size() - 1);
398    
399                    for (int i = 0; i < list.size(); i++) {
400                            T bean = list.get(i);
401    
402                            V value = accessor.get(bean);
403    
404                            if (value != null) {
405                                    sb.append(value);
406                            }
407    
408                            if ((i + 1) != list.size()) {
409                                    sb.append(delimiter);
410                            }
411                    }
412    
413                    return sb.toString();
414            }
415    
416    }