1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileReader;
25  import java.io.IOException;
26  
27  import java.util.Collection;
28  import java.util.Enumeration;
29  import java.util.HashSet;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Set;
33  
34  /**
35   * <a href="SetUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class SetUtil {
41  
42      public static Set<Long> fromArray(long[] array) {
43          if ((array == null) || (array.length == 0)) {
44              return new HashSet();
45          }
46  
47          Set<Long> set = new HashSet<Long>(array.length);
48  
49          for (int i = 0; i < array.length; i++) {
50              set.add(array[i]);
51          }
52  
53          return set;
54      }
55  
56      public static Set fromArray(Object[] array) {
57          if ((array == null) || (array.length == 0)) {
58              return new HashSet();
59          }
60  
61          Set set = new HashSet(array.length);
62  
63          for (int i = 0; i < array.length; i++) {
64              set.add(array[i]);
65          }
66  
67          return set;
68      }
69  
70      public static Set fromCollection(Collection c) {
71          if ((c != null) && (c instanceof Set)) {
72              return (Set)c;
73          }
74  
75          if ((c == null) || (c.size() == 0)) {
76              return new HashSet();
77          }
78  
79          Set set = new HashSet(c.size());
80  
81          Iterator itr = c.iterator();
82  
83          while (itr.hasNext()) {
84              set.add(itr.next());
85          }
86  
87          return set;
88      }
89  
90      public static Set fromEnumeration(Enumeration enu) {
91          Set set = new HashSet();
92  
93          while (enu.hasMoreElements()) {
94              set.add(enu.nextElement());
95          }
96  
97          return set;
98      }
99  
100     public static Set fromIterator(Iterator itr) {
101         Set set = new HashSet();
102 
103         while (itr.hasNext()) {
104             set.add(itr.next());
105         }
106 
107         return set;
108     }
109 
110     public static Set fromFile(String fileName) throws IOException {
111         return fromFile(new File(fileName));
112     }
113 
114     public static Set fromFile(File file) throws IOException {
115         Set set = new HashSet();
116 
117         BufferedReader br = new BufferedReader(new FileReader(file));
118 
119         String s = StringPool.BLANK;
120 
121         while ((s = br.readLine()) != null) {
122             set.add(s);
123         }
124 
125         br.close();
126 
127         return set;
128     }
129 
130     public static Set fromList(List array) {
131         if ((array == null) || (array.size() == 0)) {
132             return new HashSet();
133         }
134 
135         Set set = new HashSet(array.size());
136 
137         for (int i = 0; i < array.size(); i++) {
138             set.add(array.get(i));
139         }
140 
141         return set;
142     }
143 
144     public static Set fromString(String s) {
145         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
146     }
147 
148 }