Class BagUtils
- java.lang.Object
-
- org.apache.commons.collections.BagUtils
-
@Deprecated(since="2021-04-30") public class BagUtils extends java.lang.Object
Deprecated.Commons Collections 3 is in maintenance mode. Commons Collections 4 should be used instead.- Since:
- Commons Collections 2.1
-
-
Field Summary
Fields Modifier and Type Field Description static Bag
EMPTY_BAG
Deprecated.An empty unmodifiable bag.static Bag
EMPTY_SORTED_BAG
Deprecated.An empty unmodifiable sorted bag.
-
Constructor Summary
Constructors Constructor Description BagUtils()
Deprecated.Instantiation of BagUtils is not intended or required.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Bag
predicatedBag(Bag bag, Predicate predicate)
Deprecated.Returns a predicated (validating) bag backed by the given bag.static SortedBag
predicatedSortedBag(SortedBag bag, Predicate predicate)
Deprecated.Returns a predicated (validating) sorted bag backed by the given sorted bag.static Bag
synchronizedBag(Bag bag)
Deprecated.Returns a synchronized (thread-safe) bag backed by the given bag.static SortedBag
synchronizedSortedBag(SortedBag bag)
Deprecated.Returns a synchronized (thread-safe) sorted bag backed by the given sorted bag.static Bag
transformedBag(Bag bag, Transformer transformer)
Deprecated.Returns a transformed bag backed by the given bag.static SortedBag
transformedSortedBag(SortedBag bag, Transformer transformer)
Deprecated.Returns a transformed sorted bag backed by the given bag.static Bag
typedBag(Bag bag, java.lang.Class type)
Deprecated.Returns a typed bag backed by the given bag.static SortedBag
typedSortedBag(SortedBag bag, java.lang.Class type)
Deprecated.Returns a typed sorted bag backed by the given bag.static Bag
unmodifiableBag(Bag bag)
Deprecated.Returns an unmodifiable view of the given bag.static SortedBag
unmodifiableSortedBag(SortedBag bag)
Deprecated.Returns an unmodifiable view of the given sorted bag.
-
-
-
Method Detail
-
synchronizedBag
public static Bag synchronizedBag(Bag bag)
Deprecated.Returns a synchronized (thread-safe) bag backed by the given bag. In order to guarantee serial access, it is critical that all access to the backing bag is accomplished through the returned 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.- Parameters:
bag
- the bag to synchronize, must not be null- Returns:
- a synchronized bag backed by that bag
- Throws:
java.lang.IllegalArgumentException
- if the Bag is null
-
unmodifiableBag
public static Bag unmodifiableBag(Bag bag)
Deprecated.Returns an unmodifiable view of the given bag. Any modification attempts to the returned bag will raise anUnsupportedOperationException
.- Parameters:
bag
- the bag whose unmodifiable view is to be returned, must not be null- Returns:
- an unmodifiable view of that bag
- Throws:
java.lang.IllegalArgumentException
- if the Bag is null
-
predicatedBag
public static Bag predicatedBag(Bag bag, Predicate predicate)
Deprecated.Returns a predicated (validating) bag backed by the given bag.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.
- Parameters:
bag
- the bag to predicate, must not be nullpredicate
- the predicate for the bag, must not be null- Returns:
- a predicated bag backed by the given bag
- Throws:
java.lang.IllegalArgumentException
- if the Bag or Predicate is null
-
typedBag
public static Bag typedBag(Bag bag, java.lang.Class type)
Deprecated.Returns a typed bag backed by the given bag.Only objects of the specified type can be added to the bag.
- Parameters:
bag
- the bag to limit to a specific type, must not be nulltype
- the type of objects which may be added to the bag- Returns:
- a typed bag backed by the specified bag
-
transformedBag
public static Bag transformedBag(Bag bag, Transformer transformer)
Deprecated.Returns a transformed bag backed by the given bag.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.
- Parameters:
bag
- the bag to predicate, must not be nulltransformer
- the transformer for the bag, must not be null- Returns:
- a transformed bag backed by the given bag
- Throws:
java.lang.IllegalArgumentException
- if the Bag or Transformer is null
-
synchronizedSortedBag
public static SortedBag synchronizedSortedBag(SortedBag bag)
Deprecated.Returns a synchronized (thread-safe) sorted bag backed by the given sorted bag. In order to guarantee serial access, it is critical that all access to the backing bag is accomplished through the returned 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.- Parameters:
bag
- the bag to synchronize, must not be null- Returns:
- a synchronized bag backed by that bag
- Throws:
java.lang.IllegalArgumentException
- if the SortedBag is null
-
unmodifiableSortedBag
public static SortedBag unmodifiableSortedBag(SortedBag bag)
Deprecated.Returns an unmodifiable view of the given sorted bag. Any modification attempts to the returned bag will raise anUnsupportedOperationException
.- Parameters:
bag
- the bag whose unmodifiable view is to be returned, must not be null- Returns:
- an unmodifiable view of that bag
- Throws:
java.lang.IllegalArgumentException
- if the SortedBag is null
-
predicatedSortedBag
public static SortedBag predicatedSortedBag(SortedBag bag, Predicate predicate)
Deprecated.Returns a predicated (validating) sorted bag backed by the given sorted bag.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.
- Parameters:
bag
- the sorted bag to predicate, must not be nullpredicate
- the predicate for the bag, must not be null- Returns:
- a predicated bag backed by the given bag
- Throws:
java.lang.IllegalArgumentException
- if the SortedBag or Predicate is null
-
typedSortedBag
public static SortedBag typedSortedBag(SortedBag bag, java.lang.Class type)
Deprecated.Returns a typed sorted bag backed by the given bag.Only objects of the specified type can be added to the bag.
- Parameters:
bag
- the bag to limit to a specific type, must not be nulltype
- the type of objects which may be added to the bag- Returns:
- a typed bag backed by the specified bag
-
transformedSortedBag
public static SortedBag transformedSortedBag(SortedBag bag, Transformer transformer)
Deprecated.Returns a transformed sorted bag backed by the given bag.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.
- Parameters:
bag
- the bag to predicate, must not be nulltransformer
- the transformer for the bag, must not be null- Returns:
- a transformed bag backed by the given bag
- Throws:
java.lang.IllegalArgumentException
- if the Bag or Transformer is null
-
-