01: /*
02: * Copyright 2001-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.functors;
17:
18: import java.io.Serializable;
19:
20: import org.apache.commons.collections.Factory;
21: import org.apache.commons.collections.Transformer;
22:
23: /**
24: * Transformer implementation that calls a Factory and returns the result.
25: *
26: * @since Commons Collections 3.0
27: * @version $Revision: 348444 $ $Date: 2005-11-23 14:06:56 +0000 (Wed, 23 Nov 2005) $
28: *
29: * @author Stephen Colebourne
30: */
31: public class FactoryTransformer implements Transformer, Serializable {
32:
33: /** Serial version UID */
34: private static final long serialVersionUID = -6817674502475353160L;
35:
36: /** The factory to wrap */
37: private final Factory iFactory;
38:
39: /**
40: * Factory method that performs validation.
41: *
42: * @param factory the factory to call, not null
43: * @return the <code>factory</code> transformer
44: * @throws IllegalArgumentException if the factory is null
45: */
46: public static Transformer getInstance(Factory factory) {
47: if (factory == null) {
48: throw new IllegalArgumentException(
49: "Factory must not be null");
50: }
51: return new FactoryTransformer(factory);
52: }
53:
54: /**
55: * Constructor that performs no validation.
56: * Use <code>getInstance</code> if you want that.
57: *
58: * @param factory the factory to call, not null
59: */
60: public FactoryTransformer(Factory factory) {
61: super ();
62: iFactory = factory;
63: }
64:
65: /**
66: * Transforms the input by ignoring the input and returning the result of
67: * calling the decorated factory.
68: *
69: * @param input the input object to transform
70: * @return the transformed result
71: */
72: public Object transform(Object input) {
73: return iFactory.create();
74: }
75:
76: /**
77: * Gets the factory.
78: *
79: * @return the factory
80: * @since Commons Collections 3.1
81: */
82: public Factory getFactory() {
83: return iFactory;
84: }
85:
86: }
|