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.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
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 (ArrayUtil.isEmpty(array)) {
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
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 if (start < 0) {
240 start = 0;
241 }
242
243 if ((end < 0) || (end > list.size())) {
244 end = list.size();
245 }
246
247 if (start < end) {
248 return list.subList(start, end);
249 }
250
251 return Collections.emptyList();
252 }
253
254 public static List<Boolean> toList(boolean[] array) {
255 if (ArrayUtil.isEmpty(array)) {
256 return new ArrayList<Boolean>();
257 }
258
259 List<Boolean> list = new ArrayList<Boolean>(array.length);
260
261 for (boolean value : array) {
262 list.add(value);
263 }
264
265 return list;
266 }
267
268 public static List<Character> toList(char[] array) {
269 if (ArrayUtil.isEmpty(array)) {
270 return new ArrayList<Character>();
271 }
272
273 List<Character> list = new ArrayList<Character>(array.length);
274
275 for (char value : array) {
276 list.add(value);
277 }
278
279 return list;
280 }
281
282 public static List<Double> toList(double[] array) {
283 if (ArrayUtil.isEmpty(array)) {
284 return new ArrayList<Double>();
285 }
286
287 List<Double> list = new ArrayList<Double>(array.length);
288
289 for (double value : array) {
290 list.add(value);
291 }
292
293 return list;
294 }
295
296 public static <E> List<E> toList(E[] array) {
297 if (ArrayUtil.isEmpty(array)) {
298 return new ArrayList<E>();
299 }
300
301 return new ArrayList<E>(Arrays.asList(array));
302 }
303
304 public static List<Float> toList(float[] array) {
305 if (ArrayUtil.isEmpty(array)) {
306 return new ArrayList<Float>();
307 }
308
309 List<Float> list = new ArrayList<Float>(array.length);
310
311 for (float value : array) {
312 list.add(value);
313 }
314
315 return list;
316 }
317
318 public static List<Integer> toList(int[] array) {
319 if (ArrayUtil.isEmpty(array)) {
320 return new ArrayList<Integer>();
321 }
322
323 List<Integer> list = new ArrayList<Integer>(array.length);
324
325 for (int value : array) {
326 list.add(value);
327 }
328
329 return list;
330 }
331
332 public static List<Long> toList(long[] array) {
333 if (ArrayUtil.isEmpty(array)) {
334 return new ArrayList<Long>();
335 }
336
337 List<Long> list = new ArrayList<Long>(array.length);
338
339 for (long value : array) {
340 list.add(value);
341 }
342
343 return list;
344 }
345
346 public static List<Short> toList(short[] array) {
347 if (ArrayUtil.isEmpty(array)) {
348 return new ArrayList<Short>();
349 }
350
351 List<Short> list = new ArrayList<Short>(array.length);
352
353 for (short value : array) {
354 list.add(value);
355 }
356
357 return list;
358 }
359
360
363 public static String toString(List<?> list, String param) {
364 return toString(list, param, StringPool.COMMA);
365 }
366
367
370 public static String toString(
371 List<?> list, String param, String delimiter) {
372
373 if ((list == null) || list.isEmpty()) {
374 return StringPool.BLANK;
375 }
376
377 StringBundler sb = new StringBundler(2 * list.size() - 1);
378
379 for (int i = 0; i < list.size(); i++) {
380 Object bean = list.get(i);
381
382 Object value = null;
383
384 if (Validator.isNull(param)) {
385 value = String.valueOf(bean);
386 }
387 else {
388 value = BeanPropertiesUtil.getObject(bean, param);
389 }
390
391 if (value != null) {
392 sb.append(value);
393 }
394
395 if ((i + 1) != list.size()) {
396 sb.append(delimiter);
397 }
398 }
399
400 return sb.toString();
401 }
402
403
406 public static <T, V> String toString(
407 List<T> list, Accessor<T, V> accessor) {
408
409 return toString(list, accessor, StringPool.COMMA);
410 }
411
412
415 public static <T, V> String toString(
416 List<T> list, Accessor<T, V> accessor, String delimiter) {
417
418 if ((list == null) || list.isEmpty()) {
419 return StringPool.BLANK;
420 }
421
422 StringBundler sb = new StringBundler(2 * list.size() - 1);
423
424 for (int i = 0; i < list.size(); i++) {
425 T bean = list.get(i);
426
427 V value = accessor.get(bean);
428
429 if (value != null) {
430 sb.append(value);
431 }
432
433 if ((i + 1) != list.size()) {
434 sb.append(delimiter);
435 }
436 }
437
438 return sb.toString();
439 }
440
441 }