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.transform.converters;
17  
18  import java.util.Locale;
19  
20  import net.sf.morph.transform.DecoratedConverter;
21  import net.sf.morph.transform.transformers.BaseTransformer;
22  import net.sf.morph.util.ClassUtils;
23  
24  /**
25   * Converter that always returns a certain destination object.
26   *
27   * @author mbenson
28   * @since Morph 1.1
29   */
30  public class ConstantConverter extends BaseTransformer implements DecoratedConverter {
31  	private Object result;
32  
33  	/**
34  	 * Create a new {@link ConstantConverter}.
35  	 */
36  	public ConstantConverter() {
37  	}
38  
39  	/**
40  	 * Create a new ConstantConverter.
41  	 * @param result the Object result to set.
42  	 */
43  	public ConstantConverter(Object result) {
44  		setResult(result);
45  	}
46  
47  	/**
48  	 * {@inheritDoc}
49  	 * @see net.sf.morph.transform.transformers.BaseTransformer#getDestinationClassesImpl()
50  	 */
51  	protected Class[] getDestinationClassesImpl() throws Exception {
52  		return new Class[] { ClassUtils.getClass(getResult()) };
53  	}
54  
55  	/**
56  	 * {@inheritDoc}
57  	 * @see net.sf.morph.transform.transformers.BaseTransformer#getSourceClassesImpl()
58  	 */
59  	protected Class[] getSourceClassesImpl() throws Exception {
60  		return ClassUtils.getAllClasses();
61  	}
62  
63  	/**
64  	 * {@inheritDoc}
65  	 * @see net.sf.morph.transform.transformers.BaseTransformer#convertImpl(java.lang.Class, java.lang.Object, java.util.Locale)
66  	 */
67  	protected Object convertImpl(Class destinationClass, Object source, Locale locale)
68  			throws Exception {
69  		return getResult();
70  	}
71  
72  	/**
73  	 * {@inheritDoc}
74  	 * @see net.sf.morph.transform.transformers.BaseTransformer#isAutomaticallyHandlingNulls()
75  	 */
76  	protected boolean isAutomaticallyHandlingNulls() {
77  		return false;
78  	}
79  
80  	/**
81  	 * Get the result.
82  	 * @return Object.
83  	 */
84  	public Object getResult() {
85  		return result;
86  	}
87  
88  	/**
89  	 * Set the result.
90  	 * @param result the Object result to set.
91  	 */
92  	public void setResult(Object result) {
93  		this.result = result;
94  	}
95  }