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.Set;
20  
21  import org.apache.commons.collections.Transformer;
22  import org.apache.commons.collections.collection.TransformedCollection;
23  
24  /**
25   * Decorates another <code>Set</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: TransformedSet.java 1451177 2013-02-28 11:42:31Z tn $
36   */
37  public class TransformedSet<E> extends TransformedCollection<E> implements Set<E> {
38  
39      /** Serialization version */
40      private static final long serialVersionUID = 306127383500410386L;
41  
42      /**
43       * Factory method to create a transforming 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 #transformedSet(Set, 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 set
53       * @throws IllegalArgumentException if set or transformer is null
54       */
55      public static <E> TransformedSet<E> transformingSet(final Set<E> set,
56              final Transformer<? super E, ? extends E> transformer) {
57          return new TransformedSet<E>(set, transformer);
58      }
59      
60      /**
61       * Factory method to create a transforming set that will transform
62       * existing contents of the specified 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 #transformingSet(Set, 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 set
72       * @throws IllegalArgumentException if set or transformer is null
73       * @since 4.0
74       */
75      public static <E> Set<E> transformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) {
76          final TransformedSet<E> decorated = new TransformedSet<E>(set, transformer);
77          if (transformer != null && set != null && set.size() > 0) {
78              @SuppressWarnings("unchecked") // set is type E
79              final E[] values = (E[]) set.toArray();
80              set.clear();
81              for (final E value : values) {
82                  decorated.decorated().add(transformer.transform(value));
83              }
84          }
85          return decorated;
86      }
87  
88      //-----------------------------------------------------------------------
89      /**
90       * Constructor that wraps (not copies).
91       * <p>
92       * If there are any elements already in the set being decorated, they
93       * are NOT transformed.
94       * 
95       * @param set  the set to decorate, must not be null
96       * @param transformer  the transformer to use for conversion, must not be null
97       * @throws IllegalArgumentException if set or transformer is null
98       */
99      protected TransformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) {
100         super(set, transformer);
101     }
102 
103 }