1 16 package org.apache.commons.collections; 17 18 import java.util.Collection ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.Set ; 22 import java.util.SortedSet ; 23 import java.util.TreeSet ; 24 25 import org.apache.commons.collections.set.ListOrderedSet; 26 import org.apache.commons.collections.set.PredicatedSet; 27 import org.apache.commons.collections.set.PredicatedSortedSet; 28 import org.apache.commons.collections.set.SynchronizedSet; 29 import org.apache.commons.collections.set.SynchronizedSortedSet; 30 import org.apache.commons.collections.set.TransformedSet; 31 import org.apache.commons.collections.set.TransformedSortedSet; 32 import org.apache.commons.collections.set.TypedSet; 33 import org.apache.commons.collections.set.TypedSortedSet; 34 import org.apache.commons.collections.set.UnmodifiableSet; 35 import org.apache.commons.collections.set.UnmodifiableSortedSet; 36 37 49 public class SetUtils { 50 51 56 public static final Set EMPTY_SET = Collections.EMPTY_SET; 57 61 public static final SortedSet EMPTY_SORTED_SET = UnmodifiableSortedSet.decorate(new TreeSet ()); 62 63 66 public SetUtils() { 67 } 68 69 98 public static boolean isEqualSet(final Collection set1, final Collection set2) { 99 if (set1 == set2) { 100 return true; 101 } 102 if (set1 == null || set2 == null || set1.size() != set2.size()) { 103 return false; 104 } 105 106 return set1.containsAll(set2); 107 } 108 109 121 public static int hashCodeForSet(final Collection set) { 122 if (set == null) { 123 return 0; 124 } 125 int hashCode = 0; 126 Iterator it = set.iterator(); 127 Object obj = null; 128 129 while (it.hasNext()) { 130 obj = it.next(); 131 if (obj != null) { 132 hashCode += obj.hashCode(); 133 } 134 } 135 return hashCode; 136 } 137 138 161 public static Set synchronizedSet(Set set) { 162 return SynchronizedSet.decorate(set); 163 } 164 165 174 public static Set unmodifiableSet(Set set) { 175 return UnmodifiableSet.decorate(set); 176 } 177 178 191 public static Set predicatedSet(Set set, Predicate predicate) { 192 return PredicatedSet.decorate(set, predicate); 193 } 194 195 204 public static Set typedSet(Set set, Class type) { 205 return TypedSet.decorate(set, type); 206 } 207 208 220 public static Set transformedSet(Set set, Transformer transformer) { 221 return TransformedSet.decorate(set, transformer); 222 } 223 224 235 public static Set orderedSet(Set set) { 236 return ListOrderedSet.decorate(set); 237 } 238 239 262 public static SortedSet synchronizedSortedSet(SortedSet set) { 263 return SynchronizedSortedSet.decorate(set); 264 } 265 266 275 public static SortedSet unmodifiableSortedSet(SortedSet set) { 276 return UnmodifiableSortedSet.decorate(set); 277 } 278 279 292 public static SortedSet predicatedSortedSet(SortedSet set, Predicate predicate) { 293 return PredicatedSortedSet.decorate(set, predicate); 294 } 295 296 305 public static SortedSet typedSortedSet(SortedSet set, Class type) { 306 return TypedSortedSet.decorate(set, type); 307 } 308 309 321 public static SortedSet transformedSortedSet(SortedSet set, Transformer transformer) { 322 return TransformedSortedSet.decorate(set, transformer); 323 } 324 325 } 326 | Popular Tags |