1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
26  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
27  
28  import java.io.File;
29  import java.io.FileReader;
30  import java.io.IOException;
31  
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.Comparator;
37  import java.util.Enumeration;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.Set;
41  import java.util.TreeSet;
42  
43  /**
44   * <a href="ListUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class ListUtil {
49  
50      public static <E> List<E> copy(List<E> master) {
51          if (master == null) {
52              return null;
53          }
54  
55          return new ArrayList<E>(master);
56      }
57  
58      public static <E> void copy(List<E> master, List<? super E> copy) {
59          if ((master == null) || (copy == null)) {
60              return;
61          }
62  
63          copy.clear();
64  
65          copy.addAll(master);
66      }
67  
68      public static void distinct(List<?> list) {
69          distinct(list, null);
70      }
71  
72      public static <E> void distinct(List<E> list, Comparator<E> comparator) {
73          if ((list == null) || (list.size() == 0)) {
74              return;
75          }
76  
77          Set<E> set = null;
78  
79          if (comparator == null) {
80              set = new TreeSet<E>();
81          }
82          else {
83              set = new TreeSet<E>(comparator);
84          }
85  
86          Iterator<E> itr = list.iterator();
87  
88          while (itr.hasNext()) {
89              E obj = itr.next();
90  
91              if (set.contains(obj)) {
92                  itr.remove();
93              }
94              else {
95                  set.add(obj);
96              }
97          }
98      }
99  
100     public static <E> List<E> fromArray(E[] array) {
101         if ((array == null) || (array.length == 0)) {
102             return new ArrayList<E>();
103         }
104 
105         return new ArrayList<E>(Arrays.asList(array));
106     }
107 
108     @SuppressWarnings("unchecked")
109     public static <E> List<E> fromCollection(Collection<E> c) {
110         if ((c != null) && (List.class.isAssignableFrom(c.getClass()))) {
111             return (List)c;
112         }
113 
114         if ((c == null) || (c.size() == 0)) {
115             return new ArrayList<E>();
116         }
117 
118         List<E> list = new ArrayList<E>(c.size());
119 
120         list.addAll(c);
121 
122         return list;
123     }
124 
125     public static <E> List<E> fromEnumeration(Enumeration<E> enu) {
126         List<E> list = new ArrayList<E>();
127 
128         while (enu.hasMoreElements()) {
129             E obj = enu.nextElement();
130 
131             list.add(obj);
132         }
133 
134         return list;
135     }
136 
137     public static List<String> fromFile(File file) throws IOException {
138         List<String> list = new ArrayList<String>();
139 
140         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
141             new FileReader(file));
142 
143         String s = StringPool.BLANK;
144 
145         while ((s = unsyncBufferedReader.readLine()) != null) {
146             list.add(s);
147         }
148 
149         unsyncBufferedReader.close();
150 
151         return list;
152     }
153 
154     public static List<String> fromFile(String fileName) throws IOException {
155         return fromFile(new File(fileName));
156     }
157 
158     public static List<String> fromString(String s) {
159         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
160     }
161 
162     public static <E> List<E> sort(List<E> list) {
163         return sort(list, null);
164     }
165 
166     public static <E> List<E> sort(
167         List<E> list, Comparator<? super E> comparator) {
168 
169         if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
170             list = copy(list);
171         }
172 
173         Collections.sort(list, comparator);
174 
175         return list;
176     }
177 
178     public static <E> List<E> subList(List<E> list, int start, int end) {
179         List<E> newList = new ArrayList<E>();
180 
181         int normalizedSize = list.size() - 1;
182 
183         if ((start < 0) || (start > normalizedSize) || (end < 0) ||
184             (start > end)) {
185 
186             return newList;
187         }
188 
189         for (int i = start; i < end && i <= normalizedSize; i++) {
190             newList.add(list.get(i));
191         }
192 
193         return newList;
194     }
195 
196     public static List<Boolean> toList(boolean[] array) {
197         if ((array == null) || (array.length == 0)) {
198             return Collections.emptyList();
199         }
200 
201         List<Boolean> list = new ArrayList<Boolean>(array.length);
202 
203         for (boolean value : array) {
204             list.add(value);
205         }
206 
207         return list;
208     }
209 
210     public static List<Double> toList(double[] array) {
211         if ((array == null) || (array.length == 0)) {
212             return Collections.emptyList();
213         }
214 
215         List<Double> list = new ArrayList<Double>(array.length);
216 
217         for (double value : array) {
218             list.add(value);
219         }
220 
221         return list;
222     }
223 
224     public static <E> List<E> toList(E[] array) {
225         if ((array == null) || (array.length == 0)) {
226             return Collections.emptyList();
227         }
228 
229         return new ArrayList<E>(Arrays.asList(array));
230     }
231 
232     public static List<Float> toList(float[] array) {
233         if ((array == null) || (array.length == 0)) {
234             return Collections.emptyList();
235         }
236 
237         List<Float> list = new ArrayList<Float>(array.length);
238 
239         for (float value : array) {
240             list.add(value);
241         }
242 
243         return list;
244     }
245 
246     public static List<Integer> toList(int[] array) {
247         if ((array == null) || (array.length == 0)) {
248             return Collections.emptyList();
249         }
250 
251         List<Integer> list = new ArrayList<Integer>(array.length);
252 
253         for (int value : array) {
254             list.add(value);
255         }
256 
257         return list;
258     }
259 
260     public static List<Long> toList(long[] array) {
261         if ((array == null) || (array.length == 0)) {
262             return Collections.emptyList();
263         }
264 
265         List<Long> list = new ArrayList<Long>(array.length);
266 
267         for (long value : array) {
268             list.add(value);
269         }
270 
271         return list;
272     }
273 
274     public static List<Short> toList(short[] array) {
275         if ((array == null) || (array.length == 0)) {
276             return Collections.emptyList();
277         }
278 
279         List<Short> list = new ArrayList<Short>(array.length);
280 
281         for (short value : array) {
282             list.add(value);
283         }
284 
285         return list;
286     }
287 
288     public static String toString(List<?> list, String param) {
289         return toString(list, param, StringPool.COMMA);
290     }
291 
292     public static String toString(
293         List<?> list, String param, String delimiter) {
294 
295         StringBuilder sb = new StringBuilder();
296 
297         for (int i = 0; i < list.size(); i++) {
298             Object bean = list.get(i);
299 
300             Object value = BeanPropertiesUtil.getObject(bean, param);
301 
302             if (value == null) {
303                 value = StringPool.BLANK;
304             }
305 
306             sb.append(value.toString());
307 
308             if ((i + 1) != list.size()) {
309                 sb.append(delimiter);
310             }
311         }
312 
313         return sb.toString();
314     }
315 
316 }