TransformedSortedSet.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.Comparator;
  19. import java.util.SortedSet;

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

  21. /**
  22.  * Decorates another {@code SortedSet} 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.  * <p>
  30.  * This class is Serializable from Commons Collections 3.1.
  31.  * </p>
  32.  *
  33.  * @param <E> the type of the elements in this set
  34.  * @since 3.0
  35.  */
  36. public class TransformedSortedSet<E> extends TransformedSet<E> implements SortedSet<E> {

  37.     /** Serialization version */
  38.     private static final long serialVersionUID = -1675486811351124386L;

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

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

  67.     /**
  68.      * Factory method to create a transforming sorted set.
  69.      * <p>
  70.      * If there are any elements already in the set being decorated, they
  71.      * are NOT transformed.
  72.      * Contrast this with {@link #transformedSortedSet(SortedSet, Transformer)}.
  73.      *
  74.      * @param <E> the element type
  75.      * @param set  the set to decorate, must not be null
  76.      * @param transformer  the transformer to use for conversion, must not be null
  77.      * @return a new transformed {@link SortedSet}
  78.      * @throws NullPointerException if set or transformer is null
  79.      * @since 4.0
  80.      */
  81.     public static <E> TransformedSortedSet<E> transformingSortedSet(final SortedSet<E> set,
  82.             final Transformer<? super E, ? extends E> transformer) {
  83.         return new TransformedSortedSet<>(set, transformer);
  84.     }

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

  98.     @Override
  99.     public Comparator<? super E> comparator() {
  100.         return getSortedSet().comparator();
  101.     }

  102.     @Override
  103.     public E first() {
  104.         return getSortedSet().first();
  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.     @Override
  115.     public SortedSet<E> headSet(final E toElement) {
  116.         final SortedSet<E> set = getSortedSet().headSet(toElement);
  117.         return new TransformedSortedSet<>(set, transformer);
  118.     }

  119.     @Override
  120.     public E last() {
  121.         return getSortedSet().last();
  122.     }

  123.     @Override
  124.     public SortedSet<E> subSet(final E fromElement, final E toElement) {
  125.         final SortedSet<E> set = getSortedSet().subSet(fromElement, toElement);
  126.         return new TransformedSortedSet<>(set, transformer);
  127.     }

  128.     @Override
  129.     public SortedSet<E> tailSet(final E fromElement) {
  130.         final SortedSet<E> set = getSortedSet().tailSet(fromElement);
  131.         return new TransformedSortedSet<>(set, transformer);
  132.     }

  133. }