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