KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > collection > TransformedCollection


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.collection;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.collections.Transformer;
24
25 /**
26  * Decorates another <code>Collection</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:13 $
37  *
38  * @author Stephen Colebourne
39  */

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

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

74     protected TransformedCollection(Collection JavaDoc coll, Transformer transformer) {
75         super(coll);
76         if (transformer == null) {
77             throw new IllegalArgumentException JavaDoc("Transformer must not be null");
78         }
79         this.transformer = transformer;
80     }
81
82     /**
83      * Transforms an object.
84      * <p>
85      * The transformer itself may throw an exception if necessary.
86      *
87      * @param object the object to transform
88      * @return a transformed object
89      */

90     protected Object JavaDoc transform(Object JavaDoc object) {
91         return transformer.transform(object);
92     }
93
94     /**
95      * Transforms a collection.
96      * <p>
97      * The transformer itself may throw an exception if necessary.
98      *
99      * @param coll the collection to transform
100      * @return a transformed object
101      */

102     protected Collection JavaDoc transform(Collection JavaDoc coll) {
103         List JavaDoc list = new ArrayList JavaDoc(coll.size());
104         for (Iterator JavaDoc it = coll.iterator(); it.hasNext(); ) {
105             list.add(transform(it.next()));
106         }
107         return list;
108     }
109
110     //-----------------------------------------------------------------------
111
public boolean add(Object JavaDoc object) {
112         object = transform(object);
113         return getCollection().add(object);
114     }
115
116     public boolean addAll(Collection JavaDoc coll) {
117         coll = transform(coll);
118         return getCollection().addAll(coll);
119     }
120
121 }
122
Popular Tags