org.omnaest.utils.structure.collection.set
Class SetUtils

java.lang.Object
  extended by org.omnaest.utils.structure.collection.set.SetUtils

public class SetUtils
extends Object

Helper for Set types

Author:
Omnaest
See Also:
CollectionUtils

Constructor Summary
SetUtils()
           
 
Method Summary
static
<E> Set<E>
adapter(List<E> list)
          Returns a new ListToSetAdapter for the given List
static
<FROM,TO> Set<TO>
adapter(Set<FROM> set, ElementBidirectionalConverter<FROM,TO> elementBidirectionalConverter)
          Returns a new SetToSetAdapter for the given Set
static
<E> Set<E>
add(Set<E> set, E... elements)
          Returns the given Set, or a new instance if the given Set is null.
static
<E> Set<E>
addAll(Set<? extends E> set, Iterable<? extends E> elementIterable)
          Adds the elements from the given Iterable to the given Set.
static
<E> Set<E>
composite(Collection<Set<E>> setCollection)
          Returns a SetComposite instance for the given Sets
static
<E> Set<E>
composite(Set<E>... sets)
          Returns a SetComposite instance for the given Sets
static
<FROM,TO> Set<TO>
convert(ElementConverter<FROM,TO> elementConverter, FROM... elements)
          Transforms elements to a Set of instances of another type using a given ElementConverter.
static
<FROM,TO> Set<TO>
convert(Iterable<FROM> iterable, ElementConverter<FROM,TO> elementConverter)
          Transforms a given Collection instance from one generic type into the other using a given ElementConverter.
static
<E> SetDelta<E>
delta(Set<E> firstSet, Set<E> secondSet)
          Returns a new SetDelta instance for the given Sets
static
<E> Set<E>
emptySet()
          Returns a new LinkedHashSet instance
static
<E> Set<E>
filter(Iterable<E> iterable, ElementFilter<E> elementFilter)
          Filters the given Iterable and returns a Set which only contains the elements which are not filtered out by the given ElementFilter.
static
<E> Set<E>
intersection(Collection<? extends Collection<E>> collectionOfCollections)
          Returns the intersection of the Collections of the given container Collection
static
<E> Set<E>
intersection(Collection<E>... collections)
          Returns the intersection of the Collections of the given container Collection
static
<E> Set<E>
intersection(Collection<E> collectionFirst, Collection<E> collectionSecond)
          Returns the intersection of the Collections of the given container Collection
static
<E> Set<E>
intersection(Set<E> set1, Set<E> set2)
          Returns the intersection of two given Set instances by iterating over the smaller given Set and testing on the Set.contains(Object) method of the larger Set.
static boolean isSetType(Class<?> type)
          Returns true if the given type is assignable to the Set interface
static boolean isSortedSetType(Class<?> type)
          Returns true if the given type is assignable to the SortedSet interface
static
<E> Set<E>
locked(Set<E> set, Lock lock)
          Returns a view of the given Set using the given Lock to synchronize all of its methods
static
<E> Set<E>
lockedByReentrantLock(Set<E> set)
          Returns a view of the given Set using a new ReentrantLock instance to synchronize all of its methods
static
<E> Set<E>
mergeAll(Collection<? extends Collection<E>> collectionOfCollections)
          Merges all elements of the given Collection instances into one single Set instance .
static
<E> Set<E>
mergeAll(Collection<E>... collections)
          Merges all elements of the given Collection instances into one single Set instance.
static
<E> Set<E>
mergeAll(Iterable<? extends Collection<E>> iterableOfCollections)
          Similar to mergeAll(Collection)
static
<E> Set<E>
remove(Set<E> set, E element)
          Removes the given element from the given Set instance.
static
<E> Set<E>
removeAll(Set<E> set, Iterable<? extends E> removingIterable)
          Returns the given Set instance reduced by the elements of the given Iterable
