ConstantFactory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.functors;

  18. import java.io.Serializable;

  19. import org.apache.commons.collections4.Factory;

  20. /**
  21.  * Factory implementation that returns the same constant each time.
  22.  * <p>
  23.  * No check is made that the object is immutable. In general, only immutable
  24.  * objects should use the constant factory. Mutable objects should
  25.  * use the prototype factory.
  26.  * </p>
  27.  *
  28.  * @param <T> the type of results supplied by this supplier.
  29.  * @since 3.0
  30.  */
  31. public class ConstantFactory<T> implements Factory<T>, Serializable {

  32.     /** Serial version UID */
  33.     private static final long serialVersionUID = -3520677225766901240L;

  34.     /** Returns null each time */
  35.     @SuppressWarnings("rawtypes") // The null factory works for all object types
  36.     public static final Factory NULL_INSTANCE = new ConstantFactory<>(null);

  37.     /**
  38.      * Factory method that performs validation.
  39.      *
  40.      * @param <T>  the type of the constant
  41.      * @param constantToReturn  the constant object to return each time in the factory
  42.      * @return the {@code constant} factory.
  43.      */
  44.     public static <T> Factory<T> constantFactory(final T constantToReturn) {
  45.         if (constantToReturn == null) {
  46.             return NULL_INSTANCE;
  47.         }
  48.         return new ConstantFactory<>(constantToReturn);
  49.     }

  50.     /** The closures to call in turn */
  51.     private final T iConstant;

  52.     /**
  53.      * Constructor that performs no validation.
  54.      * Use {@code constantFactory} if you want that.
  55.      *
  56.      * @param constantToReturn  the constant to return each time
  57.      */
  58.     public ConstantFactory(final T constantToReturn) {
  59.         iConstant = constantToReturn;
  60.     }

  61.     /**
  62.      * Always return constant.
  63.      *
  64.      * @return the stored constant value
  65.      */
  66.     @Override
  67.     public T create() {
  68.         return iConstant;
  69.     }

  70.     /**
  71.      * Gets the constant.
  72.      *
  73.      * @return the constant
  74.      * @since 3.1
  75.      */
  76.     public T getConstant() {
  77.         return iConstant;
  78.     }

  79. }