View Javadoc

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.collections.functors;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.collections.Factory;
22  
23  /**
24   * Factory implementation that returns the same constant each time.
25   * <p>
26   * No check is made that the object is immutable. In general, only immutable
27   * objects should use the constant factory. Mutable objects should
28   * use the prototype factory.
29   *
30   * @since 3.0
31   * @version $Id: ConstantFactory.java 1435965 2013-01-20 21:13:18Z tn $
32   */
33  public class ConstantFactory<T> implements Factory<T>, Serializable {
34  
35      /** Serial version UID */
36      private static final long serialVersionUID = -3520677225766901240L;
37      
38      /** Returns null each time */
39      public static final Factory<Object> NULL_INSTANCE = new ConstantFactory<Object>(null);
40  
41      /** The closures to call in turn */
42      private final T iConstant;
43  
44      /**
45       * Factory method that performs validation.
46       *
47       * @param <T>  the type of the constant
48       * @param constantToReturn  the constant object to return each time in the factory
49       * @return the <code>constant</code> factory.
50       */
51      @SuppressWarnings("unchecked")
52      public static <T> Factory<T> constantFactory(final T constantToReturn) {
53          if (constantToReturn == null) {
54              return (Factory<T>) NULL_INSTANCE;
55          }
56          return new ConstantFactory<T>(constantToReturn);
57      }
58      
59      /**
60       * Constructor that performs no validation.
61       * Use <code>getInstance</code> if you want that.
62       * 
63       * @param constantToReturn  the constant to return each time
64       */
65      public ConstantFactory(final T constantToReturn) {
66          super();
67          iConstant = constantToReturn;
68      }
69  
70      /**
71       * Always return constant.
72       * 
73       * @return the stored constant value
74       */
75      public T create() {
76          return iConstant;
77      }
78  
79      /**
80       * Gets the constant.
81       * 
82       * @return the constant
83       * @since 3.1
84       */
85      public T getConstant() {
86          return iConstant;
87      }
88  
89  }