1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections;
  17. import org.apache.commons.collections.functors.ConstantFactory;
  18. import org.apache.commons.collections.functors.InstantiateFactory;
  19. import org.apache.commons.collections.functors.ExceptionFactory;
  20. import org.apache.commons.collections.functors.PrototypeFactory;
  21. /**
  22. * <code>FactoryUtils</code> provides reference implementations and utilities
  23. * for the Factory functor interface. The supplied factories are:
  24. * <ul>
  25. * <li>Prototype - clones a specified object
  26. * <li>Reflection - creates objects using reflection
  27. * <li>Constant - always returns the same object
  28. * <li>Null - always returns null
  29. * <li>Exception - always throws an exception
  30. * </ul>
  31. * All the supplied factories are Serializable.
  32. *
  33. * @since Commons Collections 3.0
  34. * @version $Revision: 1.14 $ $Date: 2004/04/14 21:47:47 $
  35. *
  36. * @author Stephen Colebourne
  37. */
  38. public class FactoryUtils {
  39. /**
  40. * This class is not normally instantiated.
  41. */
  42. public FactoryUtils() {
  43. super();
  44. }
  45. /**
  46. * Gets a Factory that always throws an exception.
  47. * This could be useful during testing as a placeholder.
  48. *
  49. * @see org.apache.commons.collections.functors.ExceptionFactory
  50. *
  51. * @return the factory
  52. */
  53. public static Factory exceptionFactory() {
  54. return ExceptionFactory.INSTANCE;
  55. }
  56. /**
  57. * Gets a Factory that will return null each time the factory is used.
  58. * This could be useful during testing as a placeholder.
  59. *
  60. * @see org.apache.commons.collections.functors.ConstantFactory
  61. *
  62. * @return the factory
  63. */
  64. public static Factory nullFactory() {
  65. return ConstantFactory.NULL_INSTANCE;
  66. }
  67. /**
  68. * Creates a Factory that will return the same object each time the factory
  69. * is used. No check is made that the object is immutable. In general, only
  70. * immutable objects should use the constant factory. Mutable objects should
  71. * use the prototype factory.
  72. *
  73. * @see org.apache.commons.collections.functors.ConstantFactory
  74. *
  75. * @param constantToReturn the constant object to return each time in the factory
  76. * @return the <code>constant</code> factory.
  77. */
  78. public static Factory constantFactory(Object constantToReturn) {
  79. return ConstantFactory.getInstance(constantToReturn);
  80. }
  81. /**
  82. * Creates a Factory that will return a clone of the same prototype object
  83. * each time the factory is used. The prototype will be cloned using one of these
  84. * techniques (in order):
  85. * <ul>
  86. * <li>public clone method
  87. * <li>public copy constructor
  88. * <li>serialization clone
  89. * <ul>
  90. *
  91. * @see org.apache.commons.collections.functors.PrototypeFactory
  92. *
  93. * @param prototype the object to clone each time in the factory
  94. * @return the <code>prototype</code> factory
  95. * @throws IllegalArgumentException if the prototype is null
  96. * @throws IllegalArgumentException if the prototype cannot be cloned
  97. */
  98. public static Factory prototypeFactory(Object prototype) {
  99. return PrototypeFactory.getInstance(prototype);
  100. }
  101. /**
  102. * Creates a Factory that can create objects of a specific type using
  103. * a no-args constructor.
  104. *
  105. * @see org.apache.commons.collections.functors.InstantiateFactory
  106. *
  107. * @param classToInstantiate the Class to instantiate each time in the factory
  108. * @return the <code>reflection</code> factory
  109. * @throws IllegalArgumentException if the classToInstantiate is null
  110. */
  111. public static Factory instantiateFactory(Class classToInstantiate) {
  112. return InstantiateFactory.getInstance(classToInstantiate, null, null);
  113. }
  114. /**
  115. * Creates a Factory that can create objects of a specific type using
  116. * the arguments specified to this method.
  117. *
  118. * @see org.apache.commons.collections.functors.InstantiateFactory
  119. *
  120. * @param classToInstantiate the Class to instantiate each time in the factory
  121. * @param paramTypes parameter types for the constructor, can be null
  122. * @param args the arguments to pass to the constructor, can be null
  123. * @return the <code>reflection</code> factory
  124. * @throws IllegalArgumentException if the classToInstantiate is null
  125. * @throws IllegalArgumentException if the paramTypes and args don't match
  126. * @throws IllegalArgumentException if the constructor doesn't exist
  127. */
  128. public static Factory instantiateFactory(Class classToInstantiate, Class[] paramTypes, Object[] args) {
  129. return InstantiateFactory.getInstance(classToInstantiate, paramTypes, args);
  130. }
  131. }