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 */
017package org.apache.commons.collections4.collection;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.List;
022
023import org.apache.commons.collections4.Transformer;
024
025/**
026 * Decorates another {@link Collection} to transform objects that are added.
027 * <p>
028 * The add methods are affected by this class.
029 * Thus objects must be removed or searched for using their transformed form.
030 * For example, if the transformation converts Strings to Integers, you must
031 * use the Integer form to remove objects.
032 * <p>
033 * This class is Serializable from Commons Collections 3.1.
034 *
035 * @param <E> the type of the elements in the collection
036 * @since 3.0
037 * @version $Id: TransformedCollection.html 887892 2013-11-24 13:43:45Z tn $
038 */
039public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
040
041    /** Serialization version */
042    private static final long serialVersionUID = 8692300188161871514L;
043
044    /** The transformer to use */
045    protected final Transformer<? super E, ? extends E> transformer;
046
047    /**
048     * Factory method to create a transforming collection.
049     * <p>
050     * If there are any elements already in the collection being decorated, they
051     * are NOT transformed.
052     * Contrast this with {@link #transformedCollection(Collection, Transformer)}.
053     *
054     * @param <E> the type of the elements in the collection
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     * @since 4.0
060     */
061    public static <E> TransformedCollection<E> transformingCollection(final Collection<E> coll,
062            final Transformer<? super E, ? extends E> transformer) {
063        return new TransformedCollection<E>(coll, transformer);
064    }
065
066    /**
067     * Factory method to create a transforming collection that will transform
068     * existing contents of the specified collection.
069     * <p>
070     * If there are any elements already in the collection being decorated, they
071     * will be transformed by this method.
072     * Contrast this with {@link #transformingCollection(Collection, Transformer)}.
073     *
074     * @param <E> the type of the elements in the collection
075     * @param collection  the collection to decorate, must not be null
076     * @param transformer  the transformer to use for conversion, must not be null
077     * @return a new transformed Collection
078     * @throws IllegalArgumentException if collection or transformer is null
079     * @since 4.0
080     */
081    public static <E> TransformedCollection<E> transformedCollection(final Collection<E> collection,
082            final Transformer<? super E, ? extends E> transformer) {
083
084        final TransformedCollection<E> decorated = new TransformedCollection<E>(collection, transformer);
085        // null collection & transformer are disallowed by the constructor call above
086        if (collection.size() > 0) {
087            @SuppressWarnings("unchecked") // collection is of type E
088            final E[] values = (E[]) collection.toArray(); // NOPMD - false positive for generics
089            collection.clear();
090            for (final E value : values) {
091                decorated.decorated().add(transformer.transform(value));
092            }
093        }
094        return decorated;
095    }
096
097    //-----------------------------------------------------------------------
098    /**
099     * Constructor that wraps (not copies).
100     * <p>
101     * If there are any elements already in the collection being decorated, they
102     * are NOT transformed.
103     *
104     * @param coll  the collection to decorate, must not be null
105     * @param transformer  the transformer to use for conversion, must not be null
106     * @throws IllegalArgumentException if collection or transformer is null
107     */
108    protected TransformedCollection(final Collection<E> coll, final Transformer<? super E, ? extends E> transformer) {
109        super(coll);
110        if (transformer == null) {
111            throw new IllegalArgumentException("Transformer must not be null");
112        }
113        this.transformer = transformer;
114    }
115
116    /**
117     * Transforms an object.
118     * <p>
119     * The transformer itself may throw an exception if necessary.
120     *
121     * @param object  the object to transform
122     * @return a transformed object
123     */
124    protected E transform(final E object) {
125        return transformer.transform(object);
126    }
127
128    /**
129     * Transforms a collection.
130     * <p>
131     * The transformer itself may throw an exception if necessary.
132     *
133     * @param coll  the collection to transform
134     * @return a transformed object
135     */
136    protected Collection<E> transform(final Collection<? extends E> coll) {
137        final List<E> list = new ArrayList<E>(coll.size());
138        for (final E item : coll) {
139            list.add(transform(item));
140        }
141        return list;
142    }
143
144    //-----------------------------------------------------------------------
145    @Override
146    public boolean add(final E object) {
147        return decorated().add(transform(object));
148    }
149
150    @Override
151    public boolean addAll(final Collection<? extends E> coll) {
152        return decorated().addAll(transform(coll));
153    }
154
155}