ariba.util.core
Class SetUtil

java.lang.Object
  extended by ariba.util.core.SetUtil

public final class SetUtil
extends java.lang.Object

Set Utilities. These are helper functions for dealing with sets.


Field Summary
static java.util.SortedSet EmptySortedSet
          An immutable, empty SortedSet.
static java.lang.Class ImmutableSetClass
           
 
Method Summary
static
<T> java.util.Set<T>
arrayToSet(T[] array, boolean assertIfDuplicateElementFound)
          Creates a Set from the objects in the given array.

Note, the elements in the array are NOT copied.

static
<T> java.util.Set<T>
cloneSet(java.util.Set<? extends T> set)
          Clones and returns the specified set if the actual dynamic type of set supports clone (that is, implements Cloneable and has a publicly available clone() method.)
static
<T> java.util.SortedSet<T>
emptySortedSet()
           
static
<T> T
getAnyElement(java.util.Set<T> set)
          Retrieve a random element from set.
static
<T> java.util.Set<T>
immutableSet(java.util.Set<? extends T> s)
          Returns a version of the Set that is not modifiable If s is null, null will be returned.
static
<T> java.util.Set<T>
intersect(java.util.Set<? extends T> set1, java.util.Set<? extends T> set2)
          Perform the operation intersect on two given sets.
static
<T> void
intersect(java.util.Set<? extends T> set1, java.util.Set<? extends T> set2, java.util.Set<T> result)
          Perform the operation intersect on two given sets.
static boolean nullOrEmptySet(java.util.Set<?> set)
          Determine if a Set is null or empty.
static
<T> java.util.Set<T>
set()
          Constructs an empty optionally type-safe Set.
static
<T> java.util.Set<T>
set(java.util.Collection<? extends T> source)
          Creates new Set with the same contents as unique elements in the given Collection.
static
<T> java.util.Set<T>
set(int initialCapacity)
          Constructs a Set capable of holding a specified number of elements.
static
<T> java.util.Set<T>
setAddAll(java.util.Collection<T> source)
          Returns new Set with all the items in source Collection added to it.
static
<T> java.util.SortedSet<T>
sortedSet()
          Creates an empty SortedSet.
static
<T> java.util.SortedSet<T>
sortedSet(java.util.Comparator<? super T> comparator)
          Creates an empty SortedSet

Given that compX is a Comparator<X>, compY is a Comparator<Y> (and that Y extends X) and comp is a raw Comparator, the following all compile validly.

static
<T> java.util.SortedSet<T>
sortedSet(java.util.SortedSet<? extends T> source)
          Creates a new SortedSet with the same content as the given SortedSet.
static
<T> java.util.Set<T>
subtract(java.util.Set<? extends T> set1, java.util.Set<? extends T> set2)
          Performs the operation subtract set2 from set1.
static
<T> void
subtract(java.util.Set<? extends T> set1, java.util.Set<? extends T> set2, java.util.Set<T> result)
          Performs the operation subtract set2 from set1.
static
<T> java.util.Set<T>
union(java.util.Set<? extends T> set1, java.util.Set<? extends T> set2)
          Returns a new Set containing all the elements from two sets
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EmptySortedSet

public static final java.util.SortedSet EmptySortedSet
An immutable, empty SortedSet.


ImmutableSetClass

public static final java.lang.Class ImmutableSetClass
Method Detail

set

public static <T> java.util.Set<T> set()
Constructs an empty optionally type-safe Set.

To construct a type-safe Set:

For a raw Set: There will be no compile warnings either way.

Returns:
new empty Set

set

public static <T> java.util.Set<T> set(int initialCapacity)
Constructs a Set capable of holding a specified number of elements.

To construct a type-safe Set:

For a raw Set: There will be no compile warnings either way.

Parameters:
initialCapacity - the number of elements this Set is capable of holding before needing to grow.
Returns:
new empty Set with given initial capacity

set

public static <T> java.util.Set<T> set(java.util.Collection<? extends T> source)
Creates new Set with the same contents as unique elements in the given Collection.

Given that setOfX is a Set<X>, setOfY is a Set<Y> (and that Y extends X) and raw is a Set, the following all compile validly.

The following constructs compile with an unchecked warning:

Parameters:
source - source collection
Returns:
new Set with same contents as source
Throws:
java.lang.NullPointerException - if the specified Set is null.

cloneSet

public static <T> java.util.Set<T> cloneSet(java.util.Set<? extends T> set)
Clones and returns the specified set if the actual dynamic type of set supports clone (that is, implements Cloneable and has a publicly available clone() method.)

If you just want a java.util.Set, then you're better off using set(java.util.Collection), since it's guaranteed to work and is probably just as fast on small sets.

Parameters:
set - the Set to clone
Returns:
a new Set of the same type

