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