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 * </p>
030 *
031 * @since 3.0
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<>(null);
041
042    /**
043     * Transformer method that performs validation.
044     *
045     * @param <I>  the input type
046     * @param <O>  the output type
047     * @param constantToReturn  the constant object to return each time in the factory
048     * @return the {@code constant} factory.
049     */
050    public static <I, O> Transformer<I, O> constantTransformer(final O constantToReturn) {
051        if (constantToReturn == null) {
052            return nullTransformer();
053        }
054        return new ConstantTransformer<>(constantToReturn);
055    }
056
057    /**
058     * Gets a typed null instance.
059     *
060     * @param <I>  the input type
061     * @param <O>  the output type
062     * @return Transformer&lt;I, O&gt; that always returns null.
063     */
064    public static <I, O> Transformer<I, O> nullTransformer() {
065        return NULL_INSTANCE;
066    }
067
068    /** The closures to call in turn */
069    private final O iConstant;
070
071    /**
072     * Constructor that performs no validation.
073     * Use {@code constantTransformer} if you want that.
074     *
075     * @param constantToReturn  the constant to return each time
076     */
077    public ConstantTransformer(final O constantToReturn) {
078        iConstant = constantToReturn;
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public boolean equals(final Object obj) {
086        if (obj == this) {
087            return true;
088        }
089        if (!(obj instanceof ConstantTransformer)) {
090            return false;
091        }
092        final Object otherConstant = ((ConstantTransformer<?, ?>) obj).getConstant();
093        return otherConstant == getConstant() || otherConstant != null && otherConstant.equals(getConstant());
094    }
095
096    /**
097     * Gets the constant.
098     *
099     * @return the constant
100     * @since 3.1
101     */
102    public O getConstant() {
103        return iConstant;
104    }
105
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public int hashCode() {
111        int result = "ConstantTransformer".hashCode() << 2;
112        if (getConstant() != null) {
113            result |= getConstant().hashCode();
114        }
115        return result;
116    }
117
118    /**
119     * Transforms the input by ignoring it and returning the stored constant instead.
120     *
121     * @param input  the input object which is ignored
122     * @return the stored constant
123     */
124    @Override
125    public O transform(final I input) {
126        return iConstant;
127    }
128}