    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.set;
    018
    019import java.util.Set;
    020
    021import org.apache.commons.collections4.Transformer;
    022import org.apache.commons.collections4.collection.TransformedCollection;
    023
    024/**
    025 * Decorates another {@code Set} to transform objects that are added.
    026 * <p>
    027 * The add methods are affected by this class.
    028 * Thus objects must be removed or searched for using their transformed form.
    029 * For example, if the transformation converts Strings to Integers, you must
    030 * use the Integer form to remove objects.
    031 * </p>
    032 * <p>
    033 * This class is Serializable from Commons Collections 3.1.
    034 * </p>
    035 *
    036 * @param <E> The type of the elements in this set
    037 * @since 3.0
    038 */
    039public class TransformedSet<E> extends TransformedCollection<E> implements Set<E> {
    040
    041    /** Serialization version */
    042    private static final long serialVersionUID = 306127383500410386L;
    043
    044    /**
    045     * Factory method to create a transforming set that will transform
    046     * existing contents of the specified set.
    047     * <p>
    048     * If there are any elements already in the set being decorated, they
    049     * will be transformed by this method.
    050     * Contrast this with {@link #transformingSet(Set, Transformer)}.
    051     *
    052     * @param <E> The element type
    053     * @param set  The set to decorate, must not be null
    054     * @param transformer  The transformer to use for conversion, must not be null
    055     * @return A new transformed set
    056     * @throws NullPointerException if set or transformer is null
    057     * @since 4.0
    058     */
    059    public static <E> Set<E> transformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) {
    060        final TransformedSet<E> decorated = new TransformedSet<>(set, transformer);
    061        if (!set.isEmpty()) {
    062            @SuppressWarnings("unchecked") // set is type E
    063            final E[] values = (E[]) set.toArray(); // NOPMD - false positive for generics
    064            set.clear();
    065            for (final E value : values) {
    066                decorated.decorated().add(transformer.apply(value));
    067            }
    068        }
    069        return decorated;
    070    }
    071
    072    /**
    073     * Factory method to create a transforming set.
    074     * <p>
    075     * If there are any elements already in the set being decorated, they
    076     * are NOT transformed.
    077     * Contrast this with {@link #transformedSet(Set, Transformer)}.
    078     *
    079     * @param <E> The element type
    080     * @param set  The set to decorate, must not be null
    081     * @param transformer  The transformer to use for conversion, must not be null
    082     * @return A new transformed set
    083     * @throws NullPointerException if set or transformer is null
    084     * @since 4.0
    085     */
    086    public static <E> TransformedSet<E> transformingSet(final Set<E> set,
    087            final Transformer<? super E, ? extends E> transformer) {
    088        return new TransformedSet<>(set, transformer);
    089    }
    090
    091    /**
    092     * Constructor that wraps (not copies).
    093     * <p>
    094     * If there are any elements already in the set being decorated, they
    095     * are NOT transformed.
    096     *
    097     * @param set  The set to decorate, must not be null
    098     * @param transformer  The transformer to use for conversion, must not be null
    099     * @throws NullPointerException if set or transformer is null
    100     */
    101    protected TransformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) {
    102        super(set, transformer);
    103    }
    104
    105    @Override
    106    public boolean equals(final Object object) {
    107        return object == this || decorated().equals(object);
    108    }
    109
    110    @Override
    111    public int hashCode() {
    112        return decorated().hashCode();
    113    }
    114
    115}