1
14
15 package com.liferay.portal.kernel.util;
16
17 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
18 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
19
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.IOException;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.Enumeration;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Set;
34
35
40 public class ListUtil {
41
42 public static <E> List<E> copy(List<E> master) {
43 if (master == null) {
44 return null;
45 }
46
47 return new ArrayList<E>(master);
48 }
49
50 public static <E> void copy(List<E> master, List<? super E> copy) {
51 if ((master == null) || (copy == null)) {
52 return;
53 }
54
55 copy.clear();
56
57 copy.addAll(master);
58 }
59
60 public static void distinct(List<?> list) {
61 distinct(list, null);
62 }
63
64 public static <E> void distinct(List<E> list, Comparator<E> comparator) {
65 if ((list == null) || list.isEmpty()) {
66 return;
67 }
68
69 Set<E> set = new HashSet<E>();
70
71 Iterator<E> itr = list.iterator();
72
73 while (itr.hasNext()) {
74 E obj = itr.next();
75
76 if (set.contains(obj)) {
77 itr.remove();
78 }
79 else {
80 set.add(obj);
81 }
82 }
83
84 if (comparator != null) {
85 Collections.sort(list, comparator);
86 }
87 }
88
89 public static <E> List<E> fromArray(E[] array) {
90 if ((array == null) || (array.length == 0)) {
91 return new ArrayList<E>();
92 }
93
94 return new ArrayList<E>(Arrays.asList(array));
95 }
96
97 @SuppressWarnings("unchecked")
98 public static <E> List<E> fromCollection(Collection<E> c) {
99 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 List<String> list = new ArrayList<String>();
128
129 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
130 new FileReader(file));
131
132 String s = StringPool.BLANK;
133
134 while ((s = unsyncBufferedReader.readLine()) != null) {
135 list.add(s);
136 }
137
138 unsyncBufferedReader.close();
139
140 return list;
141 }
142
143 public static List<String> fromFile(String fileName) throws IOException {
144 return fromFile(new File(fileName));
145 }
146
147 public static List<String> fromString(String s) {
148 return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
149 }
150
151 public static <E> List<E> sort(List<E> list) {
152 return sort(list, null);
153 }
154
155 public static <E> List<E> sort(
156 List<E> list, Comparator<? super E> comparator) {
157
158 if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
159 list = copy(list);
160 }
161
162 Collections.sort(list, comparator);
163
164 return list;
165 }
166
167 public static <E> List<E> subList(List<E> list, int start, int end) {
168 List<E> newList = new ArrayList<E>();
169
170 int normalizedSize = list.size() - 1;
171
172 if ((start < 0) || (start > normalizedSize) || (end < 0) ||
173 (start > end)) {
174
175 return newList;
176 }
177
178 for (int i = start; (i < end) && (i <= normalizedSize); i++) {
179 newList.add(list.get(i));
180 }
181
182 return newList;
183 }
184
185 public static List<Boolean> toList(boolean[] array) {
186 if ((array == null) || (array.length == 0)) {
187 return new ArrayList<Boolean>();
188 }
189
190 List<Boolean> list = new ArrayList<Boolean>(array.length);
191
192 for (boolean value : array) {
193 list.add(value);
194 }
195
196 return list;
197 }
198
199 public static List<Double> toList(double[] array) {
200 if ((array == null) || (array.length == 0)) {
201 return new ArrayList<Double>();
202 }
203
204 List<Double> list = new ArrayList<Double>(array.length);
205
206 for (double value : array) {
207 list.add(value);
208 }
209
210 return list;
211 }
212
213 public static <E> List<E> toList(E[] array) {
214 if ((array == null) || (array.length == 0)) {
215 return new ArrayList<E>();
216 }
217
218 return new ArrayList<E>(Arrays.asList(array));
219 }
220
221 public static List<Float> toList(float[] array) {
222 if ((array == null) || (array.length == 0)) {
223 return new ArrayList<Float>();
224 }
225
226 List<Float> list = new ArrayList<Float>(array.length);
227
228 for (float value : array) {
229 list.add(value);
230 }
231
232 return list;
233 }
234
235 public static List<Integer> toList(int[] array) {
236 if ((array == null) || (array.length == 0)) {
237 return new ArrayList<Integer>();
238 }
239
240 List<Integer> list = new ArrayList<Integer>(array.length);
241
242 for (int value : array) {
243 list.add(value);
244 }
245
246 return list;
247 }
248
249 public static List<Long> toList(long[] array) {
250 if ((array == null) || (array.length == 0)) {
251 return new ArrayList<Long>();
252 }
253
254 List<Long> list = new ArrayList<Long>(array.length);
255
256 for (long value : array) {
257 list.add(value);
258 }
259
260 return list;
261 }
262
263 public static List<Short> toList(short[] array) {
264 if ((array == null) || (array.length == 0)) {
265 return new ArrayList<Short>();
266 }
267
268 List<Short> list = new ArrayList<Short>(array.length);
269
270 for (short value : array) {
271 list.add(value);
272 }
273
274 return list;
275 }
276
277 public static String toString(List<?> list, String param) {
278 return toString(list, param, StringPool.COMMA);
279 }
280
281 public static String toString(
282 List<?> list, String param, String delimiter) {
283
284 StringBundler sb = null;
285
286 if (list.isEmpty()) {
287 sb = new StringBundler();
288 }
289 else {
290 sb = new StringBundler(2 * list.size() - 1);
291 }
292
293 for (int i = 0; i < list.size(); i++) {
294 Object bean = list.get(i);
295
296 Object value = BeanPropertiesUtil.getObject(bean, param);
297
298 if (value == null) {
299 value = StringPool.BLANK;
300 }
301
302 sb.append(value.toString());
303
304 if ((i + 1) != list.size()) {
305 sb.append(delimiter);
306 }
307 }
308
309 return sb.toString();
310 }
311
312 }