MapTransformer.java

  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. import java.io.Serializable;
  19. import java.util.Map;

  20. import org.apache.commons.collections4.Transformer;

  21. /**
  22.  * Transformer implementation that returns the value held in a specified map
  23.  * using the input parameter as a key.
  24.  *
  25.  * @param <T> the type of the input to the function.
  26.  * @param <R> the type of the result of the function.
  27.  * @since 3.0
  28.  */
  29. public final class MapTransformer<T, R> implements Transformer<T, R>, Serializable {

  30.     /** Serial version UID */
  31.     private static final long serialVersionUID = 862391807045468939L;

  32.     /**
  33.      * Creates the transformer.
  34.      * <p>
  35.      * If the map is null, a transformer that always returns null is returned.
  36.      *
  37.      * @param <I>  the input type
  38.      * @param <O>  the output type
  39.      * @param map the map, not cloned
  40.      * @return the transformer
  41.      */
  42.     public static <I, O> Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map) {
  43.         if (map == null) {
  44.             return ConstantTransformer.<I, O>nullTransformer();
  45.         }
  46.         return new MapTransformer<>(map);
  47.     }

  48.     /** The map of data to lookup in */
  49.     private final Map<? super T, ? extends R> iMap;

  50.     /**
  51.      * Constructor that performs no validation.
  52.      * Use {@code mapTransformer} if you want that.
  53.      *
  54.      * @param map  the map to use for lookup, not cloned
  55.      */
  56.     private MapTransformer(final Map<? super T, ? extends R> map) {
  57.         iMap = map;
  58.     }

  59.     /**
  60.      * Gets the map to lookup in.
  61.      *
  62.      * @return the map
  63.      * @since 3.1
  64.      */
  65.     public Map<? super T, ? extends R> getMap() {
  66.         return iMap;
  67.     }

  68.     /**
  69.      * Transforms the input to result by looking it up in a {@code Map}.
  70.      *
  71.      * @param input  the input object to transform
  72.      * @return the transformed result
  73.      */
  74.     @Override
  75.     public R transform(final T input) {
  76.         return iMap.get(input);
  77.     }

  78. }