    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 *      http://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
    029 * <li>Instantiate - creates objects using reflection
    030 * <li>Constant - always returns the same object
    031 * <li>Null - always returns null
    032 * <li>Exception - always throws an exception
    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 * <ul>
    038 * <li>Prototype
    039 * <li>Instantiate
    040 * </ul>
    041 *
    042 * @since 3.0
    043 */
    044public class FactoryUtils {
    045
    046    /**
    047     * Creates a Factory that will return the same object each time the factory
    048     * is used. No check is made that the object is immutable. In general, only
    049     * immutable objects should use the constant factory. Mutable objects should
    050     * use the prototype factory.
    051     *
    052     * @see org.apache.commons.collections4.functors.ConstantFactory
    053     *
    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     *
    068     * @param <T> the type that the factory creates
    069     * @return the factory
    070     */
    071    public static <T> Factory<T> exceptionFactory() {
    072        return ExceptionFactory.<T>exceptionFactory();
    073    }
    074
    075    /**
    076     * Creates a Factory that can create objects of a specific type using
    077     * a no-args constructor.
    078     *
    079     * @see org.apache.commons.collections4.functors.InstantiateFactory
    080     *
    081     * @param <T> the type that the factory creates
    082     * @param classToInstantiate  the Class to instantiate each time in the factory
    083     * @return the {@code reflection} factory
    084     * @throws NullPointerException if the classToInstantiate is null
    085     */
    086    public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate) {
    087        return InstantiateFactory.instantiateFactory(classToInstantiate, null, null);
    088    }
    089
    090    /**
    091     * Creates a Factory that can create objects of a specific type using
    092     * the arguments specified to this method.
    093     *
    094     * @see org.apache.commons.collections4.functors.InstantiateFactory
    095     *
    096     * @param <T> the type that the factory creates
    097     * @param classToInstantiate  the Class to instantiate each time in the factory
    098     * @param paramTypes  parameter types for the constructor, can be null
    099     * @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}