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 * </p> 38 * <ul> 39 * <li>Prototype 40 * <li>Instantiate 41 * </ul> 42 * 43 * @since 3.0 44 */ 45 public class FactoryUtils { 46 47 /** 48 * Creates a Factory that will return the same object each time the factory 49 * is used. No check is made that the object is immutable. In general, only 50 * immutable objects should use the constant factory. Mutable objects should 51 * use the prototype factory. 52 * 53 * @see org.apache.commons.collections4.functors.ConstantFactory 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 * @param <T> the type that the factory creates 68 * @return the factory 69 */ 70 public static <T> Factory<T> exceptionFactory() { 71 return ExceptionFactory.<T>exceptionFactory(); 72 } 73 74 /** 75 * Creates a Factory that can create objects of a specific type using 76 * a no-args constructor. 77 * 78 * @see org.apache.commons.collections4.functors.InstantiateFactory 79 * @param <T> the type that the factory creates 80 * @param classToInstantiate the Class to instantiate each time in the factory 81 * @return the {@code reflection} factory 82 * @throws NullPointerException if the classToInstantiate is null 83 */ 84 public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate) { 85 return InstantiateFactory.instantiateFactory(classToInstantiate, null, null); 86 } 87 88 /** 89 * Creates a Factory that can create objects of a specific type using 90 * the arguments specified to this method. 91 * 92 * @see org.apache.commons.collections4.functors.InstantiateFactory 93 * @param <T> the type that the factory creates 94 * @param classToInstantiate the Class to instantiate each time in the factory 95 * @param paramTypes parameter types for the constructor, can be null 96 * @param args the arguments to pass to the constructor, can be null 97 * @return the {@code reflection} factory 98 * @throws NullPointerException if the classToInstantiate is null 99 * @throws IllegalArgumentException if the paramTypes and args don't match 100 * @throws IllegalArgumentException if the constructor doesn't exist 101 */ 102 public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes, 103 final Object[] args) { 104 return InstantiateFactory.instantiateFactory(classToInstantiate, paramTypes, args); 105 } 106 107 /** 108 * Gets a Factory that will return null each time the factory is used. 109 * This could be useful during testing as a placeholder. 110 * 111 * @see org.apache.commons.collections4.functors.ConstantFactory 112 * @param <T> the "type" of null object the factory should return. 113 * @return the factory 114 */ 115 public static <T> Factory<T> nullFactory() { 116 return ConstantFactory.<T>constantFactory(null); 117 } 118 119 /** 120 * Creates a Factory that will return a clone of the same prototype object 121 * each time the factory is used. The prototype will be cloned using one of these 122 * techniques (in order): 123 * 124 * <ul> 125 * <li>public clone method</li> 126 * <li>public copy constructor</li> 127 * <li>serialization clone</li> 128 * </ul> 129 * 130 * @see org.apache.commons.collections4.functors.PrototypeFactory 131 * @param <T> the type that the factory creates 132 * @param prototype the object to clone each time in the factory 133 * @return the {@code prototype} factory, or a {@link ConstantFactory#NULL_INSTANCE} if 134 * the {@code prototype} is {@code null} 135 * @throws IllegalArgumentException if the prototype cannot be cloned 136 */ 137 public static <T> Factory<T> prototypeFactory(final T prototype) { 138 return PrototypeFactory.<T>prototypeFactory(prototype); 139 } 140 141 /** 142 * Don't allow instances. 143 */ 144 private FactoryUtils() { 145 // empty 146 } 147 148 }