1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4;
import org.apache.commons.collections4.bag.CollectionBag;
import org.apache.commons.collections4.bag.HashBag;
import org.apache.commons.collections4.bag.PredicatedBag;
import org.apache.commons.collections4.bag.PredicatedSortedBag;
import org.apache.commons.collections4.bag.SynchronizedBag;
import org.apache.commons.collections4.bag.SynchronizedSortedBag;
import org.apache.commons.collections4.bag.TransformedBag;
import org.apache.commons.collections4.bag.TransformedSortedBag;
import org.apache.commons.collections4.bag.TreeBag;
import org.apache.commons.collections4.bag.UnmodifiableBag;
import org.apache.commons.collections4.bag.UnmodifiableSortedBag;
/**
* Provides utility methods and decorators for {@link Bag} and {@link SortedBag} instances.
*
* @since 2.1
* @version $Id: BagUtils.java 1543964 2013-11-20 21:53:39Z tn $
*/
public class BagUtils {
/**
* An empty unmodifiable bag.
*/
@SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type
public static final Bag EMPTY_BAG = UnmodifiableBag.unmodifiableBag(new HashBag<Object>());
/**
* An empty unmodifiable sorted bag.
*/
@SuppressWarnings("rawtypes") // OK, empty bag is compatible with any type
public static final Bag EMPTY_SORTED_BAG =
UnmodifiableSortedBag.unmodifiableSortedBag(new TreeBag<Object>());
/**
* Instantiation of BagUtils is not intended or required.
*/
private BagUtils() {}
//-----------------------------------------------------------------------
/**
* 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.
* <p>
* It is imperative that the user manually synchronize on the returned bag
* when iterating over it:
*
* <pre>
* Bag bag = BagUtils.synchronizedBag(new HashBag());
* ...
* synchronized(bag) {
* Iterator i = bag.iterator(); // Must be in synchronized block
* while (i.hasNext())
* foo(i.next());
* }
* }
* </pre>
*
* Failure to follow this advice may result in non-deterministic behavior.
*
* @param <E> the element type
* @param bag the bag to synchronize, must not be null
* @return a synchronized bag backed by that bag
* @throws IllegalArgumentException if the Bag is null
*/
public static <E> Bag<E> synchronizedBag(final Bag<E> bag) {
return SynchronizedBag.synchronizedBag(bag);
}
/**
* Returns an unmodifiable view of the given bag. Any modification attempts
* to the returned bag will raise an {@link UnsupportedOperationException}.
*
* @param <E> the element type
* @param bag the bag whose unmodifiable view is to be returned, must not be null
* @return an unmodifiable view of that bag
* @throws IllegalArgumentException if the Bag is null
*/
public static <E> Bag<E> unmodifiableBag(final Bag<? extends E> bag) {
return UnmodifiableBag.unmodifiableBag(bag);
}
/**
* Returns a predicated (validating) bag backed by the given bag.
* <p>
* 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.
*
* @param <E> the element type
* @param bag the bag to predicate, must not be null
* @param predicate the predicate for the bag, must not be null
* @return a predicated bag backed by the given bag
* @throws IllegalArgumentException if the Bag or Predicate is null
*/
public static <E> Bag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) {
return PredicatedBag.predicatedBag(bag, predicate);
}
/**
* Returns a transformed bag backed by the given bag.
* <p>
* 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.
* <p>
* Existing entries in the specified bag will not be transformed.
* If you want that behaviour, see {@link TransformedBag#transformedBag(Bag, Transformer)}.
*
* @param <E> the element type
* @param bag the bag to predicate, must not be null
* @param transformer the transformer for the bag, must not be null
* @return a transformed bag backed by the given bag
* @throws IllegalArgumentException if the Bag or Transformer is null
*/
public static <E> Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) {
return TransformedBag.transformingBag(bag, transformer);
}
/**
* Returns a bag that complies to the Collection contract, backed by the given bag.
*
* @param <E> the element type
* @param bag the bag to decorate, must not be null
* @return a Bag that complies to the Collection contract
* @throws IllegalArgumentException if bag is null
* @since 4.0
*/
public static <E> Bag<E> collectionBag(final Bag<E> bag) {
return CollectionBag.collectionBag(bag);
}
//-----------------------------------------------------------------------
/**
* 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.
* <p>
* It is imperative that the user manually synchronize on the returned bag
* when iterating over it:
*
* <pre>
* SortedBag bag = BagUtils.synchronizedSortedBag(new TreeBag());
* ...
* synchronized(bag) {
* Iterator i = bag.iterator(); // Must be in synchronized block
* while (i.hasNext())
* foo(i.next());
* }
* }
* </pre>
*
* Failure to follow this advice may result in non-deterministic behavior.
*
* @param <E> the element type
* @param bag the bag to synchronize, must not be null
* @return a synchronized bag backed by that bag
* @throws IllegalArgumentException if the SortedBag is null
*/
public static <E> SortedBag<E> synchronizedSortedBag(final SortedBag<E> bag) {
return SynchronizedSortedBag.synchronizedSortedBag(bag);
}
/**
* Returns an unmodifiable view of the given sorted bag. Any modification
* attempts to the returned bag will raise an
* {@link UnsupportedOperationException}.
*
* @param <E> the element type
* @param bag the bag whose unmodifiable view is to be returned, must not be null
* @return an unmodifiable view of that bag
* @throws IllegalArgumentException if the SortedBag is null
*/
public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag) {
return UnmodifiableSortedBag.unmodifiableSortedBag(bag);
}
/**
* Returns a predicated (validating) sorted bag backed by the given sorted
* bag.
* <p>
* 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.
*
* @param <E> the element type
* @param bag the sorted bag to predicate, must not be null
* @param predicate the predicate for the bag, must not be null
* @return a predicated bag backed by the given bag
* @throws IllegalArgumentException if the SortedBag or Predicate is null
*/
public static <E> SortedBag<E> predicatedSortedBag(final SortedBag<E> bag,
final Predicate<? super E> predicate) {
return PredicatedSortedBag.predicatedSortedBag(bag, predicate);
}
/**
* Returns a transformed sorted bag backed by the given bag.
* <p>
* 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.
* <p>
* Existing entries in the specified bag will not be transformed.
* If you want that behaviour, see
* {@link TransformedSortedBag#transformedSortedBag(SortedBag, Transformer)}.
*
* @param <E> the element type
* @param bag the bag to predicate, must not be null
* @param transformer the transformer for the bag, must not be null
* @return a transformed bag backed by the given bag
* @throws IllegalArgumentException if the Bag or Transformer is null
*/
public static <E> SortedBag<E> transformingSortedBag(final SortedBag<E> bag,
final Transformer<? super E, ? extends E> transformer) {
return TransformedSortedBag.transformingSortedBag(bag, transformer);
}
/**
* Get an empty <code>Bag</code>.
*
* @param <E> the element type
* @return an empty Bag
*/
@SuppressWarnings("unchecked") // OK, empty bag is compatible with any type
public static <E> Bag<E> emptyBag() {
return (Bag<E>) EMPTY_BAG;
}
/**
* Get an empty <code>SortedBag</code>.
*
* @param <E> the element type
* @return an empty sorted Bag
*/
@SuppressWarnings("unchecked") // OK, empty bag is compatible with any type
public static <E> SortedBag<E> emptySortedBag() {
return (SortedBag<E>) EMPTY_SORTED_BAG;
}
}
|