1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.io.BufferedReader;
30  import java.io.File;
31  import java.io.FileReader;
32  import java.io.IOException;
33  
34  import java.util.ArrayList;
35  import java.util.Collection;
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   */
49  public class ListUtil {
50  
51      public static List copy(List master) {
52          if (master == null) {
53              return null;
54          }
55  
56          List copy = new ArrayList(master.size());
57  
58          copy(master, copy);
59  
60          return copy;
61      }
62  
63      public static void copy(List master, List copy) {
64          if ((master == null) || (copy == null)) {
65              return;
66          }
67  
68          copy.clear();
69  
70          Iterator itr = master.iterator();
71  
72          while (itr.hasNext()) {
73              Object obj = itr.next();
74  
75              copy.add(obj);
76          }
77      }
78  
79      public static void distinct(List list) {
80          distinct(list, null);
81      }
82  
83      public static void distinct(List list, Comparator comparator) {
84          if ((list == null) || (list.size() == 0)) {
85              return;
86          }
87  
88          Set set = null;
89  
90          if (comparator == null) {
91              set = new TreeSet();
92          }
93          else {
94              set = new TreeSet(comparator);
95          }
96  
97          Iterator itr = list.iterator();
98  
99          while (itr.hasNext()) {
100             Object obj = itr.next();
101 
102             if (set.contains(obj)) {
103                 itr.remove();
104             }
105             else {
106                 set.add(obj);
107             }
108         }
109     }
110 
111     public static List fromArray(Object[] array) {
112         if ((array == null) || (array.length == 0)) {
113             return new ArrayList();
114         }
115 
116         List list = new ArrayList(array.length);
117 
118         for (int i = 0; i < array.length; i++) {
119             list.add(array[i]);
120         }
121 
122         return list;
123     }
124 
125     public static List fromCollection(Collection c) {
126         if ((c != null) && (c instanceof List)) {
127             return (List)c;
128         }
129 
130         if ((c == null) || (c.size() == 0)) {
131             return new ArrayList();
132         }
133 
134         List list = new ArrayList(c.size());
135 
136         Iterator itr = c.iterator();
137 
138         while (itr.hasNext()) {
139             list.add(itr.next());
140         }
141 
142         return list;
143     }
144 
145     public static List fromEnumeration(Enumeration enu) {
146         List list = new ArrayList();
147 
148         while (enu.hasMoreElements()) {
149             Object obj = enu.nextElement();
150 
151             list.add(obj);
152         }
153 
154         return list;
155     }
156 
157     public static List fromFile(String fileName) throws IOException {
158         return fromFile(new File(fileName));
159     }
160 
161     public static List fromFile(File file) throws IOException {
162         List list = new ArrayList();
163 
164         BufferedReader br = new BufferedReader(new FileReader(file));
165 
166         String s = StringPool.BLANK;
167 
168         while ((s = br.readLine()) != null) {
169             list.add(s);
170         }
171 
172         br.close();
173 
174         return list;
175     }
176 
177     public static List fromString(String s) {
178         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
179     }
180 
181     public static List subList(List list, int begin, int end) {
182         List newList = new ArrayList();
183 
184         int normalizedSize = list.size() - 1;
185 
186         if ((begin < 0) || (begin > normalizedSize) || (end < 0) ||
187             (begin > end)) {
188 
189             return newList;
190         }
191 
192         for (int i = begin; i < end && i <= normalizedSize; i++) {
193             newList.add(list.get(i));
194         }
195 
196         return newList;
197     }
198 
199     public static String toString(List list, String param) {
200         return toString(list, param, StringPool.COMMA);
201     }
202 
203     public static String toString(List list, String param, String delimiter) {
204         StringMaker sm = new StringMaker();
205 
206         for (int i = 0; i < list.size(); i++) {
207             Object bean = list.get(i);
208 
209             Object value = BeanUtil.getObject(bean, param);
210 
211             if (value == null) {
212                 value = StringPool.BLANK;
213             }
214 
215             sm.append(value.toString());
216 
217             if ((i + 1) != list.size()) {
218                 sm.append(delimiter);
219             }
220         }
221 
222         return sm.toString();
223     }
224 
225 }