001: /*
002: * Copyright 2002-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections;
017:
018: import org.apache.commons.collections.functors.ConstantFactory;
019: import org.apache.commons.collections.functors.InstantiateFactory;
020: import org.apache.commons.collections.functors.ExceptionFactory;
021: import org.apache.commons.collections.functors.PrototypeFactory;
022:
023: /**
024: * <code>FactoryUtils</code> provides reference implementations and utilities
025: * for the Factory functor interface. The supplied factories are:
026: * <ul>
027: * <li>Prototype - clones a specified object
028: * <li>Reflection - creates objects using reflection
029: * <li>Constant - always returns the same object
030: * <li>Null - always returns null
031: * <li>Exception - always throws an exception
032: * </ul>
033: * All the supplied factories are Serializable.
034: *
035: * @since Commons Collections 3.0
036: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
037: *
038: * @author Stephen Colebourne
039: */
040: public class FactoryUtils {
041:
042: /**
043: * This class is not normally instantiated.
044: */
045: public FactoryUtils() {
046: super ();
047: }
048:
049: /**
050: * Gets a Factory that always throws an exception.
051: * This could be useful during testing as a placeholder.
052: *
053: * @see org.apache.commons.collections.functors.ExceptionFactory
054: *
055: * @return the factory
056: */
057: public static Factory exceptionFactory() {
058: return ExceptionFactory.INSTANCE;
059: }
060:
061: /**
062: * Gets a Factory that will return null each time the factory is used.
063: * This could be useful during testing as a placeholder.
064: *
065: * @see org.apache.commons.collections.functors.ConstantFactory
066: *
067: * @return the factory
068: */
069: public static Factory nullFactory() {
070: return ConstantFactory.NULL_INSTANCE;
071: }
072:
073: /**
074: * Creates a Factory that will return the same object each time the factory
075: * is used. No check is made that the object is immutable. In general, only
076: * immutable objects should use the constant factory. Mutable objects should
077: * use the prototype factory.
078: *
079: * @see org.apache.commons.collections.functors.ConstantFactory
080: *
081: * @param constantToReturn the constant object to return each time in the factory
082: * @return the <code>constant</code> factory.
083: */
084: public static Factory constantFactory(Object constantToReturn) {
085: return ConstantFactory.getInstance(constantToReturn);
086: }
087:
088: /**
089: * Creates a Factory that will return a clone of the same prototype object
090: * each time the factory is used. The prototype will be cloned using one of these
091: * techniques (in order):
092: * <ul>
093: * <li>public clone method
094: * <li>public copy constructor
095: * <li>serialization clone
096: * <ul>
097: *
098: * @see org.apache.commons.collections.functors.PrototypeFactory
099: *
100: * @param prototype the object to clone each time in the factory
101: * @return the <code>prototype</code> factory
102: * @throws IllegalArgumentException if the prototype is null
103: * @throws IllegalArgumentException if the prototype cannot be cloned
104: */
105: public static Factory prototypeFactory(Object prototype) {
106: return PrototypeFactory.getInstance(prototype);
107: }
108:
109: /**
110: * Creates a Factory that can create objects of a specific type using
111: * a no-args constructor.
112: *
113: * @see org.apache.commons.collections.functors.InstantiateFactory
114: *
115: * @param classToInstantiate the Class to instantiate each time in the factory
116: * @return the <code>reflection</code> factory
117: * @throws IllegalArgumentException if the classToInstantiate is null
118: */
119: public static Factory instantiateFactory(Class classToInstantiate) {
120: return InstantiateFactory.getInstance(classToInstantiate, null,
121: null);
122: }
123:
124: /**
125: * Creates a Factory that can create objects of a specific type using
126: * the arguments specified to this method.
127: *
128: * @see org.apache.commons.collections.functors.InstantiateFactory
129: *
130: * @param classToInstantiate the Class to instantiate each time in the factory
131: * @param paramTypes parameter types for the constructor, can be null
132: * @param args the arguments to pass to the constructor, can be null
133: * @return the <code>reflection</code> factory
134: * @throws IllegalArgumentException if the classToInstantiate is null
135: * @throws IllegalArgumentException if the paramTypes and args don't match
136: * @throws IllegalArgumentException if the constructor doesn't exist
137: */
138: public static Factory instantiateFactory(Class classToInstantiate,
139: Class[] paramTypes, Object[] args) {
140: return InstantiateFactory.getInstance(classToInstantiate,
141: paramTypes, args);
142: }
143:
144: }
|