KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > FactoryUtils


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
18 import org.apache.commons.collections.functors.ConstantFactory;
19 import org.apache.commons.collections.functors.InstantiateFactory;
20 import org.apache.commons.collections.functors.ExceptionFactory;
21 import org.apache.commons.collections.functors.PrototypeFactory;
22
23 /**
24  * <code>FactoryUtils</code> provides reference implementations and utilities
25  * for the Factory functor interface. The supplied factories are:
26  * <ul>
27  * <li>Prototype - clones a specified object
28  * <li>Reflection - creates objects using reflection
29  * <li>Constant - always returns the same object
30  * <li>Null - always returns null
31  * <li>Exception - always throws an exception
32  * </ul>
33  * All the supplied factories are Serializable.
34  *
35  * @since Commons Collections 3.0
36  * @version $Revision: 1.14 $ $Date: 2004/04/14 21:47:47 $
37  *
38  * @author Stephen Colebourne
39  */

40 public class FactoryUtils {
41
42     /**
43      * This class is not normally instantiated.
44      */

45     public FactoryUtils() {
46         super();
47     }
48
49     /**
50      * Gets a Factory that always throws an exception.
51      * This could be useful during testing as a placeholder.
52      *
53      * @see org.apache.commons.collections.functors.ExceptionFactory
54      *
55      * @return the factory
56      */

57     public static Factory exceptionFactory() {
58         return ExceptionFactory.INSTANCE;
59     }
60
61     /**
62      * Gets a Factory that will return null each time the factory is used.
63      * This could be useful during testing as a placeholder.
64      *
65      * @see org.apache.commons.collections.functors.ConstantFactory
66      *
67      * @return the factory
68      */

69     public static Factory nullFactory() {
70         return ConstantFactory.NULL_INSTANCE;
71     }
72
73     /**
74      * Creates a Factory that will return the same object each time the factory
75      * is used. No check is made that the object is immutable. In general, only
76      * immutable objects should use the constant factory. Mutable objects should
77      * use the prototype factory.
78      *
79      * @see org.apache.commons.collections.functors.ConstantFactory
80      *
81      * @param constantToReturn the constant object to return each time in the factory
82      * @return the <code>constant</code> factory.
83      */

84     public static Factory constantFactory(Object JavaDoc constantToReturn) {
85         return ConstantFactory.getInstance(constantToReturn);
86     }
87
88     /**
89      * Creates a Factory that will return a clone of the same prototype object
90      * each time the factory is used. The prototype will be cloned using one of these
91      * techniques (in order):
92      * <ul>
93      * <li>public clone method
94      * <li>public copy constructor
95      * <li>serialization clone
96      * <ul>
97      *
98      * @see org.apache.commons.collections.functors.PrototypeFactory
99      *
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 JavaDoc 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 JavaDoc classToInstantiate) {
120         return InstantiateFactory.getInstance(classToInstantiate, null, null);
121     }
122
123     /**
124      * Creates a Factory that can create objects of a specific type using
125      * the arguments specified to this method.
126      *
127      * @see org.apache.commons.collections.functors.InstantiateFactory
128      *
129      * @param classToInstantiate the Class to instantiate each time in the factory
130      * @param paramTypes parameter types for the constructor, can be null
131      * @param args the arguments to pass to the constructor, can be null
132      * @return the <code>reflection</code> factory
133      * @throws IllegalArgumentException if the classToInstantiate is null
134      * @throws IllegalArgumentException if the paramTypes and args don't match
135      * @throws IllegalArgumentException if the constructor doesn't exist
136      */

137     public static Factory instantiateFactory(Class JavaDoc classToInstantiate, Class JavaDoc[] paramTypes, Object JavaDoc[] args) {
138         return InstantiateFactory.getInstance(classToInstantiate, paramTypes, args);
139     }
140
141 }
142
Popular Tags