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.multimap;
18  
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.Objects;
22  
23  import org.apache.commons.collections4.CollectionUtils;
24  import org.apache.commons.collections4.FluentIterable;
25  import org.apache.commons.collections4.MultiValuedMap;
26  import org.apache.commons.collections4.Transformer;
27  
28  /**
29   * Decorates another {@code MultiValuedMap} to transform objects that are added.
30   * <p>
31   * This class affects the MultiValuedMap put methods. Thus objects must be
32   * removed or searched for using their transformed form. For example, if the
33   * transformation converts Strings to Integers, you must use the Integer form to
34   * remove objects.
35   * </p>
36   * <p>
37   * <strong>Note that TransformedMultiValuedMap is not synchronized and is not thread-safe.</strong>
38   * </p>
39   *
40   * @param <K> the type of the keys in this map
41   * @param <V> the type of the values in this map
42   * @since 4.1
43   */
44  public class TransformedMultiValuedMap<K, V> extends AbstractMultiValuedMapDecorator<K, V> {
45  
46      /** Serialization Version */
47      private static final long serialVersionUID = 20150612L;
48  
49      /**
50       * Factory method to create a transforming MultiValuedMap that will
51       * transform existing contents of the specified map.
52       * <p>
53       * If there are any elements already in the map being decorated, they will
54       * be transformed by this method. Contrast this with
55       * {@link #transformingMap(MultiValuedMap, Transformer, Transformer)}.
56       *
57       * @param <K> the key type
58       * @param <V> the value type
59       * @param map  the MultiValuedMap to decorate, may not be null
60       * @param keyTransformer  the transformer to use for key conversion, null means no conversion
61       * @param valueTransformer  the transformer to use for value conversion, null means no conversion
62       * @return a new transformed MultiValuedMap
63       * @throws NullPointerException if map is null
64       */
65      public static <K, V> TransformedMultiValuedMap<K, V> transformedMap(final MultiValuedMap<K, V> map,
66              final Transformer<? super K, ? extends K> keyTransformer,
67              final Transformer<? super V, ? extends V> valueTransformer) {
68          final TransformedMultiValuedMap<K, V> decorated =
69                  new TransformedMultiValuedMap<>(map, keyTransformer, valueTransformer);
70          if (!map.isEmpty()) {
71              final MultiValuedMap<K, V> mapCopy = new ArrayListValuedHashMap<>(map);
72              decorated.clear();
73              decorated.putAll(mapCopy);
74          }
75          return decorated;
76      }
77  
78      /**
79       * Factory method to create a transforming MultiValuedMap.
80       * <p>
81       * If there are any elements already in the map being decorated, they are
82       * NOT transformed. Contrast this with
83       * {@link #transformedMap(MultiValuedMap, Transformer, Transformer)}.
84       *
85       * @param <K> the key type
86       * @param <V> the value type
87       * @param map  the MultiValuedMap to decorate, may not be null
88       * @param keyTransformer  the transformer to use for key conversion, null means no conversion
89       * @param valueTransformer  the transformer to use for value conversion, null means no conversion
90       * @return a new transformed MultiValuedMap
91       * @throws NullPointerException if map is null
92       */
93      public static <K, V> TransformedMultiValuedMap<K, V> transformingMap(final MultiValuedMap<K, V> map,
94              final Transformer<? super K, ? extends K> keyTransformer,
95              final Transformer<? super V, ? extends V> valueTransformer) {
96          return new TransformedMultiValuedMap<>(map, keyTransformer, valueTransformer);
97      }
98  
99      /** The key transformer */
100     private final Transformer<? super K, ? extends K> keyTransformer;
101 
102     /** The value transformer */
103     private final Transformer<? super V, ? extends V> valueTransformer;
104 
105     /**
106      * Constructor that wraps (not copies).
107      * <p>
108      * If there are any elements already in the collection being decorated, they
109      * are NOT transformed.
110      *
111      * @param map  the MultiValuedMap to decorate, may not be null
112      * @param keyTransformer  the transformer to use for key conversion, null means no conversion
113      * @param valueTransformer  the transformer to use for value conversion, null means no conversion
114      * @throws NullPointerException if map is null
115      */
116     protected TransformedMultiValuedMap(final MultiValuedMap<K, V> map,
117             final Transformer<? super K, ? extends K> keyTransformer,
118             final Transformer<? super V, ? extends V> valueTransformer) {
119         super(map);
120         this.keyTransformer = keyTransformer;
121         this.valueTransformer = valueTransformer;
122     }
123 
124     @Override
125     public boolean put(final K key, final V value) {
126         return decorated().put(transformKey(key), transformValue(value));
127     }
128 
129     @Override
130     public boolean putAll(final K key, final Iterable<? extends V> values) {
131         Objects.requireNonNull(values, "values");
132 
133         final Iterable<V> transformedValues = FluentIterable.of(values).transform(valueTransformer);
134         final Iterator<? extends V> it = transformedValues.iterator();
135         return it.hasNext() && CollectionUtils.addAll(decorated().get(transformKey(key)), it);
136     }
137 
138     @Override
139     public boolean putAll(final Map<? extends K, ? extends V> map) {
140         Objects.requireNonNull(map, "map");
141         boolean changed = false;
142         for (final Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
143             changed |= put(entry.getKey(), entry.getValue());
144         }
145         return changed;
146     }
147 
148     @Override
149     public boolean putAll(final MultiValuedMap<? extends K, ? extends V> map) {
150         Objects.requireNonNull(map, "map");
151         boolean changed = false;
152         for (final Map.Entry<? extends K, ? extends V> entry : map.entries()) {
153             changed |= put(entry.getKey(), entry.getValue());
154         }
155         return changed;
156     }
157 
158     /**
159      * Transforms a key.
160      * <p>
161      * The transformer itself may throw an exception if necessary.
162      *
163      * @param object  the object to transform
164      * @return the transformed object
165      */
166     protected K transformKey(final K object) {
167         if (keyTransformer == null) {
168             return object;
169         }
170         return keyTransformer.transform(object);
171     }
172 
173     /**
174      * Transforms a value.
175      * <p>
176      * The transformer itself may throw an exception if necessary.
177      *
178      * @param object  the object to transform
179      * @return the transformed object
180      */
181     protected V transformValue(final V object) {
182         if (valueTransformer == null) {
183             return object;
184         }
185         return valueTransformer.transform(object);
186     }
187 
188 }