    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.Objects;
    021
    022import org.apache.commons.collections4.Transformer;
    023
    024/**
    025 * Transformer implementation that returns the same constant each time.
    026 * <p>
    027 * No check is made that the object is immutable. In general, only immutable
    028 * objects should use the constant factory. Mutable objects should
    029 * use the prototype factory.
    030 * </p>
    031 *
    032 * @param <T> the type of the input to the function.
    033 * @param <R> the type of the result of the function.
    034 * @since 3.0
    035 */
    036public class ConstantTransformer<T, R> implements Transformer<T, R>, Serializable {
    037
    038    /** Serial version UID */
    039    private static final long serialVersionUID = 6374440726369055124L;
    040
    041    /** Returns null each time */
    042    @SuppressWarnings("rawtypes")
    043    public static final Transformer NULL_INSTANCE = new ConstantTransformer<>(null);
    044
    045    /**
    046     * Transformer method that performs validation.
    047     *
    048     * @param <I>  the input type
    049     * @param <O>  the output type
    050     * @param constantToReturn  the constant object to return each time in the factory
    051     * @return the {@code constant} factory.
    052     */
    053    public static <I, O> Transformer<I, O> constantTransformer(final O constantToReturn) {
    054        if (constantToReturn == null) {
    055            return nullTransformer();
    056        }
    057        return new ConstantTransformer<>(constantToReturn);
    058    }
    059
    060    /**
    061     * Gets a typed null instance.
    062     *
    063     * @param <I>  the input type
    064     * @param <O>  the output type
    065     * @return Transformer&lt;I, O&gt; that always returns null.
    066     */
    067    public static <I, O> Transformer<I, O> nullTransformer() {
    068        return NULL_INSTANCE;
    069    }
    070
    071    /** The closures to call in turn */
    072    private final R iConstant;
    073
    074    /**
    075     * Constructor that performs no validation.
    076     * Use {@code constantTransformer} if you want that.
    077     *
    078     * @param constantToReturn  the constant to return each time
    079     */
    080    public ConstantTransformer(final R constantToReturn) {
    081        iConstant = constantToReturn;
    082    }
    083
    084    /**
    085     * {@inheritDoc}
    086     */
    087    @Override
    088    public boolean equals(final Object obj) {
    089        if (obj == this) {
    090            return true;
    091        }
    092        if (!(obj instanceof ConstantTransformer)) {
    093            return false;
    094        }
    095        final Object otherConstant = ((ConstantTransformer<?, ?>) obj).getConstant();
    096        return Objects.equals(otherConstant, getConstant());
    097    }
    098
    099    /**
    100     * Gets the constant.
    101     *
    102     * @return the constant
    103     * @since 3.1
    104     */
    105    public R getConstant() {
    106        return iConstant;
    107    }
    108
    109    /**
    110     * {@inheritDoc}
    111     */
    112    @Override
    113    public int hashCode() {
    114        int result = "ConstantTransformer".hashCode() << 2;
    115        if (getConstant() != null) {
    116            result |= getConstant().hashCode();
    117        }
    118        return result;
    119    }
    120
    121    /**
    122     * Transforms the input by ignoring it and returning the stored constant instead.
    123     *
    124     * @param input  the input object which is ignored
    125     * @return the stored constant
    126     */
    127    @Override
    128    public R transform(final T input) {
    129        return iConstant;
    130    }
    131}