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.ExplicitTransformer;
22 import net.sf.morph.transform.TransformationException;
23 import net.sf.morph.transform.transformers.BaseTransformer;
24 import net.sf.morph.util.TransformerUtils;
25
26 /**
27 * Converts objects that are already instances of the destination class by
28 * simply returning the object unchanged.
29 *
30 * @author Matt Sgarlata
31 * @since Dec 31, 2004
32 */
33 public class IdentityConverter extends BaseTransformer implements DecoratedConverter,
34 ExplicitTransformer {
35
36 /** Default source/destination types */
37 public static final Class[] DEFAULT_SOURCE_AND_DESTINATION_TYPES = {
38 Object.class, boolean.class, byte.class, char.class, short.class, int.class,
39 long.class, float.class, double.class, null };
40
41 /**
42 * Create a new IdentityConverter.
43 */
44 public IdentityConverter() {
45 super();
46 }
47
48 /**
49 * Create a new IdentityConverter.
50 * @param sourceAndDestinationClasses
51 */
52 public IdentityConverter(Class[] sourceAndDestinationClasses) {
53 super();
54 setSourceClasses(sourceAndDestinationClasses);
55 setDestinationClasses(sourceAndDestinationClasses);
56 }
57
58 /**
59 * {@inheritDoc}
60 */
61 protected boolean isTransformableImpl(Class destinationType, Class sourceType)
62 throws Exception {
63 if (TransformerUtils.isImplicitlyTransformable(this, destinationType, sourceType)) {
64 if (destinationType == sourceType) {
65 return true;
66 }
67 if (destinationType == null) {
68 return false;
69 }
70 if (sourceType == null) {
71 return !destinationType.isPrimitive();
72 }
73 return destinationType.isAssignableFrom(sourceType);
74 }
75 return false;
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 protected Object convertImpl(Class destinationClass, Object source, Locale locale)
82 throws Exception {
83
84 if (destinationClass.isAssignableFrom(source.getClass())) {
85 return source;
86 }
87 throw new TransformationException(destinationClass, source);
88 }
89
90 /**
91 * {@inheritDoc}
92 */
93 protected boolean isPerformingLogging() {
94
95 return false;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 protected boolean isWrappingRuntimeExceptions() {
102 return true;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 protected Class[] getSourceClassesImpl() throws Exception {
109 return DEFAULT_SOURCE_AND_DESTINATION_TYPES;
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 protected Class[] getDestinationClassesImpl() throws Exception {
116 return DEFAULT_SOURCE_AND_DESTINATION_TYPES;
117 }
118
119 /**
120 * {@inheritDoc}
121 * @see net.sf.morph.transform.transformers.BaseTransformer#setSourceClasses(java.lang.Class[])
122 */
123 public synchronized void setDestinationClasses(Class[] destinationClasses) {
124 super.setDestinationClasses(destinationClasses);
125 }
126
127 /**
128 * {@inheritDoc}
129 * @see net.sf.morph.transform.transformers.BaseTransformer#setSourceClasses(java.lang.Class[])
130 */
131 public synchronized void setSourceClasses(Class[] sourceClasses) {
132 super.setSourceClasses(sourceClasses);
133 }
134
135 }