From 2a90a1d77264b8581b8e6a064c06c4906c62a2d1 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Tue, 15 Sep 2009 05:56:47 +0000 Subject: [PATCH] Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956. Also see the following revisions: ------------------------------------------------------------------------ r555925 | skestle | 2007-07-13 03:39:24 -0700 (Fri, 13 Jul 2007) | 2 lines Added Edwin Tellman's patch for COLLECTIONS-243. It all seems pretty reasonable, and it should all be checked again as the project is worked through ------------------------------------------------------------------------ r471166 | scolebourne | 2006-11-04 03:33:22 -0800 (Sat, 04 Nov 2006) | 1 line Removed Typed* containers such as TypedList and TypedMap as generics now provides type safety ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815104 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/collections/SetUtils.java | 82 ++++++++----------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/src/java/org/apache/commons/collections/SetUtils.java b/src/java/org/apache/commons/collections/SetUtils.java index 90ed64d99..f0cf5b39e 100644 --- a/src/java/org/apache/commons/collections/SetUtils.java +++ b/src/java/org/apache/commons/collections/SetUtils.java @@ -18,7 +18,6 @@ package org.apache.commons.collections; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -30,8 +29,6 @@ import org.apache.commons.collections.set.SynchronizedSet; import org.apache.commons.collections.set.SynchronizedSortedSet; import org.apache.commons.collections.set.TransformedSet; import org.apache.commons.collections.set.TransformedSortedSet; -import org.apache.commons.collections.set.TypedSet; -import org.apache.commons.collections.set.TypedSortedSet; import org.apache.commons.collections.set.UnmodifiableSet; import org.apache.commons.collections.set.UnmodifiableSortedSet; @@ -54,12 +51,32 @@ public class SetUtils { * This uses the {@link Collections} implementation * and is provided for completeness. */ - public static final Set EMPTY_SET = Collections.EMPTY_SET; + public static final Set EMPTY_SET = Collections.EMPTY_SET; + + /** + * Get a typed empty unmodifiable Set. + * @param + * @return Set + */ + public static Set emptySet() { + return Collections.emptySet(); + } + /** * An empty unmodifiable sorted set. * This is not provided in the JDK. */ - public static final SortedSet EMPTY_SORTED_SET = UnmodifiableSortedSet.decorate(new TreeSet()); + public static final SortedSet EMPTY_SORTED_SET = UnmodifiableSortedSet.decorate(new TreeSet()); + + /** + * Get a typed empty unmodifiable sorted set. + * @param + * @return SortedSet + */ + @SuppressWarnings("unchecked") + public static SortedSet emptySortedSet() { + return (SortedSet) EMPTY_SORTED_SET; + } /** * SetUtils should not normally be instantiated. @@ -96,7 +113,7 @@ public class SetUtils { * @param set2 the second set, may be null * @return whether the sets are equal by value comparison */ - public static boolean isEqualSet(final Collection set1, final Collection set2) { + public static boolean isEqualSet(final Collection set1, final Collection set2) { if (set1 == set2) { return true; } @@ -119,16 +136,13 @@ public class SetUtils { * @param set the set to calculate the hash code for, may be null * @return the hash code */ - public static int hashCodeForSet(final Collection set) { + public static int hashCodeForSet(final Collection set) { if (set == null) { return 0; } - int hashCode = 0; - Iterator it = set.iterator(); - Object obj = null; - while (it.hasNext()) { - obj = it.next(); + int hashCode = 0; + for (T obj : set) { if (obj != null) { hashCode += obj.hashCode(); } @@ -159,7 +173,7 @@ public class SetUtils { * @return a synchronized set backed by the given set * @throws IllegalArgumentException if the set is null */ - public static Set synchronizedSet(Set set) { + public static Set synchronizedSet(Set set) { return SynchronizedSet.decorate(set); } @@ -172,7 +186,7 @@ public class SetUtils { * @return an unmodifiable set backed by the given set * @throws IllegalArgumentException if the set is null */ - public static Set unmodifiableSet(Set set) { + public static Set unmodifiableSet(Set set) { return UnmodifiableSet.decorate(set); } @@ -189,23 +203,10 @@ public class SetUtils { * @return a predicated set backed by the given set * @throws IllegalArgumentException if the Set or Predicate is null */ - public static Set predicatedSet(Set set, Predicate predicate) { + public static Set predicatedSet(Set set, Predicate predicate) { return PredicatedSet.decorate(set, predicate); } - /** - * Returns a typed set backed by the given set. - *

- * Only objects of the specified type can be added to the set. - * - * @param set the set to limit to a specific type, must not be null - * @param type the type of objects which may be added to the set - * @return a typed set backed by the specified set - */ - public static Set typedSet(Set set, Class type) { - return TypedSet.decorate(set, type); - } - /** * Returns a transformed set backed by the given set. *

@@ -221,7 +222,7 @@ public class SetUtils { * @return a transformed set backed by the given set * @throws IllegalArgumentException if the Set or Transformer is null */ - public static Set transformedSet(Set set, Transformer transformer) { + public static Set transformedSet(Set set, Transformer transformer) { return TransformedSet.decorate(set, transformer); } @@ -236,7 +237,7 @@ public class SetUtils { * @return an ordered set backed by the given set * @throws IllegalArgumentException if the Set is null */ - public static Set orderedSet(Set set) { + public static Set orderedSet(Set set) { return ListOrderedSet.decorate(set); } @@ -263,7 +264,7 @@ public class SetUtils { * @return a synchronized set backed by the given set * @throws IllegalArgumentException if the set is null */ - public static SortedSet synchronizedSortedSet(SortedSet set) { + public static SortedSet synchronizedSortedSet(SortedSet set) { return SynchronizedSortedSet.decorate(set); } @@ -276,7 +277,7 @@ public class SetUtils { * @return an unmodifiable set backed by the given set * @throws IllegalArgumentException if the set is null */ - public static SortedSet unmodifiableSortedSet(SortedSet set) { + public static SortedSet unmodifiableSortedSet(SortedSet set) { return UnmodifiableSortedSet.decorate(set); } @@ -293,23 +294,10 @@ public class SetUtils { * @return a predicated sorted set backed by the given sorted set * @throws IllegalArgumentException if the Set or Predicate is null */ - public static SortedSet predicatedSortedSet(SortedSet set, Predicate predicate) { + public static SortedSet predicatedSortedSet(SortedSet set, Predicate predicate) { return PredicatedSortedSet.decorate(set, predicate); } - /** - * Returns a typed sorted set backed by the given set. - *

- * Only objects of the specified type can be added to the set. - * - * @param set the set to limit to a specific type, must not be null - * @param type the type of objects which may be added to the set - * @return a typed set backed by the specified set - */ - public static SortedSet typedSortedSet(SortedSet set, Class type) { - return TypedSortedSet.decorate(set, type); - } - /** * Returns a transformed sorted set backed by the given set. *

@@ -325,7 +313,7 @@ public class SetUtils { * @return a transformed set backed by the given set * @throws IllegalArgumentException if the Set or Transformer is null */ - public static SortedSet transformedSortedSet(SortedSet set, Transformer transformer) { + public static SortedSet transformedSortedSet(SortedSet set, Transformer transformer) { return TransformedSortedSet.decorate(set, transformer); }