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