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.Set;
20
21 import org.apache.commons.collections4.Transformer;
22 import org.apache.commons.collections4.collection.TransformedCollection;
23
24 /**
25 * Decorates another {@code Set} 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 TransformedSet<E> extends TransformedCollection<E> implements Set<E> {
40
41 /** Serialization version */
42 private static final long serialVersionUID = 306127383500410386L;
43
44 /**
45 * Factory method to create a transforming set that will transform
46 * existing contents of the specified 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 #transformingSet(Set, 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 set
56 * @throws NullPointerException if set or transformer is null
57 * @since 4.0
58 */
59 public static <E> Set<E> transformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) {
60 final TransformedSet<E> decorated = new TransformedSet<>(set, transformer);
61 if (!set.isEmpty()) {
62 @SuppressWarnings("unchecked") // set is type E
63 final E[] values = (E[]) set.toArray(); // NOPMD - false positive for generics
64 set.clear();
65 for (final E value : values) {
66 decorated.decorated().add(transformer.apply(value));
67 }
68 }
69 return decorated;
70 }
71
72 /**
73 * Factory method to create a transforming set.
74 * <p>
75 * If there are any elements already in the set being decorated, they
76 * are NOT transformed.
77 * Contrast this with {@link #transformedSet(Set, Transformer)}.
78 *
79 * @param <E> the element type
80 * @param set the set to decorate, must not be null
81 * @param transformer the transformer to use for conversion, must not be null
82 * @return a new transformed set
83 * @throws NullPointerException if set or transformer is null
84 * @since 4.0
85 */
86 public static <E> TransformedSet<E> transformingSet(final Set<E> set,
87 final Transformer<? super E, ? extends E> transformer) {
88 return new TransformedSet<>(set, transformer);
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 NullPointerException if set or transformer is null
100 */
101 protected TransformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) {
102 super(set, transformer);
103 }
104
105 @Override
106 public boolean equals(final Object object) {
107 return object == this || decorated().equals(object);
108 }
109
110 @Override
111 public int hashCode() {
112 return decorated().hashCode();
113 }
114
115 }