    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.Comparator;
    020import java.util.SortedSet;
    021
    022import org.apache.commons.collections4.Transformer;
    023
    024/**
    025 * Decorates another {@code SortedSet} 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 TransformedSortedSet<E> extends TransformedSet<E> implements SortedSet<E> {
    040
    041    /** Serialization version */
    042    private static final long serialVersionUID = -1675486811351124386L;
    043
    044    /**
    045     * Factory method to create a transforming sorted set that will transform
    046     * existing contents of the specified sorted 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 #transformingSortedSet(SortedSet, 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 {@link SortedSet}
    056     * @throws NullPointerException if set or transformer is null
    057     * @since 4.0
    058     */
    059    public static <E> TransformedSortedSet<E> transformedSortedSet(final SortedSet<E> set,
    060            final Transformer<? super E, ? extends E> transformer) {
    061
    062        final TransformedSortedSet<E> decorated = new TransformedSortedSet<>(set, transformer);
    063        if (!set.isEmpty()) {
    064            @SuppressWarnings("unchecked") // set is type E
    065            final E[] values = (E[]) set.toArray(); // NOPMD - false positive for generics
    066            set.clear();
    067            for (final E value : values) {
    068                decorated.decorated().add(transformer.apply(value));
    069            }
    070        }
    071        return decorated;
    072    }
    073
    074    /**
    075     * Factory method to create a transforming sorted set.
    076     * <p>
    077     * If there are any elements already in the set being decorated, they
    078     * are NOT transformed.
    079     * Contrast this with {@link #transformedSortedSet(SortedSet, Transformer)}.
    080     *
    081     * @param <E> The element type
    082     * @param set  The set to decorate, must not be null
    083     * @param transformer  The transformer to use for conversion, must not be null
    084     * @return A new transformed {@link SortedSet}
    085     * @throws NullPointerException if set or transformer is null
    086     * @since 4.0
    087     */
    088    public static <E> TransformedSortedSet<E> transformingSortedSet(final SortedSet<E> set,
    089            final Transformer<? super E, ? extends E> transformer) {
    090        return new TransformedSortedSet<>(set, transformer);
    091    }
    092
    093    /**
    094     * Constructor that wraps (not copies).
    095     * <p>
    096     * If there are any elements already in the set being decorated, they
    097     * are NOT transformed.
    098     *
    099     * @param set  The set to decorate, must not be null
    100     * @param transformer  The transformer to use for conversion, must not be null
    101     * @throws NullPointerException if set or transformer is null
    102     */
    103    protected TransformedSortedSet(final SortedSet<E> set, final Transformer<? super E, ? extends E> transformer) {
    104        super(set, transformer);
    105    }
    106
    107    @Override
    108    public Comparator<? super E> comparator() {
    109        return getSortedSet().comparator();
    110    }
    111
    112    @Override
    113    public E first() {
    114        return getSortedSet().first();
    115    }
    116
    117    /**
    118     * Gets the decorated set.
    119     *
    120     * @return The decorated set
    121     */
    122    protected SortedSet<E> getSortedSet() {
    123        return (SortedSet<E>) decorated();
    124    }
    125
    126    @Override
    127    public SortedSet<E> headSet(final E toElement) {
    128        final SortedSet<E> set = getSortedSet().headSet(toElement);
    129        return new TransformedSortedSet<>(set, transformer);
    130    }
    131
    132    @Override
    133    public E last() {
    134        return getSortedSet().last();
    135    }
    136
    137    @Override
    138    public SortedSet<E> subSet(final E fromElement, final E toElement) {
    139        final SortedSet<E> set = getSortedSet().subSet(fromElement, toElement);
    140        return new TransformedSortedSet<>(set, transformer);
    141    }
    142
    143    @Override
    144    public SortedSet<E> tailSet(final E fromElement) {
    145        final SortedSet<E> set = getSortedSet().tailSet(fromElement);
    146        return new TransformedSortedSet<>(set, transformer);
    147    }
    148
    149}