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 1015642 2017-07-18 12:10:19Z chtompki $
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    @Override
090    public O transform(final I input) {
091        return iConstant;
092    }
093
094    /**
095     * Gets the constant.
096     *
097     * @return the constant
098     * @since 3.1
099     */
100    public O getConstant() {
101        return iConstant;
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public boolean equals(final Object obj) {
109        if (obj == this) {
110            return true;
111        }
112        if (obj instanceof ConstantTransformer == false) {
113            return false;
114        }
115        final Object otherConstant = ((ConstantTransformer<?, ?>) obj).getConstant();
116        return otherConstant == getConstant() || otherConstant != null && otherConstant.equals(getConstant());
117    }
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public int hashCode() {
124        int result = "ConstantTransformer".hashCode() << 2;
125        if (getConstant() != null) {
126            result |= getConstant().hashCode();
127        }
128        return result;
129    }
130}