1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18  
19  import java.io.File;
20  import java.io.FileReader;
21  import java.io.IOException;
22  
23  import java.util.Collection;
24  import java.util.Enumeration;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Set;
29  
30  /**
31   * <a href="SetUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class SetUtil {
36  
37      public static <E> Set<E> fromArray(E[] array) {
38          if ((array == null) || (array.length == 0)) {
39              return new HashSet<E>();
40          }
41  
42          Set<E> set = new HashSet<E>(array.length);
43  
44          for (int i = 0; i < array.length; i++) {
45              set.add(array[i]);
46          }
47  
48          return set;
49      }
50  
51      public static Set<Long> fromArray(long[] array) {
52          if ((array == null) || (array.length == 0)) {
53              return new HashSet<Long>();
54          }
55  
56          Set<Long> set = new HashSet<Long>(array.length);
57  
58          for (int i = 0; i < array.length; i++) {
59              set.add(array[i]);
60          }
61  
62          return set;
63      }
64  
65      @SuppressWarnings("unchecked")
66      public static <E> Set<E> fromCollection(Collection<E> c) {
67          if ((c != null) && (Set.class.isAssignableFrom(c.getClass()))) {
68              return (Set)c;
69          }
70  
71          if ((c == null) || (c.size() == 0)) {
72              return new HashSet<E>();
73          }
74  
75          return new HashSet<E>(c);
76      }
77  
78      public static <E> Set<E> fromEnumeration(Enumeration<E> enu) {
79          Set<E> set = new HashSet<E>();
80  
81          while (enu.hasMoreElements()) {
82              set.add(enu.nextElement());
83          }
84  
85          return set;
86      }
87  
88      public static Set<String> fromFile(File file) throws IOException {
89          Set<String> set = new HashSet<String>();
90  
91          UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
92              new FileReader(file));
93  
94          String s = StringPool.BLANK;
95  
96          while ((s = unsyncBufferedReader.readLine()) != null) {
97              set.add(s);
98          }
99  
100         unsyncBufferedReader.close();
101 
102         return set;
103     }
104 
105     public static Set<String> fromFile(String fileName) throws IOException {
106         return fromFile(new File(fileName));
107     }
108 
109     public static <E> Set<E> fromIterator(Iterator<E> itr) {
110         Set<E> set = new HashSet<E>();
111 
112         while (itr.hasNext()) {
113             set.add(itr.next());
114         }
115 
116         return set;
117     }
118 
119     public static <E> Set<E> fromList(List<E> array) {
120         if ((array == null) || (array.size() == 0)) {
121             return new HashSet<E>();
122         }
123 
124         return new HashSet<E>(array);
125     }
126 
127     public static Set<String> fromString(String s) {
128         return fromArray(StringUtil.split(s, StringPool.NEW_LINE));
129     }
130 
131 }