    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;
    018
    019import org.apache.commons.collections4.functors.ConstantFactory;
    020import org.apache.commons.collections4.functors.ExceptionFactory;
    021import org.apache.commons.collections4.functors.InstantiateFactory;
    022import org.apache.commons.collections4.functors.PrototypeFactory;
    023
    024/**
    025 * {@code FactoryUtils} provides reference implementations and utilities
    026 * for the Factory functor interface. The supplied factories are:
    027 * <ul>
    028 * <li>Prototype - clones a specified object</li>
    029 * <li>Instantiate - creates objects using reflection</li>
    030 * <li>Constant - always returns the same object</li>
    031 * <li>Null - always returns null</li>
    032 * <li>Exception - always throws an exception</li>
    033 * </ul>
    034 * <p>
    035 * Since v4.1 only factories which are considered to be safe are
    036 * Serializable. Factories considered to be unsafe for serialization are:
    037 * </p>
    038 * <ul>
    039 * <li>Prototype</li>
    040 * <li>Instantiate</li>
    041 * </ul>
    042 *
    043 * @since 3.0
    044 */
    045public class FactoryUtils {
    046
    047    /**
    048     * Creates a Factory that will return the same object each time the factory
    049     * is used. No check is made that the object is immutable. In general, only
    050     * immutable objects should use the constant factory. Mutable objects should
    051     * use the prototype factory.
    052     *
    053     * @see org.apache.commons.collections4.functors.ConstantFactory
    054     * @param <T> The type that the factory creates
    055     * @param constantToReturn  The constant object to return each time in the factory
    056     * @return The {@code constant} factory.
    057     */
    058    public static <T> Factory<T> constantFactory(final T constantToReturn) {
    059        return ConstantFactory.constantFactory(constantToReturn);
    060    }
    061
    062    /**
    063     * Gets a Factory that always throws an exception.
    064     * This could be useful during testing as a placeholder.
    065     *
    066     * @see org.apache.commons.collections4.functors.ExceptionFactory
    067     * @param <T> The type that the factory creates
    068     * @return The factory
    069     */
    070    public static <T> Factory<T> exceptionFactory() {
    071        return ExceptionFactory.<T>exceptionFactory();
    072    }
    073
    074    /**
    075     * Creates a Factory that can create objects of a specific type using
    076     * a no-args constructor.
    077     *
    078     * @see org.apache.commons.collections4.functors.InstantiateFactory
    079     * @param <T> The type that the factory creates
    080     * @param classToInstantiate  The Class to instantiate each time in the factory
    081     * @return The {@code reflection} factory
    082     * @throws NullPointerException if the classToInstantiate is null
    083     */
    084    public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate) {
    085        return InstantiateFactory.instantiateFactory(classToInstantiate, null, null);
    086    }
    087
    088    /**
    089     * Creates a Factory that can create objects of a specific type using
    090     * the arguments specified to this method.
    091     *
    092     * @see org.apache.commons.collections4.functors.InstantiateFactory
    093     * @param <T> The type that the factory creates
    094     * @param classToInstantiate  The Class to instantiate each time in the factory
    095     * @param paramTypes  parameter types for the constructor, can be null
    096     * @param args  The arguments to pass to the constructor, can be null
    097     * @return The {@code reflection} factory
    098     * @throws NullPointerException if the classToInstantiate is null
    099     * @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}