    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.Iterator;
    020import java.util.NavigableSet;
    021
    022import org.apache.commons.collections4.Transformer;
    023
    024/**
    025 * Decorates another {@code NavigableSet} 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 *
    033 * @param <E> the type of the elements in this set
    034 * @since 4.1
    035 */
    036public class TransformedNavigableSet<E> extends TransformedSortedSet<E> implements NavigableSet<E> {
    037
    038    /** Serialization version */
    039    private static final long serialVersionUID = 20150528L;
    040
    041    /**
    042     * Factory method to create a transforming navigable set that will transform
    043     * existing contents of the specified navigable set.
    044     * <p>
    045     * If there are any elements already in the set being decorated, they
    046     * will be transformed by this method.
    047     * Contrast this with {@link #transformingNavigableSet(NavigableSet, 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 NavigableSet}
    053     * @throws NullPointerException if set or transformer is null
    054     */
    055    public static <E> TransformedNavigableSet<E> transformedNavigableSet(final NavigableSet<E> set,
    056            final Transformer<? super E, ? extends E> transformer) {
    057
    058        final TransformedNavigableSet<E> decorated = new TransformedNavigableSet<>(set, transformer);
    059        if (!set.isEmpty()) {
    060            @SuppressWarnings("unchecked") // set is type E
    061            final E[] values = (E[]) set.toArray(); // NOPMD - false positive for generics
    062            set.clear();
    063            for (final E value : values) {
    064                decorated.decorated().add(transformer.apply(value));
    065            }
    066        }
    067        return decorated;
    068    }
    069
    070    /**
    071     * Factory method to create a transforming navigable set.
    072     * <p>
    073     * If there are any elements already in the set being decorated, they
    074     * are NOT transformed.
    075     * Contrast this with {@link #transformedNavigableSet(NavigableSet, Transformer)}.
    076     *
    077     * @param <E> the element type
    078     * @param set  the set to decorate, must not be null
    079     * @param transformer  the transformer to use for conversion, must not be null
    080     * @return a new transformed {@link NavigableSet}
    081     * @throws NullPointerException if set or transformer is null
    082     */
    083    public static <E> TransformedNavigableSet<E> transformingNavigableSet(final NavigableSet<E> set,
    084            final Transformer<? super E, ? extends E> transformer) {
    085        return new TransformedNavigableSet<>(set, transformer);
    086    }
    087
    088    /**
    089     * Constructor that wraps (not copies).
    090     * <p>
    091     * If there are any elements already in the set being decorated, they
    092     * are NOT transformed.
    093     *
    094     * @param set  the set to decorate, must not be null
    095     * @param transformer  the transformer to use for conversion, must not be null
    096     * @throws NullPointerException if set or transformer is null
    097     */
    098    protected TransformedNavigableSet(final NavigableSet<E> set,
    099                                      final Transformer<? super E, ? extends E> transformer) {
    100        super(set, transformer);
    101    }
    102
    103    @Override
    104    public E ceiling(final E e) {
    105        return decorated().ceiling(e);
    106    }
    107
    108    /**
    109     * Gets the decorated navigable set.
    110     *
    111     * @return the decorated navigable set
    112     */
    113    @Override
    114    protected NavigableSet<E> decorated() {
    115        return (NavigableSet<E>) super.decorated();
    116    }
    117
    118    @Override
    119    public Iterator<E> descendingIterator() {
    120        return decorated().descendingIterator();
    121    }
    122
    123    @Override
    124    public NavigableSet<E> descendingSet() {
    125        return transformingNavigableSet(decorated().descendingSet(), transformer);
    126    }
    127
    128    @Override
    129    public E floor(final E e) {
    130        return decorated().floor(e);
    131    }
    132
    133    @Override
    134    public NavigableSet<E> headSet(final E toElement, final boolean inclusive) {
    135        final NavigableSet<E> head = decorated().headSet(toElement, inclusive);
    136        return transformingNavigableSet(head, transformer);
    137    }
    138
    139    @Override
    140    public E higher(final E e) {
    141        return decorated().higher(e);
    142    }
    143
    144    @Override
    145    public E lower(final E e) {
    146        return decorated().lower(e);
    147    }
    148
    149    @Override
    150    public E pollFirst() {
    151        return decorated().pollFirst();
    152    }
    153
    154    @Override
    155    public E pollLast() {
    156        return decorated().pollLast();
    157    }
    158
    159    @Override
    160    public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
    161            final boolean toInclusive) {
    162        final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive);
    163        return transformingNavigableSet(sub, transformer);
    164    }
    165
    166    @Override
    167    public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) {
    168        final NavigableSet<E> tail = decorated().tailSet(fromElement, inclusive);
    169        return transformingNavigableSet(tail, transformer);
    170    }
    171
    172}