001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018
019 import java.io.File;
020 import java.io.FileReader;
021 import java.io.IOException;
022
023 import java.util.Collection;
024 import java.util.Enumeration;
025 import java.util.HashSet;
026 import java.util.Iterator;
027 import java.util.List;
028 import java.util.Set;
029
030
033 public class SetUtil {
034
035 public static Set<Boolean> fromArray(boolean[] array) {
036 if ((array == null) || (array.length == 0)) {
037 return new HashSet<Boolean>();
038 }
039
040 Set<Boolean> set = new HashSet<Boolean>(array.length);
041
042 for (int i = 0; i < array.length; i++) {
043 set.add(array[i]);
044 }
045
046 return set;
047 }
048
049 public static Set<Byte> fromArray(byte[] array) {
050 if ((array == null) || (array.length == 0)) {
051 return new HashSet<Byte>();
052 }
053
054 Set<Byte> set = new HashSet<Byte>(array.length);
055
056 for (int i = 0; i < array.length; i++) {
057 set.add(array[i]);
058 }
059
060 return set;
061 }
062
063 public static Set<Character> fromArray(char[] array) {
064 if ((array == null) || (array.length == 0)) {
065 return new HashSet<Character>();
066 }
067
068 Set<Character> set = new HashSet<Character>(array.length);
069
070 for (int i = 0; i < array.length; i++) {
071 set.add(array[i]);
072 }
073
074 return set;
075 }
076
077 public static Set<Double> fromArray(double[] array) {
078 if ((array == null) || (array.length == 0)) {
079 return new HashSet<Double>();
080 }
081
082 Set<Double> set = new HashSet<Double>(array.length);
083
084 for (int i = 0; i < array.length; i++) {
085 set.add(array[i]);
086 }
087
088 return set;
089 }
090
091 public static <E> Set<E> fromArray(E[] array) {
092 if ((array == null) || (array.length == 0)) {
093 return new HashSet<E>();
094 }
095
096 Set<E> set = new HashSet<E>(array.length);
097
098 for (int i = 0; i < array.length; i++) {
099 set.add(array[i]);
100 }
101
102 return set;
103 }
104
105 public static Set<Float> fromArray(float[] array) {
106 if ((array == null) || (array.length == 0)) {
107 return new HashSet<Float>();
108 }
109
110 Set<Float> set = new HashSet<Float>(array.length);
111
112 for (int i = 0; i < array.length; i++) {
113 set.add(array[i]);
114 }
115
116 return set;
117 }
118
119 public static Set<Integer> fromArray(int[] array) {
120 if ((array == null) || (array.length == 0)) {
121 return new HashSet<Integer>();
122 }
123
124 Set<Integer> set = new HashSet<Integer>(array.length);
125
126 for (int i = 0; i < array.length; i++) {
127 set.add(array[i]);
128 }
129
130 return set;
131 }
132
133 public static Set<Long> fromArray(long[] array) {
134 if ((array == null) || (array.length == 0)) {
135 return new HashSet<Long>();
136 }
137
138 Set<Long> set = new HashSet<Long>(array.length);
139
140 for (int i = 0; i < array.length; i++) {
141 set.add(array[i]);
142 }
143
144 return set;
145 }
146
147 public static Set<Short> fromArray(short[] array) {
148 if ((array == null) || (array.length == 0)) {
149 return new HashSet<Short>();
150 }
151
152 Set<Short> set = new HashSet<Short>(array.length);
153
154 for (int i = 0; i < array.length; i++) {
155 set.add(array[i]);
156 }
157
158 return set;
159 }
160
161 @SuppressWarnings("rawtypes")
162 public static <E> Set<E> fromCollection(Collection<E> c) {
163 if ((c != null) && Set.class.isAssignableFrom(c.getClass())) {
164 return (Set)c;
165 }
166
167 if ((c == null) || (c.size() == 0)) {
168 return new HashSet<E>();
169 }
170
171 return new HashSet<E>(c);
172 }
173
174 public static <E> Set<E> fromEnumeration(Enumeration<E> enu) {
175 Set<E> set = new HashSet<E>();
176
177 while (enu.hasMoreElements()) {
178 set.add(enu.nextElement());
179 }
180
181 return set;
182 }
183
184 public static Set<String> fromFile(File file) throws IOException {
185 Set<String> set = new HashSet<String>();
186
187 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
188 new FileReader(file));
189
190 String s = StringPool.BLANK;
191
192 while ((s = unsyncBufferedReader.readLine()) != null) {
193 set.add(s);
194 }
195
196 unsyncBufferedReader.close();
197
198 return set;
199 }
200
201 public static Set<String> fromFile(String fileName) throws IOException {
202 return fromFile(new File(fileName));
203 }
204
205 public static <E> Set<E> fromIterator(Iterator<E> itr) {
206 Set<E> set = new HashSet<E>();
207
208 while (itr.hasNext()) {
209 set.add(itr.next());
210 }
211
212 return set;
213 }
214
215 public static <E> Set<E> fromList(List<E> array) {
216 if ((array == null) || (array.size() == 0)) {
217 return new HashSet<E>();
218 }
219
220 return new HashSet<E>(array);
221 }
222
223 public static Set<String> fromString(String s) {
224 return fromArray(StringUtil.splitLines(s));
225 }
226
227 }