KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bag > PredicatedBag


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.bag;
17
18 import java.util.Set JavaDoc;
19
20 import org.apache.commons.collections.Bag;
21 import org.apache.commons.collections.Predicate;
22 import org.apache.commons.collections.collection.PredicatedCollection;
23
24 /**
25  * Decorates another <code>Bag</code> to validate that additions
26  * match a specified predicate.
27  * <p>
28  * This bag exists to provide validation for the decorated bag.
29  * It is normally created to decorate an empty bag.
30  * If an object cannot be added to the bag, an IllegalArgumentException is thrown.
31  * <p>
32  * One usage would be to ensure that no null entries are added to the bag.
33  * <pre>Bag bag = PredicatedBag.decorate(new HashBag(), NotNullPredicate.INSTANCE);</pre>
34  * <p>
35  * This class is Serializable from Commons Collections 3.1.
36  *
37  * @since Commons Collections 3.0
38  * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:12 $
39  *
40  * @author Stephen Colebourne
41  * @author Paul Jack
42  */

43 public class PredicatedBag
44         extends PredicatedCollection implements Bag {
45
46     /** Serialization version */
47     private static final long serialVersionUID = -2575833140344736876L;
48
49     /**
50      * Factory method to create a predicated (validating) bag.
51      * <p>
52      * If there are any elements already in the bag being decorated, they
53      * are validated.
54      *
55      * @param bag the bag to decorate, must not be null
56      * @param predicate the predicate to use for validation, must not be null
57      * @return a new predicated Bag
58      * @throws IllegalArgumentException if bag or predicate is null
59      * @throws IllegalArgumentException if the bag contains invalid elements
60      */

61     public static Bag decorate(Bag bag, Predicate predicate) {
62         return new PredicatedBag(bag, predicate);
63     }
64
65     //-----------------------------------------------------------------------
66
/**
67      * Constructor that wraps (not copies).
68      * <p>
69      * If there are any elements already in the bag being decorated, they
70      * are validated.
71      *
72      * @param bag the bag to decorate, must not be null
73      * @param predicate the predicate to use for validation, must not be null
74      * @throws IllegalArgumentException if bag or predicate is null
75      * @throws IllegalArgumentException if the bag contains invalid elements
76      */

77     protected PredicatedBag(Bag bag, Predicate predicate) {
78         super(bag, predicate);
79     }
80
81     /**
82      * Gets the decorated bag.
83      *
84      * @return the decorated bag
85      */

86     protected Bag getBag() {
87         return (Bag) getCollection();
88     }
89     
90     //-----------------------------------------------------------------------
91
public boolean add(Object JavaDoc object, int count) {
92         validate(object);
93         return getBag().add(object, count);
94     }
95
96     public boolean remove(Object JavaDoc object, int count) {
97         return getBag().remove(object, count);
98     }
99
100     public Set JavaDoc uniqueSet() {
101         return getBag().uniqueSet();
102     }
103
104     public int getCount(Object JavaDoc object) {
105         return getBag().getCount(object);
106     }
107
108 }
109
Popular Tags