001    /**
002     * Copyright (c) 2000-2013 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<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            @SuppressWarnings("rawtypes")
163            public static <E> Set<E> fromCollection(Collection<E> c) {
164                    if ((c != null) && Set.class.isAssignableFrom(c.getClass())) {
165                            return (Set)c;
166                    }
167    
168                    if ((c == null) || c.isEmpty()) {
169                            return new HashSet<E>();
170                    }
171    
172                    return new HashSet<E>(c);
173            }
174    
175            public static <E> Set<E> fromEnumeration(Enumeration<E> enu) {
176                    Set<E> set = new HashSet<E>();
177    
178                    while (enu.hasMoreElements()) {
179                            set.add(enu.nextElement());
180                    }
181    
182                    return set;
183            }
184    
185            public static Set<String> fromFile(File file) throws IOException {
186                    Set<String> set = new HashSet<String>();
187    
188                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
189                            new FileReader(file));
190    
191                    String s = StringPool.BLANK;
192    
193                    while ((s = unsyncBufferedReader.readLine()) != null) {
194                            set.add(s);
195                    }
196    
197                    unsyncBufferedReader.close();
198    
199                    return set;
200            }
201    
202            public static Set<String> fromFile(String fileName) throws IOException {
203                    return fromFile(new File(fileName));
204            }
205    
206            public static <E> Set<E> fromIterator(Iterator<E> itr) {
207                    Set<E> set = new HashSet<E>();
208    
209                    while (itr.hasNext()) {
210                            set.add(itr.next());
211                    }
212    
213                    return set;
214            }
215    
216            public static <E> Set<E> fromList(List<E> array) {
217                    if ((array == null) || array.isEmpty()) {
218                            return new HashSet<E>();
219                    }
220    
221                    return new HashSet<E>(array);
222            }
223    
224            public static Set<String> fromString(String s) {
225                    return fromArray(StringUtil.splitLines(s));
226            }
227    
228            public static <T> Set<T> intersect(
229                    Collection<T> collection1, Collection<T> collection2) {
230    
231                    if (collection1.isEmpty() || collection2.isEmpty()) {
232                            return Collections.emptySet();
233                    }
234    
235                    Set<T> set1 = _toSet(collection1);
236                    Set<T> set2 = _toSet(collection2);
237    
238                    if (set1.size() > set2.size()) {
239                            set2.retainAll(set1);
240    
241                            return set2;
242                    }
243    
244                    set1.retainAll(set2);
245    
246                    return set1;
247            }
248    
249            public static Set<Long> intersect(long[] array1, long[] array2) {
250                    return intersect(fromArray(array1), fromArray(array2));
251            }
252    
253            public static <T> Set<T> symmetricDifference(
254                    Collection<T> collection1, Collection<T> collection2) {
255    
256                    if (collection1.isEmpty()) {
257                            return _toSet(collection2);
258                    }
259    
260                    if (collection2.isEmpty()) {
261                            return _toSet(collection1);
262                    }
263    
264                    Set<T> set1 = _toSet(collection1);
265                    Set<T> set2 = _toSet(collection2);
266    
267                    Set<T> intersection = intersect(set1, set2);
268    
269                    if (set1.size() > set2.size()) {
270                            set1.addAll(set2);
271                    }
272                    else {
273                            set2.addAll(set1);
274    
275                            set1 = set2;
276                    }
277    
278                    set1.removeAll(intersection);
279    
280                    return set1;
281            }
282    
283            public static Set<Long> symmetricDifference(long[] array1, long[] array2) {
284                    return symmetricDifference(fromArray(array1), fromArray(array2));
285            }
286    
287            private static <T> Set<T> _toSet(Collection<T> collection) {
288                    if (collection instanceof Set) {
289                            return (Set<T>)collection;
290                    }
291    
292                    return new HashSet<T>(collection);
293            }
294    
295    }