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 * @since 3.0
035 * @version $Id: TransformedSortedSet.html 972421 2015-11-14 20:00:04Z tn $
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 IllegalArgumentException 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<E>(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 IllegalArgumentException 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<E>(set, transformer);
080        if (transformer != null && set != null && 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 IllegalArgumentException 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    public E first() {
117        return getSortedSet().first();
118    }
119
120    public E last() {
121        return getSortedSet().last();
122    }
123
124    public Comparator<? super E> comparator() {
125        return getSortedSet().comparator();
126    }
127
128    //-----------------------------------------------------------------------
129    public SortedSet<E> subSet(final E fromElement, final E toElement) {
130        final SortedSet<E> set = getSortedSet().subSet(fromElement, toElement);
131        return new TransformedSortedSet<E>(set, transformer);
132    }
133
134    public SortedSet<E> headSet(final E toElement) {
135        final SortedSet<E> set = getSortedSet().headSet(toElement);
136        return new TransformedSortedSet<E>(set, transformer);
137    }
138
139    public SortedSet<E> tailSet(final E fromElement) {
140        final SortedSet<E> set = getSortedSet().tailSet(fromElement);
141        return new TransformedSortedSet<E>(set, transformer);
142    }
143
144}