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  
27  import java.io.BufferedReader;
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         BufferedReader br = new BufferedReader(new FileReader(file));
141 
142         String s = StringPool.BLANK;
143 
144         while ((s = br.readLine()) != null) {
145             list.add(s);
146         }
147 
148         br.close();
149 
150         return list;
151     }
152 
153     public static List<String> fromFile(String fileName) throws IOException {
154         return fromFile(new File(fileName));
155     }
156 
157     public static List<String> fromString(String s) {
158         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
159     }
160 
161     public static <E> List<E> sort(List<E> list) {
162         return sort(list, null);
163     }
164 
165     public static <E> List<E> sort(
166         List<E> list, Comparator<? super E> comparator) {
167 
168         if (UnmodifiableList.class.isAssignableFrom(list.getClass())) {
169             list = copy(list);
170         }
171 
172         Collections.sort(list, comparator);
173 
174         return list;
175     }
176 
177     public static <E> List<E> subList(List<E> list, int start, int end) {
178         List<E> newList = new ArrayList<E>();
179 
180         int normalizedSize = list.size() - 1;
181 
182         if ((start < 0) || (start > normalizedSize) || (end < 0) ||
183             (start > end)) {
184 
185             return newList;
186         }
187 
188         for (int i = start; i < end && i <= normalizedSize; i++) {
189             newList.add(list.get(i));
190         }
191 
192         return newList;
193     }
194 
195     public static List<Boolean> toList(boolean[] array) {
196         if ((array == null) || (array.length == 0)) {
197             return Collections.emptyList();
198         }
199 
200         List<Boolean> list = new ArrayList<Boolean>(array.length);
201 
202         for (boolean value : array) {
203             list.add(value);
204         }
205 
206         return list;
207     }
208 
209     public static List<Double> toList(double[] array) {
210         if ((array == null) || (array.length == 0)) {
211             return Collections.emptyList();
212         }
213 
214         List<Double> list = new ArrayList<Double>(array.length);
215 
216         for (double value : array) {
217             list.add(value);
218         }
219 
220         return list;
221     }
222 
223     public static <E> List<E> toList(E[] array) {
224         if ((array == null) || (array.length == 0)) {
225             return Collections.emptyList();
226         }
227 
228         return new ArrayList<E>(Arrays.asList(array));
229     }
230 
231     public static List<Float> toList(float[] array) {
232         if ((array == null) || (array.length == 0)) {
233             return Collections.emptyList();
234         }
235 
236         List<Float> list = new ArrayList<Float>(array.length);
237 
238         for (float value : array) {
239             list.add(value);
240         }
241 
242         return list;
243     }
244 
245     public static List<Integer> toList(int[] array) {
246         if ((array == null) || (array.length == 0)) {
247             return Collections.emptyList();
248         }
249 
250         List<Integer> list = new ArrayList<Integer>(array.length);
251 
252         for (int value : array) {
253             list.add(value);
254         }
255 
256         return list;
257     }
258 
259     public static List<Long> toList(long[] array) {
260         if ((array == null) || (array.length == 0)) {
261             return Collections.emptyList();
262         }
263 
264         List<Long> list = new ArrayList<Long>(array.length);
265 
266         for (long value : array) {
267             list.add(value);
268         }
269 
270         return list;
271     }
272 
273     public static List<Short> toList(short[] array) {
274         if ((array == null) || (array.length == 0)) {
275             return Collections.emptyList();
276         }
277 
278         List<Short> list = new ArrayList<Short>(array.length);
279 
280         for (short value : array) {
281             list.add(value);
282         }
283 
284         return list;
285     }
286 
287     public static String toString(List<?> list, String param) {
288         return toString(list, param, StringPool.COMMA);
289     }
290 
291     public static String toString(
292         List<?> list, String param, String delimiter) {
293 
294         StringBuilder sb = new StringBuilder();
295 
296         for (int i = 0; i < list.size(); i++) {
297             Object bean = list.get(i);
298 
299             Object value = BeanPropertiesUtil.getObject(bean, param);
300 
301             if (value == null) {
302                 value = StringPool.BLANK;
303             }
304 
305             sb.append(value.toString());
306 
307             if ((i + 1) != list.size()) {
308                 sb.append(delimiter);
309             }
310         }
311 
312         return sb.toString();
313     }
314 
315 }