TransformedNavigableSet.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.set;

  18. import java.util.Iterator;
  19. import java.util.NavigableSet;

  20. import org.apache.commons.collections4.Transformer;

  21. /**
  22.  * Decorates another {@code NavigableSet} to transform objects that are added.
  23.  * <p>
  24.  * The add methods are affected by this class.
  25.  * Thus objects must be removed or searched for using their transformed form.
  26.  * For example, if the transformation converts Strings to Integers, you must
  27.  * use the Integer form to remove objects.
  28.  * </p>
  29.  *
  30.  * @param <E> the type of the elements in this set
  31.  * @since 4.1
  32.  */
  33. public class TransformedNavigableSet<E> extends TransformedSortedSet<E> implements NavigableSet<E> {

  34.     /** Serialization version */
  35.     private static final long serialVersionUID = 20150528L;

  36.     /**
  37.      * Factory method to create a transforming navigable set that will transform
  38.      * existing contents of the specified navigable set.
  39.      * <p>
  40.      * If there are any elements already in the set being decorated, they
  41.      * will be transformed by this method.
  42.      * Contrast this with {@link #transformingNavigableSet(NavigableSet, Transformer)}.
  43.      *
  44.      * @param <E> the element type
  45.      * @param set  the set to decorate, must not be null
  46.      * @param transformer  the transformer to use for conversion, must not be null
  47.      * @return a new transformed {@link NavigableSet}
  48.      * @throws NullPointerException if set or transformer is null
  49.      */
  50.     public static <E> TransformedNavigableSet<E> transformedNavigableSet(final NavigableSet<E> set,
  51.             final Transformer<? super E, ? extends E> transformer) {

  52.         final TransformedNavigableSet<E> decorated = new TransformedNavigableSet<>(set, transformer);
  53.         if (!set.isEmpty()) {
  54.             @SuppressWarnings("unchecked") // set is type E
  55.             final E[] values = (E[]) set.toArray(); // NOPMD - false positive for generics
  56.             set.clear();
  57.             for (final E value : values) {
  58.                 decorated.decorated().add(transformer.apply(value));
  59.             }
  60.         }
  61.         return decorated;
  62.     }

  63.     /**
  64.      * Factory method to create a transforming navigable set.
  65.      * <p>
  66.      * If there are any elements already in the set being decorated, they
  67.      * are NOT transformed.
  68.      * Contrast this with {@link #transformedNavigableSet(NavigableSet, Transformer)}.
  69.      *
  70.      * @param <E> the element type
  71.      * @param set  the set to decorate, must not be null
  72.      * @param transformer  the transformer to use for conversion, must not be null
  73.      * @return a new transformed {@link NavigableSet}
  74.      * @throws NullPointerException if set or transformer is null
  75.      */
  76.     public static <E> TransformedNavigableSet<E> transformingNavigableSet(final NavigableSet<E> set,
  77.             final Transformer<? super E, ? extends E> transformer) {
  78.         return new TransformedNavigableSet<>(set, transformer);
  79.     }

  80.     /**
  81.      * Constructor that wraps (not copies).
  82.      * <p>
  83.      * If there are any elements already in the set being decorated, they
  84.      * are NOT transformed.
  85.      *
  86.      * @param set  the set to decorate, must not be null
  87.      * @param transformer  the transformer to use for conversion, must not be null
  88.      * @throws NullPointerException if set or transformer is null
  89.      */
  90.     protected TransformedNavigableSet(final NavigableSet<E> set,
  91.                                       final Transformer<? super E, ? extends E> transformer) {
  92.         super(set, transformer);
  93.     }

  94.     @Override
  95.     public E ceiling(final E e) {
  96.         return decorated().ceiling(e);
  97.     }

  98.     /**
  99.      * Gets the decorated navigable set.
  100.      *
  101.      * @return the decorated navigable set
  102.      */
  103.     @Override
  104.     protected NavigableSet<E> decorated() {
  105.         return (NavigableSet<E>) super.decorated();
  106.     }

  107.     @Override
  108.     public Iterator<E> descendingIterator() {
  109.         return decorated().descendingIterator();
  110.     }

  111.     @Override
  112.     public NavigableSet<E> descendingSet() {
  113.         return transformingNavigableSet(decorated().descendingSet(), transformer);
  114.     }

  115.     @Override
  116.     public E floor(final E e) {
  117.         return decorated().floor(e);
  118.     }

  119.     @Override
  120.     public NavigableSet<E> headSet(final E toElement, final boolean inclusive) {
  121.         final NavigableSet<E> head = decorated().headSet(toElement, inclusive);
  122.         return transformingNavigableSet(head, transformer);
  123.     }

  124.     @Override
  125.     public E higher(final E e) {
  126.         return decorated().higher(e);
  127.     }

  128.     @Override
  129.     public E lower(final E e) {
  130.         return decorated().lower(e);
  131.     }

  132.     @Override
  133.     public E pollFirst() {
  134.         return decorated().pollFirst();
  135.     }

  136.     @Override
  137.     public E pollLast() {
  138.         return decorated().pollLast();
  139.     }

  140.     @Override
  141.     public NavigableSet<E> subSet(final E fromElement, final boolean fromInclusive, final E toElement,
  142.             final boolean toInclusive) {
  143.         final NavigableSet<E> sub = decorated().subSet(fromElement, fromInclusive, toElement, toInclusive);
  144.         return transformingNavigableSet(sub, transformer);
  145.     }

  146.     @Override
  147.     public NavigableSet<E> tailSet(final E fromElement, final boolean inclusive) {
  148.         final NavigableSet<E> tail = decorated().tailSet(fromElement, inclusive);
  149.         return transformingNavigableSet(tail, transformer);
  150.     }

  151. }