FactoryUtils.java

  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. import org.apache.commons.collections4.functors.ConstantFactory;
  19. import org.apache.commons.collections4.functors.ExceptionFactory;
  20. import org.apache.commons.collections4.functors.InstantiateFactory;
  21. import org.apache.commons.collections4.functors.PrototypeFactory;

  22. /**
  23.  * {@code FactoryUtils} provides reference implementations and utilities
  24.  * for the Factory functor interface. The supplied factories are:
  25.  * <ul>
  26.  * <li>Prototype - clones a specified object
  27.  * <li>Instantiate - creates objects using reflection
  28.  * <li>Constant - always returns the same object
  29.  * <li>Null - always returns null
  30.  * <li>Exception - always throws an exception
  31.  * </ul>
  32.  * <p>
  33.  * Since v4.1 only factories which are considered to be safe are
  34.  * Serializable. Factories considered to be unsafe for serialization are:
  35.  * </p>
  36.  * <ul>
  37.  * <li>Prototype
  38.  * <li>Instantiate
  39.  * </ul>
  40.  *
  41.  * @since 3.0
  42.  */
  43. public class FactoryUtils {

  44.     /**
  45.      * Creates a Factory that will return the same object each time the factory
  46.      * is used. No check is made that the object is immutable. In general, only
  47.      * immutable objects should use the constant factory. Mutable objects should
  48.      * use the prototype factory.
  49.      *
  50.      * @see org.apache.commons.collections4.functors.ConstantFactory
  51.      * @param <T> the type that the factory creates
  52.      * @param constantToReturn  the constant object to return each time in the factory
  53.      * @return the {@code constant} factory.
  54.      */
  55.     public static <T> Factory<T> constantFactory(final T constantToReturn) {
  56.         return ConstantFactory.constantFactory(constantToReturn);
  57.     }

  58.     /**
  59.      * Gets a Factory that always throws an exception.
  60.      * This could be useful during testing as a placeholder.
  61.      *
  62.      * @see org.apache.commons.collections4.functors.ExceptionFactory
  63.      * @param <T> the type that the factory creates
  64.      * @return the factory
  65.      */
  66.     public static <T> Factory<T> exceptionFactory() {
  67.         return ExceptionFactory.<T>exceptionFactory();
  68.     }

  69.     /**
  70.      * Creates a Factory that can create objects of a specific type using
  71.      * a no-args constructor.
  72.      *
  73.      * @see org.apache.commons.collections4.functors.InstantiateFactory
  74.      * @param <T> the type that the factory creates
  75.      * @param classToInstantiate  the Class to instantiate each time in the factory
  76.      * @return the {@code reflection} factory
  77.      * @throws NullPointerException if the classToInstantiate is null
  78.      */
  79.     public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate) {
  80.         return InstantiateFactory.instantiateFactory(classToInstantiate, null, null);
  81.     }

  82.     /**
  83.      * Creates a Factory that can create objects of a specific type using
  84.      * the arguments specified to this method.
  85.      *
  86.      * @see org.apache.commons.collections4.functors.InstantiateFactory
  87.      * @param <T> the type that the factory creates
  88.      * @param classToInstantiate  the Class to instantiate each time in the factory
  89.      * @param paramTypes  parameter types for the constructor, can be null
  90.      * @param args  the arguments to pass to the constructor, can be null
  91.      * @return the {@code reflection} factory
  92.      * @throws NullPointerException if the classToInstantiate is null
  93.      * @throws IllegalArgumentException if the paramTypes and args don't match
  94.      * @throws IllegalArgumentException if the constructor doesn't exist
  95.      */
  96.     public static <T> Factory<T> instantiateFactory(final Class<T> classToInstantiate, final Class<?>[] paramTypes,
  97.                                                     final Object[] args) {
  98.         return InstantiateFactory.instantiateFactory(classToInstantiate, paramTypes, args);
  99.     }

  100.     /**
  101.      * Gets a Factory that will return null each time the factory is used.
  102.      * This could be useful during testing as a placeholder.
  103.      *
  104.      * @see org.apache.commons.collections4.functors.ConstantFactory
  105.      * @param <T> the "type" of null object the factory should return.
  106.      * @return the factory
  107.      */
  108.     public static <T> Factory<T> nullFactory() {
  109.         return ConstantFactory.<T>constantFactory(null);
  110.     }

  111.     /**
  112.      * Creates a Factory that will return a clone of the same prototype object
  113.      * each time the factory is used. The prototype will be cloned using one of these
  114.      * techniques (in order):
  115.      *
  116.      * <ul>
  117.      * <li>public clone method</li>
  118.      * <li>public copy constructor</li>
  119.      * <li>serialization clone</li>
  120.      * </ul>
  121.      *
  122.      * @see org.apache.commons.collections4.functors.PrototypeFactory
  123.      * @param <T> the type that the factory creates
  124.      * @param prototype  the object to clone each time in the factory
  125.      * @return the {@code prototype} factory, or a {@link ConstantFactory#NULL_INSTANCE} if
  126.      * the {@code prototype} is {@code null}
  127.      * @throws IllegalArgumentException if the prototype cannot be cloned
  128.      */
  129.     public static <T> Factory<T> prototypeFactory(final T prototype) {
  130.         return PrototypeFactory.<T>prototypeFactory(prototype);
  131.     }

  132.     /**
  133.      * Don't allow instances.
  134.      */
  135.     private FactoryUtils() {
  136.         // empty
  137.     }

  138. }