001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.functors;
018
019import java.io.Serializable;
020import java.util.Map;
021
022import org.apache.commons.collections4.Transformer;
023
024/**
025 * Transformer implementation that returns the value held in a specified map
026 * using the input parameter as a key.
027 *
028 * @param <T> the type of the input to the function.
029 * @param <R> the type of the result of the function.
030 * @since 3.0
031 */
032public final class MapTransformer<T, R> implements Transformer<T, R>, Serializable {
033
034    /** Serial version UID */
035    private static final long serialVersionUID = 862391807045468939L;
036
037    /**
038     * Creates the transformer.
039     * <p>
040     * If the map is null, a transformer that always returns null is returned.
041     *
042     * @param <I>  the input type
043     * @param <O>  the output type
044     * @param map the map, not cloned
045     * @return the transformer
046     */
047    public static <I, O> Transformer<I, O> mapTransformer(final Map<? super I, ? extends O> map) {
048        if (map == null) {
049            return ConstantTransformer.<I, O>nullTransformer();
050        }
051        return new MapTransformer<>(map);
052    }
053
054    /** The map of data to lookup in */
055    private final Map<? super T, ? extends R> iMap;
056
057    /**
058     * Constructor that performs no validation.
059     * Use {@code mapTransformer} if you want that.
060     *
061     * @param map  the map to use for lookup, not cloned
062     */
063    private MapTransformer(final Map<? super T, ? extends R> map) {
064        iMap = map;
065    }
066
067    /**
068     * Gets the map to lookup in.
069     *
070     * @return the map
071     * @since 3.1
072     */
073    public Map<? super T, ? extends R> getMap() {
074        return iMap;
075    }
076
077    /**
078     * Transforms the input to result by looking it up in a {@code Map}.
079     *
080     * @param input  the input object to transform
081     * @return the transformed result
082     */
083    @Override
084    public R transform(final T input) {
085        return iMap.get(input);
086    }
087
088}