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