001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class SetUtil {
035    
036            public static Set<Boolean> fromArray(boolean[] array) {
037                    if (ArrayUtil.isEmpty(array)) {
038                            return new HashSet<>();
039                    }
040    
041                    Set<Boolean> set = new HashSet<>(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<>();
053                    }
054    
055                    Set<Byte> set = new HashSet<>(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<>();
067                    }
068    
069                    Set<Character> set = new HashSet<>(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<>();
081                    }
082    
083                    Set<Double> set = new HashSet<>(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<>();
095                    }
096    
097                    Set<E> set = new HashSet<>(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<>();
109                    }
110    
111                    Set<Float> set = new HashSet<>(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<>();
123                    }
124    
125                    Set<Integer> set = new HashSet<>(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<>();
137                    }
138    
139                    Set<Long> set = new HashSet<>(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<>();
151                    }
152    
153                    Set<Short> set = new HashSet<>(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.isEmpty()) {
168                            return new HashSet<>();
169                    }
170    
171                    return new HashSet<>(c);
172            }
173    
174            public static <E> Set<E> fromEnumeration(Enumeration<? extends E> enu) {
175                    Set<E> set = new HashSet<>();
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<>();
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<>();
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<>();
217                    }
218    
219                    return new HashSet<>(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 = _toSet(collection1);
234                    Set<T> set2 = _toSet(collection2);
235    
236                    if (set1.size() > set2.size()) {
237                            set2.retainAll(set1);
238    
239                            return set2;
240                    }
241    
242                    set1.retainAll(set2);
243    
244                    return set1;
245            }
246    
247            public static Set<Long> intersect(long[] array1, long[] array2) {
248                    return intersect(fromArray(array1), fromArray(array2));
249            }
250    
251            public static boolean isEmpty(Set<?> set) {
252                    if ((set == null) || set.isEmpty()) {
253                            return true;
254                    }
255    
256                    return false;
257            }
258    
259            public static boolean isNotEmpty(Set<?> set) {
260                    return !isEmpty(set);
261            }
262    
263            public static <T> Set<T> symmetricDifference(
264                    Collection<T> collection1, Collection<T> collection2) {
265    
266                    if (collection1.isEmpty()) {
267                            return _toSet(collection2);
268                    }
269    
270                    if (collection2.isEmpty()) {
271                            return _toSet(collection1);
272                    }
273    
274                    Set<T> set1 = _toSet(collection1);
275                    Set<T> set2 = _toSet(collection2);
276    
277                    Set<T> intersection = intersect(set1, set2);
278    
279                    if (set1.size() > set2.size()) {
280                            set1.addAll(set2);
281                    }
282                    else {
283                            set2.addAll(set1);
284    
285                            set1 = set2;
286                    }
287    
288                    set1.removeAll(intersection);
289    
290                    return set1;
291            }
292    
293            public static Set<Long> symmetricDifference(long[] array1, long[] array2) {
294                    return symmetricDifference(fromArray(array1), fromArray(array2));
295            }
296    
297            private static <T> Set<T> _toSet(Collection<T> collection) {
298                    if (collection instanceof Set) {
299                            return (Set<T>)collection;
300                    }
301    
302                    return new HashSet<>(collection);
303            }
304    
305    }