immutableSet

public static <T> java.util.Set<T> immutableSet(java.util.Set<? extends T> s)
Returns a version of the Set that is not modifiable If s is null, null will be returned.

Given that setOfX is a Set<X>, setOfY is a Set<Y> (and that Y extends X) and raw is a Set, the following all compile validly.

The following constructs compile with an unchecked warning:

Returns:
new Set with same contents as s but is not modifiable

sortedSet

public static <T> java.util.SortedSet<T> sortedSet()
Creates an empty SortedSet.

To construct a type-safe Set:

For a raw Set: There will be no compile warnings either way.

Returns:
new empty SortedSet
See Also:
SortedSet

sortedSet

public static <T> java.util.SortedSet<T> sortedSet(java.util.Comparator<? super T> comparator)
Creates an empty SortedSet

Given that compX is a Comparator<X>, compY is a Comparator<Y> (and that Y extends X) and comp is a raw Comparator, the following all compile validly.

The following constructs compile with an unchecked warning:

Parameters:
comparator - the non-null comparator that will be used to sort the Set
Returns:
new empty SortedSet
See Also:
SortedSet

sortedSet

public static <T> java.util.SortedSet<T> sortedSet(java.util.SortedSet<? extends T> source)
Creates a new SortedSet with the same content as the given SortedSet.

Given that setOfX is a SortedSet<X>, setOfY is a SortedSet<Y> (and that Y extends X) and raw is a raw SortedSet, the following all compile validly.

The following constructs compile with an unchecked warning:

Parameters:
source - the non-null sorted set based on the given SortedSet
Returns:
SortedSet containing the elements of the source
See Also:
SortedSet

arrayToSet

public static <T> java.util.Set<T> arrayToSet(T[] array,
                                              boolean assertIfDuplicateElementFound)
Creates a Set from the objects in the given array.

Note, the elements in the array are NOT copied.

Parameters:
array - to use as the initial contents of the Set; may not be null
assertIfDuplicateElementFound - if true the method asserts if an duplicated element is found in the input array otherwise it will be ignored
Returns:
Set containing objects from the specified array

setAddAll

public static <T> java.util.Set<T> setAddAll(java.util.Collection<T> source)
Returns new Set with all the items in source Collection added to it. Combines SetUtil.list() and Set addAll(Collection). Throws NullPointerException if source is null.


nullOrEmptySet

public static boolean nullOrEmptySet(java.util.Set<?> set)
Determine if a Set is null or empty.

Parameters:
set - object to check
Returns:
true if set is null or empty, false otherwise

getAnyElement

public static <T> T getAnyElement(java.util.Set<T> set)
Retrieve a random element from set.

Note that the set can contain null element (at most one).

Parameters:
set - object from which to retrieve an element
Returns:
any element of set if it's not null or empty, assert otherwise.

intersect

public static <T> java.util.Set<T> intersect(java.util.Set<? extends T> set1,
                                             java.util.Set<? extends T> set2)
Perform the operation intersect on two given sets. The operation is nondestructive.

If one of the sets is null or empty the method returns an empty set.

Parameters:
set1 - input set
set2 - input set
Returns:
the intersect of the given sets.

intersect

public static <T> void intersect(java.util.Set<? extends T> set1,
                                 java.util.Set<? extends T> set2,
                                 java.util.Set<T> result)
Perform the operation intersect on two given sets. The operation is nondestructive.

If one of the sets is null or empty, nothing would be added to the result set.

Parameters:
set1 - input set
set2 - input set
result - result set

subtract

public static <T> java.util.Set<T> subtract(java.util.Set<? extends T> set1,
                                            java.util.Set<? extends T> set2)
Performs the operation subtract set2 from set1. The operation is nondestructive.

Parameters:
set1 - input set
set2 - input set
Returns:
set containing the elements of the operation set1 - set2. If set1 is null or empty the method returns an empty set. If on the other hand set2 is null or empty the method returns a copy of set1.

subtract

public static <T> void subtract(java.util.Set<? extends T> set1,
                                java.util.Set<? extends T> set2,
                                java.util.Set<T> result)
Performs the operation subtract set2 from set1. The operation is nondestructive.

Parameters:
set1 - input set
set2 - input set
result - result set (containing the elements of the operation set1 - set2. If set1 is null or empty, no element would be added to it. If on the other hand set2 is null or empty, all the elements in set1 would be added to it).

union

public static <T> java.util.Set<T> union(java.util.Set<? extends T> set1,
                                         java.util.Set<? extends T> set2)
Returns a new Set containing all the elements from two sets

Parameters:
set1 - input set
set2 - input set
Returns:
the union of the given sets.

emptySortedSet

public static <T> java.util.SortedSet<T> emptySortedSet()


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.