    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 *      https://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.Factory;
    022
    023/**
    024 * Factory 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 * @param <T> The type of results supplied by this supplier.
    032 * @since 3.0
    033 */
    034public class ConstantFactory<T> implements Factory<T>, Serializable {
    035
    036    /** Serial version UID */
    037    private static final long serialVersionUID = -3520677225766901240L;
    038
    039    /** Returns null each time */
    040    @SuppressWarnings("rawtypes") // The null factory works for all object types
    041    public static final Factory NULL_INSTANCE = new ConstantFactory<>(null);
    042
    043    /**
    044     * Factory method that performs validation.
    045     *
    046     * @param <T>  the type of the constant
    047     * @param constantToReturn  The constant object to return each time in the factory
    048     * @return The {@code constant} factory.
    049     */
    050    public static <T> Factory<T> constantFactory(final T constantToReturn) {
    051        if (constantToReturn == null) {
    052            return NULL_INSTANCE;
    053        }
    054        return new ConstantFactory<>(constantToReturn);
    055    }
    056
    057    /** The closures to call in turn */
    058    private final T iConstant;
    059
    060    /**
    061     * Constructor that performs no validation.
    062     * Use {@code constantFactory} if you want that.
    063     *
    064     * @param constantToReturn  The constant to return each time
    065     */
    066    public ConstantFactory(final T constantToReturn) {
    067        iConstant = constantToReturn;
    068    }
    069
    070    /**
    071     * Always return constant.
    072     *
    073     * @return The stored constant value
    074     */
    075    @Override
    076    public T create() {
    077        return iConstant;
    078    }
    079
    080    /**
    081     * Gets the constant.
    082     *
    083     * @return The constant
    084     * @since 3.1
    085     */
    086    public T getConstant() {
    087        return iConstant;
    088    }
    089
    090}