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