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.collections4;
18  
19  import org.apache.commons.collections4.functors.ConstantFactory;
20  import org.apache.commons.collections4.functors.ExceptionFactory;
21  import org.apache.commons.collections4.functors.InstantiateFactory;
22  import org.apache.commons.collections4.functors.PrototypeFactory;
23  
24  /**
25   * {@code FactoryUtils} provides reference implementations and utilities
26   * for the Factory functor interface. The supplied factories are:
27   * <ul>
28   * <li>Prototype - clones a specified object
29   * <li>Instantiate - creates objects using reflection
30   * <li>Constant - always returns the same object
31   * <li>Null - always returns null
32   * <li>Exception - always throws an exception
33   * </ul>
34   * <p>
35   * Since v4.1 only factories which are considered to be safe are
36   * Serializable. Factories considered to be unsafe for serialization are:
37   * <ul>
38   * <li>Prototype
39   * <li>Instantiate
40   * </ul>
41   *
42   * @since 3.0
43   */
44  public class FactoryUtils {
45  
46      /**
47       * Creates a Factory that will return the same object each time the factory
48       * is used. No check is made that the object is immutable. In general, only
49       * immutable objects should use the constant factory. Mutable objects should
50       * use the prototype factory.
51       *
52       * @see org.apache.commons.collections4.functors.ConstantFactory
53       *
54       * @param <T> the type that the factory creates
55       * @param constantToReturn  the constant object to return each time in the factory
56       * @return the {@code constant} factory.
57       */
58      public static <T> Factory<T> constantFactory(final T constantToReturn) {
59          return ConstantFactory.constantFactory(constantToReturn);
60      }
61  
62      /**
63       * Gets a Factory that always throws an exception.
64       * This could be useful during testing as a placeholder.
65       *
66       * @see org.apache.commons.collections4.functors.ExceptionFactory
67       *
68       * @param <T> the type that the factory creates
69       * @return the factory
70       */
71      public static <T> Factory<T> exceptionFactory() {
72          return ExceptionFactory.<T>exceptionFactory();
73      }
74  
75      /**
76       * Creates a Factory that can create objects of a specific type using
77       * a no-args constructor.
78       *
79       * @see org.apache.commons.collections4.functors.InstantiateFactory
80       *
81       * @param <T> the type that the factory creates
82       * @param classToInstantiate  the Class to instantiate each time in the factory
83       * @return the {@code reflection} factory
84       * @throws NullPointerException if the classToInstantiate is null
85       */
86      public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate) {
87          return InstantiateFactory.instantiateFactory(classToInstantiate, null, null);
88      }
89  
90      /**
91       * Creates a Factory that can create objects of a specific type using
92       * the arguments specified to this method.
93       *
94       * @see org.apache.commons.collections4.functors.InstantiateFactory
95       *
96       * @param <T> the type that the factory creates
97       * @param classToInstantiate  the Class to instantiate each time in the factory
98       * @param paramTypes  parameter types for the constructor, can be null
99       * @param args  the arguments to pass to the constructor, can be null
100      * @return the {@code reflection} factory
101      * @throws NullPointerException if the classToInstantiate is null
102      * @throws IllegalArgumentException if the paramTypes and args don't match
103      * @throws IllegalArgumentException if the constructor doesn't exist
104      */
105     public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes,
106                                                     final Object[] args) {
107         return InstantiateFactory.instantiateFactory(classToInstantiate, paramTypes, args);
108     }
109 
110     /**
111      * Gets a Factory that will return null each time the factory is used.
112      * This could be useful during testing as a placeholder.
113      *
114      * @see org.apache.commons.collections4.functors.ConstantFactory
115      * @param <T> the "type" of null object the factory should return.
116      * @return the factory
117      */
118     public static <T> Factory<T> nullFactory() {
119         return ConstantFactory.<T>constantFactory(null);
120     }
121 
122     /**
123      * Creates a Factory that will return a clone of the same prototype object
124      * each time the factory is used. The prototype will be cloned using one of these
125      * techniques (in order):
126      *
127      * <ul>
128      * <li>public clone method</li>
129      * <li>public copy constructor</li>
130      * <li>serialization clone</li>
131      * </ul>
132      *
133      * @see org.apache.commons.collections4.functors.PrototypeFactory
134      *
135      * @param <T> the type that the factory creates
136      * @param prototype  the object to clone each time in the factory
137      * @return the {@code prototype} factory, or a {@link ConstantFactory#NULL_INSTANCE} if
138      * the {@code prototype} is {@code null}
139      * @throws IllegalArgumentException if the prototype cannot be cloned
140      */
141     public static <T> Factory<T> prototypeFactory(final T prototype) {
142         return PrototypeFactory.<T>prototypeFactory(prototype);
143     }
144 
145     /**
146      * Don't allow instances.
147      */
148     private FactoryUtils() {}
149 
150 }