001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.commons.collections.collection;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import org.apache.commons.collections.Transformer;
025    
026    /**
027     * Decorates another <code>Collection</code> to transform objects that are added.
028     * <p>
029     * The add methods are affected by this class.
030     * Thus objects must be removed or searched for using their transformed form.
031     * For example, if the transformation converts Strings to Integers, you must
032     * use the Integer form to remove objects.
033     * <p>
034     * This class is Serializable from Commons Collections 3.1.
035     *
036     * @since Commons Collections 3.0
037     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
038     * 
039     * @author Stephen Colebourne
040     */
041    public class TransformedCollection extends AbstractSerializableCollectionDecorator {
042    
043        /** Serialization version */
044        private static final long serialVersionUID = 8692300188161871514L;
045    
046        /** The transformer to use */
047        protected final Transformer transformer;
048    
049        /**
050         * Factory method to create a transforming collection.
051         * <p>
052         * If there are any elements already in the collection being decorated, they
053         * are NOT transformed.
054         * 
055         * @param coll  the collection to decorate, must not be null
056         * @param transformer  the transformer to use for conversion, must not be null
057         * @return a new transformed collection
058         * @throws IllegalArgumentException if collection or transformer is null
059         */
060        public static Collection decorate(Collection coll, Transformer transformer) {
061            return new TransformedCollection(coll, transformer);
062        }
063        
064        //-----------------------------------------------------------------------
065        /**
066         * Constructor that wraps (not copies).
067         * <p>
068         * If there are any elements already in the collection being decorated, they
069         * are NOT transformed.
070         * 
071         * @param coll  the collection to decorate, must not be null
072         * @param transformer  the transformer to use for conversion, must not be null
073         * @throws IllegalArgumentException if collection or transformer is null
074         */
075        protected TransformedCollection(Collection coll, Transformer transformer) {
076            super(coll);
077            if (transformer == null) {
078                throw new IllegalArgumentException("Transformer must not be null");
079            }
080            this.transformer = transformer;
081        }
082    
083        /**
084         * Transforms an object.
085         * <p>
086         * The transformer itself may throw an exception if necessary.
087         * 
088         * @param object  the object to transform
089         * @return a transformed object
090         */
091        protected Object transform(Object object) {
092            return transformer.transform(object);
093        }
094    
095        /**
096         * Transforms a collection.
097         * <p>
098         * The transformer itself may throw an exception if necessary.
099         * 
100         * @param coll  the collection to transform
101         * @return a transformed object
102         */
103        protected Collection transform(Collection coll) {
104            List list = new ArrayList(coll.size());
105            for (Iterator it = coll.iterator(); it.hasNext(); ) {
106                list.add(transform(it.next()));
107            }
108            return list;
109        }
110    
111        //-----------------------------------------------------------------------
112        public boolean add(Object object) {
113            object = transform(object);
114            return getCollection().add(object);
115        }
116    
117        public boolean addAll(Collection coll) {
118            coll = transform(coll);
119            return getCollection().addAll(coll);
120        }
121    
122    }