|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.collections.BagUtils
public class BagUtils
Provides utility methods and decorators for
Bag
and SortedBag
instances.
Field Summary | |
---|---|
static Bag |
EMPTY_BAG
An empty unmodifiable bag. |
static Bag |
EMPTY_SORTED_BAG
An empty unmodifiable sorted bag. |
Constructor Summary | |
---|---|
BagUtils()
Instantiation of BagUtils is not intended or required. |
Method Summary | ||
---|---|---|
static
|
predicatedBag(Bag<E> bag,
Predicate<? super E> predicate)
Returns a predicated (validating) bag backed by the given bag. |
|
static
|
predicatedSortedBag(SortedBag<E> bag,
Predicate<? super E> predicate)
Returns a predicated (validating) sorted bag backed by the given sorted bag. |
|
static
|
synchronizedBag(Bag<E> bag)
Returns a synchronized (thread-safe) bag backed by the given bag. |
|
static
|
synchronizedSortedBag(SortedBag<E> bag)
Returns a synchronized (thread-safe) sorted bag backed by the given sorted bag. |
|
static
|
transformedBag(Bag<I> bag,
Transformer<I,O> transformer)
Deprecated. TransformedCollections are not type-safe in Java 1.5. |
|
static
|
transformedSortedBag(SortedBag<I> bag,
Transformer<I,O> transformer)
Deprecated. This breaks the java.util.Collection interface in Java 1.5. It is recommended that it not be used. |
|
static
|
typedBag(Bag<E> bag,
Class<E> type)
Deprecated. Java 1.5 generics makes this method no longer useful. |
|
static
|
typedSortedBag(SortedBag<E> bag,
Class<E> type)
Deprecated. Java 1.5 generics makes this method no longer useful. |
|
static
|
unmodifiableBag(Bag<E> bag)
Returns an unmodifiable view of the given bag. |
|
static
|
unmodifiableSortedBag(SortedBag<E> bag)
Returns an unmodifiable view of the given sorted bag. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Bag EMPTY_BAG
public static final Bag EMPTY_SORTED_BAG
Constructor Detail |
---|
public BagUtils()
Method Detail |
---|
public static <E> Bag<E> synchronizedBag(Bag<E> bag)
It is imperative that the user manually synchronize on the returned bag when iterating over it:
Bag bag = BagUtils.synchronizedBag(new HashBag()); ... synchronized(bag) { Iterator i = bag.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } }
Failure to follow this advice may result in non-deterministic behavior.
bag
- the bag to synchronize, must not be null
IllegalArgumentException
- if the Bag is nullpublic static <E> Bag<E> unmodifiableBag(Bag<E> bag)
UnsupportedOperationException
.
bag
- the bag whose unmodifiable view is to be returned, must not be null
IllegalArgumentException
- if the Bag is nullpublic static <E> Bag<E> predicatedBag(Bag<E> bag, Predicate<? super E> predicate)
Only objects that pass the test in the given predicate can be added to the bag. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original bag after invoking this method, as it is a backdoor for adding invalid objects.
bag
- the bag to predicate, must not be nullpredicate
- the predicate for the bag, must not be null
IllegalArgumentException
- if the Bag or Predicate is nullpublic static <E> Bag<E> typedBag(Bag<E> bag, Class<E> type)
Only objects of the specified type can be added to the bag.
bag
- the bag to limit to a specific type, must not be nulltype
- the type of objects which may be added to the bag
public static <I,O> Bag<O> transformedBag(Bag<I> bag, Transformer<I,O> transformer)
Each object is passed through the transformer as it is added to the Bag. It is important not to use the original bag after invoking this method, as it is a backdoor for adding untransformed objects.
bag
- the bag to predicate, must not be nulltransformer
- the transformer for the bag, must not be null
IllegalArgumentException
- if the Bag or Transformer is nullpublic static <E> SortedBag<E> synchronizedSortedBag(SortedBag<E> bag)
It is imperative that the user manually synchronize on the returned bag when iterating over it:
SortedBag bag = BagUtils.synchronizedSortedBag(new TreeBag()); ... synchronized(bag) { Iterator i = bag.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } }
Failure to follow this advice may result in non-deterministic behavior.
bag
- the bag to synchronize, must not be null
IllegalArgumentException
- if the SortedBag is nullpublic static <E> SortedBag<E> unmodifiableSortedBag(SortedBag<E> bag)
UnsupportedOperationException
.
bag
- the bag whose unmodifiable view is to be returned, must not be null
IllegalArgumentException
- if the SortedBag is nullpublic static <E> SortedBag<E> predicatedSortedBag(SortedBag<E> bag, Predicate<? super E> predicate)
Only objects that pass the test in the given predicate can be added to the bag. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original bag after invoking this method, as it is a backdoor for adding invalid objects.
bag
- the sorted bag to predicate, must not be nullpredicate
- the predicate for the bag, must not be null
IllegalArgumentException
- if the SortedBag or Predicate is nullpublic static <E> SortedBag<E> typedSortedBag(SortedBag<E> bag, Class<E> type)
Only objects of the specified type can be added to the bag.
bag
- the bag to limit to a specific type, must not be nulltype
- the type of objects which may be added to the bag
public static <I,O> SortedBag<O> transformedSortedBag(SortedBag<I> bag, Transformer<I,O> transformer)
Each object is passed through the transformer as it is added to the Bag. It is important not to use the original bag after invoking this method, as it is a backdoor for adding untransformed objects.
bag
- the bag to predicate, must not be nulltransformer
- the transformer for the bag, must not be null
IllegalArgumentException
- if the Bag or Transformer is null
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |