1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }