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 * https://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.collection;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Objects;
23
24 import org.apache.commons.collections4.Transformer;
25
26 /**
27 * Decorates another {@link Collection} to transform objects that are added.
28 * <p>
29 * The add methods are affected by this class.
30 * Thus objects must be removed or searched for using their transformed form.
31 * For example, if the transformation converts Strings to Integers, you must
32 * use the Integer form to remove objects.
33 * </p>
34 * <p>
35 * This class is Serializable from Commons Collections 3.1.
36 * </p>
37 *
38 * @param <E> The type of the elements in the collection.
39 * @since 3.0
40 */
41 public class TransformedCollection<E> extends AbstractCollectionDecorator<E> {
42
43 /** Serialization version */
44 private static final long serialVersionUID = 8692300188161871514L;
45
46 /**
47 * Creates a transforming collection that will transform
48 * existing contents of the specified collection.
49 * <p>
50 * If there are any elements already in the collection being decorated, they
51 * will be transformed by this method.
52 * Contrast this with {@link #transformingCollection(Collection, Transformer)}.
53 * </p>
54 *
55 * @param <E> The type of the elements in the collection.
56 * @param collection The collection to decorate, must not be null.
57 * @param transformer The transformer to use for conversion, must not be null.
58 * @return A new transformed Collection.
59 * @throws NullPointerException if collection or transformer is null.
60 * @since 4.0
61 */
62 public static <E> TransformedCollection<E> transformedCollection(final Collection<E> collection,
63 final Transformer<? super E, ? extends E> transformer) {
64
65 final TransformedCollection<E> decorated = new TransformedCollection<>(collection, transformer);
66 // null collection & transformer are disallowed by the constructor call above
67 if (!collection.isEmpty()) {
68 @SuppressWarnings("unchecked") // collection is of type E
69 final E[] values = (E[]) collection.toArray(); // NOPMD - false positive for generics
70 collection.clear();
71 for (final E value : values) {
72 decorated.decorated().add(transformer.apply(value));
73 }
74 }
75 return decorated;
76 }
77
78 /**
79 * Creates a transforming collection.
80 * <p>
81 * If there are any elements already in the collection being decorated, they
82 * are NOT transformed.
83 * Contrast this with {@link #transformedCollection(Collection, Transformer)}.
84 * </p>
85 *
86 * @param <E> The type of the elements in the collection.
87 * @param coll The collection to decorate, must not be null.
88 * @param transformer The transformer to use for conversion, must not be null.
89 * @return A new transformed collection.
90 * @throws NullPointerException if collection or transformer is null.
91 * @since 4.0
92 */
93 public static <E> TransformedCollection<E> transformingCollection(final Collection<E> coll,
94 final Transformer<? super E, ? extends E> transformer) {
95 return new TransformedCollection<>(coll, transformer);
96 }
97
98 /** The transformer to use */
99 protected final Transformer<? super E, ? extends E> transformer;
100
101 /**
102 * Constructs and wraps (not copies).
103 * <p>
104 * If there are any elements already in the collection being decorated, they
105 * are NOT transformed.
106 * </p>
107 *
108 * @param collection The collection to decorate, must not be null.
109 * @param transformer The transformer to use for conversion, must not be null.
110 * @throws NullPointerException if collection or transformer is null.
111 */
112 protected TransformedCollection(final Collection<E> collection, final Transformer<? super E, ? extends E> transformer) {
113 super(collection);
114 this.transformer = Objects.requireNonNull(transformer, "transformer");
115 }
116
117 @Override
118 public boolean add(final E object) {
119 return decorated().add(transform(object));
120 }
121
122 @Override
123 public boolean addAll(final Collection<? extends E> coll) {
124 return decorated().addAll(transform(coll));
125 }
126
127 /**
128 * Transforms a collection.
129 * <p>
130 * The transformer itself may throw an exception if necessary.
131 * </p>
132 *
133 * @param coll The collection to transform.
134 * @return A transformed object.
135 */
136 protected Collection<E> transform(final Collection<? extends E> coll) {
137 final List<E> list = new ArrayList<>(coll.size());
138 for (final E item : coll) {
139 list.add(transform(item));
140 }
141 return list;
142 }
143
144 /**
145 * Transforms an object.
146 * <p>
147 * The transformer itself may throw an exception if necessary.
148 * </p>
149 *
150 * @param object The object to transform.
151 * @return A transformed object.
152 */
153 protected E transform(final E object) {
154 return transformer.apply(object);
155 }
156
157 }