001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.bag;
017:
018: import java.util.Set;
019:
020: import org.apache.commons.collections.Bag;
021: import org.apache.commons.collections.Predicate;
022: import org.apache.commons.collections.collection.PredicatedCollection;
023:
024: /**
025: * Decorates another <code>Bag</code> to validate that additions
026: * match a specified predicate.
027: * <p>
028: * This bag exists to provide validation for the decorated bag.
029: * It is normally created to decorate an empty bag.
030: * If an object cannot be added to the bag, an IllegalArgumentException is thrown.
031: * <p>
032: * One usage would be to ensure that no null entries are added to the bag.
033: * <pre>Bag bag = PredicatedBag.decorate(new HashBag(), NotNullPredicate.INSTANCE);</pre>
034: * <p>
035: * This class is Serializable from Commons Collections 3.1.
036: *
037: * @since Commons Collections 3.0
038: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
039: *
040: * @author Stephen Colebourne
041: * @author Paul Jack
042: */
043: public class PredicatedBag extends PredicatedCollection implements Bag {
044:
045: /** Serialization version */
046: private static final long serialVersionUID = -2575833140344736876L;
047:
048: /**
049: * Factory method to create a predicated (validating) bag.
050: * <p>
051: * If there are any elements already in the bag being decorated, they
052: * are validated.
053: *
054: * @param bag the bag to decorate, must not be null
055: * @param predicate the predicate to use for validation, must not be null
056: * @return a new predicated Bag
057: * @throws IllegalArgumentException if bag or predicate is null
058: * @throws IllegalArgumentException if the bag contains invalid elements
059: */
060: public static Bag decorate(Bag bag, Predicate predicate) {
061: return new PredicatedBag(bag, predicate);
062: }
063:
064: //-----------------------------------------------------------------------
065: /**
066: * Constructor that wraps (not copies).
067: * <p>
068: * If there are any elements already in the bag being decorated, they
069: * are validated.
070: *
071: * @param bag the bag to decorate, must not be null
072: * @param predicate the predicate to use for validation, must not be null
073: * @throws IllegalArgumentException if bag or predicate is null
074: * @throws IllegalArgumentException if the bag contains invalid elements
075: */
076: protected PredicatedBag(Bag bag, Predicate predicate) {
077: super (bag, predicate);
078: }
079:
080: /**
081: * Gets the decorated bag.
082: *
083: * @return the decorated bag
084: */
085: protected Bag getBag() {
086: return (Bag) getCollection();
087: }
088:
089: //-----------------------------------------------------------------------
090: public boolean add(Object object, int count) {
091: validate(object);
092: return getBag().add(object, count);
093: }
094:
095: public boolean remove(Object object, int count) {
096: return getBag().remove(object, count);
097: }
098:
099: public Set uniqueSet() {
100: return getBag().uniqueSet();
101: }
102:
103: public int getCount(Object object) {
104: return getBag().getCount(object);
105: }
106:
107: }
|