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.Collections;
025 import java.util.Enumeration;
026 import java.util.HashSet;
027 import java.util.Iterator;
028 import java.util.List;
029 import java.util.Set;
030
031
034 public class SetUtil {
035
036 public static Set<Boolean> fromArray(boolean[] array) {
037 if (ArrayUtil.isEmpty(array)) {
038 return new HashSet<Boolean>();
039 }
040
041 Set<Boolean> set = new HashSet<Boolean>(array.length);
042
043 for (int i = 0; i < array.length; i++) {
044 set.add(array[i]);
045 }
046
047 return set;
048 }
049
050 public static Set<Byte> fromArray(byte[] array) {
051 if (ArrayUtil.isEmpty(array)) {
052 return new HashSet<Byte>();
053 }
054
055 Set<Byte> set = new HashSet<Byte>(array.length);
056
057 for (int i = 0; i < array.length; i++) {
058 set.add(array[i]);
059 }
060
061 return set;
062 }
063
064 public static Set<Character> fromArray(char[] array) {
065 if (ArrayUtil.isEmpty(array)) {
066 return new HashSet<Character>();
067 }
068
069 Set<Character> set = new HashSet<Character>(array.length);
070
071 for (int i = 0; i < array.length; i++) {
072 set.add(array[i]);
073 }
074
075 return set;
076 }
077
078 public static Set<Double> fromArray(double[] array) {
079 if (ArrayUtil.isEmpty(array)) {
080 return new HashSet<Double>();
081 }
082
083 Set<Double> set = new HashSet<Double>(array.length);
084
085 for (int i = 0; i < array.length; i++) {
086 set.add(array[i]);
087 }
088
089 return set;
090 }
091
092 public static <E> Set<E> fromArray(E[] array) {
093 if (ArrayUtil.isEmpty(array)) {
094 return new HashSet<E>();
095 }
096
097 Set<E> set = new HashSet<E>(array.length);
098
099 for (int i = 0; i < array.length; i++) {
100 set.add(array[i]);
101 }
102
103 return set;
104 }
105
106 public static Set<Float> fromArray(float[] array) {
107 if (ArrayUtil.isEmpty(array)) {
108 return new HashSet<Float>();
109 }
110
111 Set<Float> set = new HashSet<Float>(array.length);
112
113 for (int i = 0; i < array.length; i++) {
114 set.add(array[i]);
115 }
116
117 return set;
118 }
119
120 public static Set<Integer> fromArray(int[] array) {
121 if (ArrayUtil.isEmpty(array)) {
122 return new HashSet<Integer>();
123 }
124
125 Set<Integer> set = new HashSet<Integer>(array.length);
126
127 for (int i = 0; i < array.length; i++) {
128 set.add(array[i]);
129 }
130
131 return set;
132 }
133
134 public static Set<Long> fromArray(long[] array) {
135 if (ArrayUtil.isEmpty(array)) {
136 return new HashSet<Long>();
137 }
138
139 Set<Long> set = new HashSet<Long>(array.length);
140
141 for (int i = 0; i < array.length; i++) {
142 set.add(array[i]);
143 }
144
145 return set;
146 }
147
148 public static Set<Short> fromArray(short[] array) {
149 if (ArrayUtil.isEmpty(array)) {
150 return new HashSet<Short>();
151 }
152
153 Set<Short> set = new HashSet<Short>(array.length);
154
155 for (int i = 0; i < array.length; i++) {
156 set.add(array[i]);
157 }
158
159 return set;
160 }
161
162 public static <E> Set<E> fromCollection(Collection<? extends E> c) {
163 if ((c != null) && (c instanceof Set)) {
164 return (Set<E>)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<? extends 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 try (UnsyncBufferedReader unsyncBufferedReader =
188 new UnsyncBufferedReader(new FileReader(file))) {
189
190 String s = StringPool.BLANK;
191
192 while ((s = unsyncBufferedReader.readLine()) != null) {
193 set.add(s);
194 }
195 }
196
197 return set;
198 }
199
200 public static Set<String> fromFile(String fileName) throws IOException {
201 return fromFile(new File(fileName));
202 }
203
204 public static <E> Set<E> fromIterator(Iterator<E> itr) {
205 Set<E> set = new HashSet<E>();
206
207 while (itr.hasNext()) {
208 set.add(itr.next());
209 }
210
211 return set;
212 }
213
214 public static <E> Set<E> fromList(List<? extends E> array) {
215 if (ListUtil.isEmpty(array)) {
216 return new HashSet<E>();
217 }
218
219 return new HashSet<E>(array);
220 }
221
222 public static Set<String> fromString(String s) {
223 return fromArray(StringUtil.splitLines(s));
224 }
225
226 public static <T> Set<T> intersect(
227 Collection<T> collection1, Collection<T> collection2) {
228
229 if (collection1.isEmpty() || collection2.isEmpty()) {
230 return Collections.emptySet();
231 }
232
233 Set<T> set1 = null;
234
235 if (collection1 instanceof Set) {
236 set1 = (Set<T>)collection1;
237 }
238 else {
239 set1 = new HashSet<T>(collection1);
240 }
241
242 Set<T> set2 = null;
243
244 if (collection2 instanceof Set) {
245 set2 = (Set<T>)collection2;
246 }
247 else {
248 set2 = new HashSet<T>(collection2);
249 }
250
251 if (set1.size() > set2.size()) {
252 set2.retainAll(set1);
253
254 return set2;
255 }
256
257 set1.retainAll(set2);
258
259 return set1;
260 }
261
262 public static Set<Long> intersect(long[] array1, long[] array2) {
263 return intersect(fromArray(array1), fromArray(array2));
264 }
265
266 }