static
<E> Set<E>
removeAllAsNewSet(Set<E> set, Iterable<E> removingIterable)
          Returns a new Set instance containing the elements of the given Set reduced by the elements of the given Iterable
static
<E> Set<E>
retainAll(Set<E> set, Collection<? extends E> retainableCollection)
          Does call Set.retainAll(Collection) on the given Set instance.
static
<E> Set<E>
valueOf(E... elements)
          Same as valueOf(Iterable) for one or more elements
static
<E> Set<E>
valueOf(Iterable<E> iterable)
          Returns a new LinkedHashSet instance with the element of the given Iterable which keeps the order of the elements
static
<E> Set<E>
valueOf(Iterator<E> iterator)
          Similar to valueOf(Iterable)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SetUtils

public SetUtils()
Method Detail

delta

public static <E> SetDelta<E> delta(Set<E> firstSet,
                                    Set<E> secondSet)
Returns a new SetDelta instance for the given Sets

Parameters:
firstSet -
secondSet -
Returns:
SetDelta instance
See Also:
MapUtils.delta(java.util.Map, java.util.Map)

removeAll

public static <E> Set<E> removeAll(Set<E> set,
                                   Iterable<? extends E> removingIterable)
Returns the given Set instance reduced by the elements of the given Iterable

Parameters:
set - Set
removingIterable - Iterable
Returns:
same Set instance or null, if null is given

remove

public static <E> Set<E> remove(Set<E> set,
                                E element)
Removes the given element from the given Set instance.

Parameters:
set -
element -
Returns:
the given Set instance or null if null is given

removeAllAsNewSet

public static <E> Set<E> removeAllAsNewSet(Set<E> set,
                                           Iterable<E> removingIterable)
Returns a new Set instance containing the elements of the given Set reduced by the elements of the given Iterable

Parameters:
set - Set
removingIterable - Iterable
Returns:
new Set instance always

add

public static <E> Set<E> add(Set<E> set,
                             E... elements)
Returns the given Set, or a new instance if the given Set is null. The returned Set will have the given elements added.

Parameters:
set -
elements -
Returns:
given Set or new instance if null

addAll

public static <E> Set<E> addAll(Set<? extends E> set,
                                Iterable<? extends E> elementIterable)
Adds the elements from the given Iterable to the given Set. If the given Set is null, a new LinkedHashSet is returned. If the given Iterable is null, nothing is added.

Parameters:
set - Set
elementIterable - Iterable
Returns:
given Set instance or new LinkedHashSet

retainAll

public static <E> Set<E> retainAll(Set<E> set,
                                   Collection<? extends E> retainableCollection)
Does call Set.retainAll(Collection) on the given Set instance. If null is given as Set null is returned as result. If null is given as retainable element Collection the given Set is cleared.

Parameters:
set -
retainableCollection -
Returns:
given instance or null if null is given

mergeAll

public static <E> Set<E> mergeAll(Collection<E>... collections)
Merges all elements of the given Collection instances into one single Set instance.

Type Parameters:
E -
Parameters:
collections -
Returns:
See Also:
mergeAll(Collection)

mergeAll

public static <E> Set<E> mergeAll(Collection<? extends Collection<E>> collectionOfCollections)
Merges all elements of the given Collection instances into one single Set instance .

Type Parameters:
E -
Parameters:
collectionOfCollections -
Returns:
See Also:
mergeAll(Collection...)

mergeAll

public static <E> Set<E> mergeAll(Iterable<? extends Collection<E>> iterableOfCollections)
Similar to mergeAll(Collection)

Parameters:
iterableOfCollections -
Returns:
LinkedHashSet

intersection

public static <E> Set<E> intersection(Collection<? extends Collection<E>> collectionOfCollections)
Returns the intersection of the Collections of the given container Collection

Parameters:
collectionOfCollections -
Returns:

intersection

public static <E> Set<E> intersection(Set<E> set1,
                                      Set<E> set2)
