View Javadoc
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  
19  import java.util.Comparator;
20  import java.util.SortedSet;
21  
22  import org.apache.commons.collections4.Transformer;
23  
24  /**
25   * Decorates another {@code SortedSet} to transform objects that are added.
26   * <p>
27   * The add methods are affected by this class.
28   * Thus objects must be removed or searched for using their transformed form.
29   * For example, if the transformation converts Strings to Integers, you must
30   * use the Integer form to remove objects.
31   * </p>
32   * <p>
33   * This class is Serializable from Commons Collections 3.1.
34   * </p>
35   *
36   * @param <E> the type of the elements in this set
37   * @since 3.0
38   */
39  public class TransformedSortedSet<E> extends TransformedSet<E> implements SortedSet<E> {
40  
41      /** Serialization version */
42      private static final long serialVersionUID = -1675486811351124386L;
43  
44      /**
45       * Factory method to create a transforming sorted set that will transform
46       * existing contents of the specified sorted set.
47       * <p>
48       * If there are any elements already in the set being decorated, they
49       * will be transformed by this method.
50       * Contrast this with {@link #transformingSortedSet(SortedSet, Transformer)}.
51       *
52       * @param <E> the element type
53       * @param set  the set to decorate, must not be null
54       * @param transformer  the transformer to use for conversion, must not be null
55       * @return a new transformed {@link SortedSet}
56       * @throws NullPointerException if set or transformer is null
57       * @since 4.0
58       */
59      public static <E> TransformedSortedSet<E> transformedSortedSet(final SortedSet<E> set,
60              final Transformer<? super E, ? extends E> transformer) {
61  
62          final TransformedSortedSet<E> decorated = new TransformedSortedSet<>(set, transformer);
63          if (!set.isEmpty()) {
64              @SuppressWarnings("unchecked") // set is type E
65              final E[] values = (E[]) set.toArray(); // NOPMD - false positive for generics
66              set.clear();
67              for (final E value : values) {
68                  decorated.decorated().add(transformer.transform(value));
69              }
70          }
71          return decorated;
72      }
73  
74      /**
75       * Factory method to create a transforming sorted set.
76       * <p>
77       * If there are any elements already in the set being decorated, they
78       * are NOT transformed.
79       * Contrast this with {@link #transformedSortedSet(SortedSet, Transformer)}.
80       *
81       * @param <E> the element type
82       * @param set  the set to decorate, must not be null
83       * @param transformer  the transformer to use for conversion, must not be null
84       * @return a new transformed {@link SortedSet}
85       * @throws NullPointerException if set or transformer is null
86       * @since 4.0
87       */
88      public static <E> TransformedSortedSet<E> transformingSortedSet(final SortedSet<E> set,
89              final Transformer<? super E, ? extends E> transformer) {
90          return new TransformedSortedSet<>(set, transformer);
91      }
92  
93      /**
94       * Constructor that wraps (not copies).
95       * <p>
96       * If there are any elements already in the set being decorated, they
97       * are NOT transformed.
98       *
99       * @param set  the set to decorate, must not be null
100      * @param transformer  the transformer to use for conversion, must not be null
101      * @throws NullPointerException if set or transformer is null
102      */
103     protected TransformedSortedSet(final SortedSet<E> set, final Transformer<? super E, ? extends E> transformer) {
104         super(set, transformer);
105     }
106 
107     @Override
108     public Comparator<? super E> comparator() {
109         return getSortedSet().comparator();
110     }
111 
112     @Override
113     public E first() {
114         return getSortedSet().first();
115     }
116 
117     /**
118      * Gets the decorated set.
119      *
120      * @return the decorated set
121      */
122     protected SortedSet<E> getSortedSet() {
123         return (SortedSet<E>) decorated();
124     }
125 
126     @Override
127     public SortedSet<E> headSet(final E toElement) {
128         final SortedSet<E> set = getSortedSet().headSet(toElement);
129         return new TransformedSortedSet<>(set, transformer);
130     }
131 
132     @Override
133     public E last() {
134         return getSortedSet().last();
135     }
136 
137     @Override
138     public SortedSet<E> subSet(final E fromElement, final E toElement) {
139         final SortedSet<E> set = getSortedSet().subSet(fromElement, toElement);
140         return new TransformedSortedSet<>(set, transformer);
141     }
142 
143     @Override
144     public SortedSet<E> tailSet(final E fromElement) {
145         final SortedSet<E> set = getSortedSet().tailSet(fromElement);
146         return new TransformedSortedSet<>(set, transformer);
147     }
148 
149 }