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.functors;
18  
19  import java.io.Serializable;
20  import java.util.Map;
21  
22  import org.apache.commons.collections4.Transformer;
23  
24  /**
25   * Transformer implementation that returns the value held in a specified map
26   * using the input parameter as a key.
27   *
28   * @param <T> the type of the input to the function.
29   * @param <R> the type of the result of the function.
30   * @since 3.0
31   */
32  public final class MapTransformer<T, R> implements Transformer<T, R>, Serializable {
33  
34      /** Serial version UID */
35      private static final long serialVersionUID = 862391807045468939L;
36  
37      /**
38       * Creates the transformer.
39       * <p>
40       * If the map is null, a transformer that always returns null is returned.
41       *
42       * @param <I>  the input type
43       * @param <O>  the output type
44       * @param map the map, not cloned
45       * @return the transformer
46       */
47      public static <I, O> Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map) {
48          if (map == null) {
49              return ConstantTransformer.<I, O>nullTransformer();
50          }
51          return new MapTransformer<>(map);
52      }
53  
54      /** The map of data to lookup in */
55      private final Map<? super T, ? extends R> iMap;
56  
57      /**
58       * Constructor that performs no validation.
59       * Use {@code mapTransformer} if you want that.
60       *
61       * @param map  the map to use for lookup, not cloned
62       */
63      private MapTransformer(final Map<? super T, ? extends R> map) {
64          iMap = map;
65      }
66  
67      /**
68       * Gets the map to lookup in.
69       *
70       * @return the map
71       * @since 3.1
72       */
73      public Map<? super T, ? extends R> getMap() {
74          return iMap;
75      }
76  
77      /**
78       * Transforms the input to result by looking it up in a {@code Map}.
79       *
80       * @param input  the input object to transform
81       * @return the transformed result
82       */
83      @Override
84      public R transform(final T input) {
85          return iMap.get(input);
86      }
87  
88  }