Returns the intersection of two given Set instances by iterating over the smaller given Set and testing on the Set.contains(Object) method of the larger Set.

Parameters:
set1 -
set2 -
Returns:
new Set instance

intersection

public static <E> Set<E> intersection(Collection<E>... collections)
Returns the intersection of the Collections of the given container Collection

Parameters:
collections -
Returns:

intersection

public static <E> Set<E> intersection(Collection<E> collectionFirst,
                                      Collection<E> collectionSecond)
Returns the intersection of the Collections of the given container Collection

Parameters:
collectionFirst -
collectionSecond -
Returns:
new Set instance

valueOf

public static <E> Set<E> valueOf(E... elements)
Same as valueOf(Iterable) for one or more elements

Parameters:
elements -
Returns:
a new LinkedHashSet instance containing all given elements

emptySet

public static <E> Set<E> emptySet()
Returns a new LinkedHashSet instance

Returns:

valueOf

public static <E> Set<E> valueOf(Iterator<E> iterator)
Similar to valueOf(Iterable)

Parameters:
iterator -
Returns:

valueOf

public static <E> Set<E> valueOf(Iterable<E> iterable)
Returns a new LinkedHashSet instance with the element of the given Iterable which keeps the order of the elements

Parameters:
iterable -
Returns:

locked

public static <E> Set<E> locked(Set<E> set,
                                Lock lock)
Returns a view of the given Set using the given Lock to synchronize all of its methods

Parameters:
set -
lock -
Returns:
See Also:
lockedByReentrantLock(Set)

lockedByReentrantLock

public static <E> Set<E> lockedByReentrantLock(Set<E> set)
Returns a view of the given Set using a new ReentrantLock instance to synchronize all of its methods

Parameters:
set -
Returns:
See Also:
locked(Set, Lock)

convert

public static <FROM,TO> Set<TO> convert(ElementConverter<FROM,TO> elementConverter,
                                        FROM... elements)
Transforms elements to a Set of instances of another type using a given ElementConverter.

Parameters:
elementConverter -
elements -
See Also:
convert(Iterable, ElementConverter)

convert

public static <FROM,TO> Set<TO> convert(Iterable<FROM> iterable,
                                        ElementConverter<FROM,TO> elementConverter)
Transforms a given Collection instance from one generic type into the other using a given ElementConverter.

Parameters:
iterable -
elementConverter -

filter

public static <E> Set<E> filter(Iterable<E> iterable,
                                ElementFilter<E> elementFilter)
Filters the given Iterable and returns a Set which only contains the elements which are not filtered out by the given ElementFilter.

Parameters:
iterable - Iterable
elementFilter - ElementFilter
Returns:
new Set instance

adapter

public static <FROM,TO> Set<TO> adapter(Set<FROM> set,
                                        ElementBidirectionalConverter<FROM,TO> elementBidirectionalConverter)
Returns a new SetToSetAdapter for the given Set

Parameters:
set - Set
elementBidirectionalConverter - ElementBidirectionalConverter
Returns:
new SetToSetAdapter instance

adapter

public static <E> Set<E> adapter(List<E> list)
Returns a new ListToSetAdapter for the given List

Parameters:
list - List
Returns:
Set adapter instance

composite

public static <E> Set<E> composite(Set<E>... sets)
Returns a SetComposite instance for the given Sets

Parameters:
sets -
Returns:

composite

public static <E> Set<E> composite(Collection<Set<E>> setCollection)
Returns a SetComposite instance for the given Sets

Parameters:
setCollection -
Returns:

isSetType

public static boolean isSetType(Class<?> type)
Returns true if the given type is assignable to the Set interface

Parameters:
type -
Returns:
See Also:
isSortedSetType(Class)

isSortedSetType

public static boolean isSortedSetType(Class<?> type)
Returns true if the given type is assignable to the SortedSet interface

Parameters:
type -
Returns:
See Also:
isSetType(Class)


Copyright © 2013. All Rights Reserved.