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 *      https://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     * Creates 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     * </p>
054     *
055     * @param <E> The type of the elements in the collection.
056     * @param collection  The collection to decorate, must not be null.
057     * @param transformer  The transformer to use for conversion, must not be null.
058     * @return A new transformed Collection.
059     * @throws NullPointerException if collection or transformer is null.
060     * @since 4.0
061     */
062    public static <E> TransformedCollection<E> transformedCollection(final Collection<E> collection,
063            final Transformer<? super E, ? extends E> transformer) {
064
065        final TransformedCollection<E> decorated = new TransformedCollection<>(collection, transformer);
066        // null collection & transformer are disallowed by the constructor call above
067        if (!collection.isEmpty()) {
068            @SuppressWarnings("unchecked") // collection is of type E
069            final E[] values = (E[]) collection.toArray(); // NOPMD - false positive for generics
070            collection.clear();
071            for (final E value : values) {
072                decorated.decorated().add(transformer.apply(value));
073            }
074        }
075        return decorated;
076    }
077
078    /**
079     * Creates a transforming collection.
080     * <p>
081     * If there are any elements already in the collection being decorated, they
082     * are NOT transformed.
083     * Contrast this with {@link #transformedCollection(Collection, Transformer)}.
084     * </p>
085     *
086     * @param <E> The type of the elements in the collection.
087     * @param coll  The collection to decorate, must not be null.
088     * @param transformer  The transformer to use for conversion, must not be null.
089     * @return A new transformed collection.
090     * @throws NullPointerException if collection or transformer is null.
091     * @since 4.0
092     */
093    public static <E> TransformedCollection<E> transformingCollection(final Collection<E> coll,
094            final Transformer<? super E, ? extends E> transformer) {
095        return new TransformedCollection<>(coll, transformer);
096    }
097
098    /** The transformer to use */
099    protected final Transformer<? super E, ? extends E> transformer;
100
101    /**
102     * Constructs and wraps (not copies).
103     * <p>
104     * If there are any elements already in the collection being decorated, they
105     * are NOT transformed.
106     * </p>
107     *
108     * @param collection  The collection to decorate, must not be null.
109     * @param transformer  The transformer to use for conversion, must not be null.
110     * @throws NullPointerException if collection or transformer is null.
111     */
112    protected TransformedCollection(final Collection<E> collection, final Transformer<? super E, ? extends E> transformer) {
113        super(collection);
114        this.transformer = Objects.requireNonNull(transformer, "transformer");
115    }
116
117    @Override
118    public boolean add(final E object) {
119        return decorated().add(transform(object));
120    }
121
122    @Override
123    public boolean addAll(final Collection<? extends E> coll) {
124        return decorated().addAll(transform(coll));
125    }
126
127    /**
128     * Transforms a collection.
129     * <p>
130     * The transformer itself may throw an exception if necessary.
131     * </p>
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<>(coll.size());
138        for (final E item : coll) {
139            list.add(transform(item));
140        }
141        return list;
142    }
143
144    /**
145     * Transforms an object.
146     * <p>
147     * The transformer itself may throw an exception if necessary.
148     * </p>
149     *
150     * @param object  The object to transform.
151     * @return A transformed object.
152     */
153    protected E transform(final E object) {
154        return transformer.apply(object);
155    }
156
157}