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