KickJava   Java API By Example, From Geeks To Geeks.

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


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.Transformer;
22 import org.apache.commons.collections.collection.TransformedCollection;
23 import org.apache.commons.collections.set.TransformedSet;
24
25 /**
26  * Decorates another <code>Bag</code> to transform objects that are added.
27  * <p>
28  * The add methods are affected by this class.
29  * Thus objects must be removed or searched for using their transformed form.
30  * For example, if the transformation converts Strings to Integers, you must
31  * use the Integer form to remove objects.
32  * <p>
33  * This class is Serializable from Commons Collections 3.1.
34  *
35  * @since Commons Collections 3.0
36  * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:12 $
37  *
38  * @author Stephen Colebourne
39  */

40 public class TransformedBag
41         extends TransformedCollection implements Bag {
42
43     /** Serialization version */
44     private static final long serialVersionUID = 5421170911299074185L;
45
46     /**
47      * Factory method to create a transforming bag.
48      * <p>
49      * If there are any elements already in the bag being decorated, they
50      * are NOT transformed.
51      *
52      * @param bag the bag to decorate, must not be null
53      * @param transformer the transformer to use for conversion, must not be null
54      * @return a new transformed Bag
55      * @throws IllegalArgumentException if bag or transformer is null
56      */

57     public static Bag decorate(Bag bag, Transformer transformer) {
58         return new TransformedBag(bag, transformer);
59     }
60     
61     //-----------------------------------------------------------------------
62
/**
63      * Constructor that wraps (not copies).
64      * <p>
65      * If there are any elements already in the bag being decorated, they
66      * are NOT transformed.
67      *
68      * @param bag the bag to decorate, must not be null
69      * @param transformer the transformer to use for conversion, must not be null
70      * @throws IllegalArgumentException if bag or transformer is null
71      */

72     protected TransformedBag(Bag bag, Transformer transformer) {
73         super(bag, transformer);
74     }
75
76     /**
77      * Gets the decorated bag.
78      *
79      * @return the decorated bag
80      */

81     protected Bag getBag() {
82         return (Bag) collection;
83     }
84
85     //-----------------------------------------------------------------------
86
public int getCount(Object JavaDoc object) {
87         return getBag().getCount(object);
88     }
89
90     public boolean remove(Object JavaDoc object, int nCopies) {
91         return getBag().remove(object, nCopies);
92     }
93
94     //-----------------------------------------------------------------------
95
public boolean add(Object JavaDoc object, int nCopies) {
96         object = transform(object);
97         return getBag().add(object, nCopies);
98     }
99
100     public Set JavaDoc uniqueSet() {
101         Set JavaDoc set = getBag().uniqueSet();
102         return TransformedSet.decorate(set, transformer);
103     }
104
105 }
106
Popular Tags