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.Set;
034
035
038 public class ListUtil {
039
040 public static <E> List<E> copy(List<E> master) {
041 if (master == null) {
042 return null;
043 }
044
045 return new ArrayList<E>(master);
046 }
047
048 public static <E> void copy(List<E> master, List<? super E> copy) {
049 if ((master == null) || (copy == null)) {
050 return;
051 }
052
053 copy.clear();
054
055 copy.addAll(master);
056 }
057
058 public static void distinct(List<?> list) {
059 distinct(list, null);
060 }
061
062 public static <E> void distinct(List<E> list, Comparator<E> comparator) {
063 if ((list == null) || list.isEmpty()) {
064 return;
065 }
066
067 Set<E> set = new HashSet<E>();
068
069 Iterator<E> itr = list.iterator();
070
071 while (itr.hasNext()) {
072 E obj = itr.next();
073
074 if (set.contains(obj)) {
075 itr.remove();
076 }
077 else {
078 set.add(obj);
079 }
080 }
081
082 if (comparator != null) {
083 Collections.sort(list, comparator);
084 }
085 }
086
087 public static <E> List<E> fromArray(E[] array) {
088 if ((array == null) || (array.length == 0)) {
089 return new ArrayList<E>();
090 }
091
092 return new ArrayList<E>(Arrays.asList(array));
093 }
094
095 @SuppressWarnings("rawtypes")
096 public static <E> List<E> fromCollection(Collection<E> c) {
097 if ((c != null) && (List.class.isAssignableFrom(c.getClass()))) {
098 return (List)c;
099 }
100
101 if ((c == null) || c.isEmpty()) {
102 return new ArrayList<E>();
103 }
104
105 List<E> list = new ArrayList<E>(c.size());
106
107 list.addAll(c);
108
109 return list;
110 }
111
112 public static <E> List<E> fromEnumeration(Enumeration<E> enu) {
113 List<E> list = new ArrayList<E>();
114
115 while (enu.hasMoreElements()) {
116 E obj = enu.nextElement();
117
118 list.add(obj);
119 }
120
121 return list;
122 }
123
124 public static List<String> fromFile(File file) throws IOException {
125 List<String> list = new ArrayList<String>();
126
127 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
128 new FileReader(file));
129
130 String s = StringPool.BLANK;
131
132 while ((s = unsyncBufferedReader.readLine()) != null) {
133 list.add(s);
134 }
135
136 unsyncBufferedReader.close();
137
138 return list;
139 }
140
141 public static List<String> fromFile(String fileName) throws IOException {
142 return fromFile(new File(fileName));
143 }
144
145 public static List<String> fromString(String s) {
146 return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
147 }
148
149 public static <E> boolean remove(List<E> list, E element) {
150 Iterator<E> itr = list.iterator();
151
152 while (itr.hasNext()) {
153 E curElement = itr.next();
154
155 if ((curElement == element) || curElement.equals(element)) {
156 itr.remove();
157
158 return true;
159 }
160 }
161
162 return false;
163 }
164
165 public static <E> List<E> sort(List<E> list) {
166 return sort(list, null);
167 }
168
169 public static <E> List<E> sort(
170 List<E> list, Comparator<? super E> comparator) {
171
172 if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
173 list = copy(list);
174 }
175
176 Collections.sort(list, comparator);
177
178 return list;
179 }
180
181 public static <E> List<E> subList(List<E> list, int start, int end) {
182 List<E> newList = new ArrayList<E>();
183
184 int normalizedSize = list.size() - 1;
185
186 if ((start < 0) || (start > normalizedSize) || (end < 0) ||
187 (start > end)) {
188
189 return newList;
190 }
191
192 for (int i = start; (i < end) && (i <= normalizedSize); i++) {
193 newList.add(list.get(i));
194 }
195
196 return newList;
197 }
198
199 public static List<Boolean> toList(boolean[] array) {
200 if ((array == null) || (array.length == 0)) {
201 return new ArrayList<Boolean>();
202 }
203
204 List<Boolean> list = new ArrayList<Boolean>(array.length);
205
206 for (boolean value : array) {
207 list.add(value);
208 }
209
210 return list;
211 }
212
213 public static List<Double> toList(double[] array) {
214 if ((array == null) || (array.length == 0)) {
215 return new ArrayList<Double>();
216 }
217
218 List<Double> list = new ArrayList<Double>(array.length);
219
220 for (double value : array) {
221 list.add(value);
222 }
223
224 return list;
225 }
226
227 public static <E> List<E> toList(E[] array) {
228 if ((array == null) || (array.length == 0)) {
229 return new ArrayList<E>();
230 }
231
232 return new ArrayList<E>(Arrays.asList(array));
233 }
234
235 public static List<Float> toList(float[] array) {
236 if ((array == null) || (array.length == 0)) {
237 return new ArrayList<Float>();
238 }
239
240 List<Float> list = new ArrayList<Float>(array.length);
241
242 for (float value : array) {
243 list.add(value);
244 }
245
246 return list;
247 }
248
249 public static List<Integer> toList(int[] array) {
250 if ((array == null) || (array.length == 0)) {
251 return new ArrayList<Integer>();
252 }
253
254 List<Integer> list = new ArrayList<Integer>(array.length);
255
256 for (int value : array) {
257 list.add(value);
258 }
259
260 return list;
261 }
262
263 public static List<Long> toList(long[] array) {
264 if ((array == null) || (array.length == 0)) {
265 return new ArrayList<Long>();
266 }
267
268 List<Long> list = new ArrayList<Long>(array.length);
269
270 for (long value : array) {
271 list.add(value);
272 }
273
274 return list;
275 }
276
277 public static List<Short> toList(short[] array) {
278 if ((array == null) || (array.length == 0)) {
279 return new ArrayList<Short>();
280 }
281
282 List<Short> list = new ArrayList<Short>(array.length);
283
284 for (short value : array) {
285 list.add(value);
286 }
287
288 return list;
289 }
290
291 public static String toString(List<?> list, String param) {
292 return toString(list, param, StringPool.COMMA);
293 }
294
295 public static String toString(
296 List<?> list, String param, String delimiter) {
297
298 StringBundler sb = null;
299
300 if (list.isEmpty()) {
301 sb = new StringBundler();
302 }
303 else {
304 sb = new StringBundler(2 * list.size() - 1);
305 }
306
307 for (int i = 0; i < list.size(); i++) {
308 Object bean = list.get(i);
309
310 Object value = BeanPropertiesUtil.getObject(bean, param);
311
312 if (value == null) {
313 value = StringPool.BLANK;
314 }
315
316 sb.append(value.toString());
317
318 if ((i + 1) != list.size()) {
319 sb.append(delimiter);
320 }
321 }
322
323 return sb.toString();
324 }
325
326 }