View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.integration.commons.collections;
17  
18  import java.util.Locale;
19  
20  import org.apache.commons.collections.Transformer;
21  
22  import net.sf.morph.transform.DecoratedConverter;
23  import net.sf.morph.transform.transformers.BaseTransformer;
24  import net.sf.morph.util.Assert;
25  import net.sf.morph.util.ClassUtils;
26  
27  /**
28   * Adapt a org.apache.commons.collections.Transformer to our DecoratedConverter interface.
29   *
30   * @author mbenson
31   * @since Morph 1.1
32   */
33  public class TransformerToDecoratedConverterAdapter extends BaseTransformer implements
34  		DecoratedConverter {
35  
36  	private Transformer delegate;
37  
38  	/**
39  	 * Create a new TransformerToDecoratedConverterAdapter.
40  	 */
41  	public TransformerToDecoratedConverterAdapter() {
42  	}
43  
44  	/**
45  	 * Create a new TransformerToDecoratedConverterAdapter.
46  	 * @param delegate
47  	 */
48  	public TransformerToDecoratedConverterAdapter(Transformer delegate) {
49  		setDelegate(delegate);
50  	}
51  
52  	/**
53  	 * {@inheritDoc}
54  	 * @see net.sf.morph.transform.transformers.BaseTransformer#convertImpl(java.lang.Class, java.lang.Object, java.util.Locale)
55  	 */
56  	protected Object convertImpl(Class destinationClass, Object source, Locale locale)
57  			throws Exception {
58  		Assert.notNull(delegate, "delegate");
59  		return delegate.transform(source);
60  	}
61  
62  	/**
63  	 * {@inheritDoc}
64  	 * @see net.sf.morph.transform.transformers.BaseTransformer#setDestinationClasses(java.lang.Class[])
65  	 */
66  	public synchronized void setDestinationClasses(Class[] destinationClasses) {
67  		super.setDestinationClasses(destinationClasses);
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 * @see net.sf.morph.transform.transformers.BaseTransformer#getDestinationClassesImpl()
73  	 */
74  	protected Class[] getDestinationClassesImpl() throws Exception {
75  		return ClassUtils.getAllClasses();
76  	}
77  
78  	/**
79  	 * {@inheritDoc}
80  	 * @see net.sf.morph.transform.transformers.BaseTransformer#setSourceClasses(java.lang.Class[])
81  	 */
82  	public synchronized void setSourceClasses(Class[] sourceClasses) {
83  		super.setSourceClasses(sourceClasses);
84  	}
85  
86  	/**
87  	 * {@inheritDoc}
88  	 * @see net.sf.morph.transform.transformers.BaseTransformer#getSourceClassesImpl()
89  	 */
90  	protected Class[] getSourceClassesImpl() throws Exception {
91  		return ClassUtils.getAllClasses();
92  	}
93  
94  	/**
95  	 * Get the Transformer delegate.
96  	 * @return Transformer
97  	 */
98  	public Transformer getDelegate() {
99  		return delegate;
100 	}
101 
102 	/**
103 	 * Set the Transformer delegate.
104 	 * @param delegate Transformer
105 	 */
106 	public void setDelegate(Transformer delegate) {
107 		this.delegate = delegate;
108 	}
109 
110 	/**
111 	 * {@inheritDoc}
112 	 * @see net.sf.morph.transform.transformers.BaseTransformer#isAutomaticallyHandlingNulls()
113 	 */
114 	protected boolean isAutomaticallyHandlingNulls() {
115 		return false;
116 	}
117 
118 	/**
119 	 * {@inheritDoc}
120 	 * @see net.sf.morph.transform.transformers.BaseTransformer#isWrappingRuntimeExceptions()
121 	 */
122 	protected boolean isWrappingRuntimeExceptions() {
123 		return false;
124 	}
125 }