bb.util
Class SetUtil

java.lang.Object
  extended by bb.util.SetUtil

public final class SetUtil
extends Object

Provides static utility methods for Sets.

Every method comes in two versions. Tne first type takes SortedSet args, and preserves the sortedness in the result, which is always a NavigableSet instance. The second type takes arbitrary Sets.

Every method returns a new Set instance, and never modifies any of its args.

This class is multithread safe: it is stateless.

Author:
Brent Boyer

Nested Class Summary
static class SetUtil.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Constructor Summary
private SetUtil()
          This private constructor suppresses the default (public) constructor, ensuring non-instantiability.
 
Method Summary
static
<T> Set<T>
difference(Set<? extends T> set1, Set<? extends T> set2)
          Returns a new Set instance that is the difference of set1 and set2 (i.e. every element of set1 that is not in set2).
private static
<T> Set<T>
difference(Set<? extends T> set1, Set<? extends T> set2, Set<T> target)
           
static
<T extends Comparable>
NavigableSet<T>
difference(SortedSet<T> set1, SortedSet<T> set2)
          Returns a new NavigableSet instance that is the difference of set1 and set2 (i.e. every element of set1 that is not in set2).
static
<T> Set<T>
intersection(Set<? extends T> set1, Set<? extends T> set2)
          Returns a new Set instance that is the intersection of set1 and set2 (i.e. every element that is in both set1 and set2).
private static
<T> Set<T>
intersection(Set<? extends T> set1, Set<? extends T> set2, Set<T> target)
           
static
<T extends Comparable>
NavigableSet<T>
intersection(SortedSet<T> set1, SortedSet<T> set2)
          Returns a new NavigableSet instance that is the intersection of set1 and set2 (i.e. every element that is in both set1 and set2).
static
<T> Set<T>
union(Set<? extends T> set1, Set<? extends T> set2)
          Returns a new Set instance that is the union of set1 and set2 (i.e. every element that is in either set1 or set2).
private static
<T> Set<T>
union(Set<? extends T> set1, Set<? extends T> set2, Set<T> target)
           
static
<T extends Comparable>
NavigableSet<T>
union(SortedSet<T> set1, SortedSet<T> set2)
          Returns a new NavigableSet instance that is the union of set1 and set2 (i.e. every element that is in either set1 or set2).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SetUtil

private SetUtil()
This private constructor suppresses the default (public) constructor, ensuring non-instantiability.

Method Detail

union

public static <T extends Comparable> NavigableSet<T> union(SortedSet<T> set1,
                                                           SortedSet<T> set2)
                                                throws IllegalArgumentException
Returns a new NavigableSet instance that is the union of set1 and set2 (i.e. every element that is in either set1 or set2).

The args set1 and set2 are two SortedSets that share the same Comparable element type. The result is a NavigableSet of the same element type.

Throws:
IllegalArgumentException - if set1 == null; set2 == null

union

public static <T> Set<T> union(Set<? extends T> set1,
                               Set<? extends T> set2)
                    throws IllegalArgumentException
Returns a new Set instance that is the union of set1 and set2 (i.e. every element that is in either set1 or set2).

The args set1 and set2 may be any type of Set class. The element type of the result is the most specific common superclass of set1 and set2.

Throws:
IllegalArgumentException - if set1 == null; set2 == null

union

private static <T> Set<T> union(Set<? extends T> set1,
                                Set<? extends T> set2,
                                Set<T> target)
                     throws IllegalArgumentException
Throws:
IllegalArgumentException

intersection

public static <T extends Comparable> NavigableSet<T> intersection(SortedSet<T> set1,
                                                                  SortedSet<T> set2)
                                                       throws IllegalArgumentException
Returns a new NavigableSet instance that is the intersection of set1 and set2 (i.e. every element that is in both set1 and set2).

The args set1 and set2 are two SortedSets that share the same Comparable element type. The result is a NavigableSet of the same element type.

Throws:
IllegalArgumentException - if set1 == null; set2 == null

intersection

public static <T> Set<T> intersection(Set<? extends T> set1,
                                      Set<? extends T> set2)
                           throws IllegalArgumentException
Returns a new Set instance that is the intersection of set1 and set2 (i.e. every element that is in both set1 and set2).

The args set1 and set2 may be any type of Set class. The element type of the result is the most specific common superclass of set1 and set2.

Throws:
IllegalArgumentException - if set1 == null; set2 == null

intersection

private static <T> Set<T> intersection(Set<? extends T> set1,
                                       Set<? extends T> set2,
                                       Set<T> target)
                            throws IllegalArgumentException
Throws:
IllegalArgumentException

difference

public static <T extends Comparable> NavigableSet<T> difference(SortedSet<T> set1,
                                                                SortedSet<T> set2)
                                                     throws IllegalArgumentException
Returns a new NavigableSet instance that is the difference of set1 and set2 (i.e. every element of set1 that is not in set2).

The args set1 and set2 are two SortedSets that share the same Comparable element type. The result is a NavigableSet of the same element type.

Throws:
IllegalArgumentException - if set1 == null; set2 == null

difference

public static <T> Set<T> difference(Set<? extends T> set1,
                                    Set<? extends T> set2)
                         throws IllegalArgumentException
Returns a new Set instance that is the difference of set1 and set2 (i.e. every element of set1 that is not in set2).

The args set1 and set2 may be any type of Set class. The element type of the result is the most specific common superclass of set1 and set2.

Throws:
IllegalArgumentException - if set1 == null; set2 == null

difference

private static <T> Set<T> difference(Set<? extends T> set1,
                                     Set<? extends T> set2,
                                     Set<T> target)
                          throws IllegalArgumentException
Throws:
IllegalArgumentException