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.Converter;
21 import net.sf.morph.transform.DecoratedConverter;
22 import net.sf.morph.transform.ExplicitTransformer;
23 import net.sf.morph.transform.transformers.BaseTransformer;
24 import net.sf.morph.util.TransformerUtils;
25
26 /**
27 * Decorates any Converter so that it implements
28 * {@link net.sf.morph.transform.DecoratedConverter}. Example usage:
29 *
30 * <pre>
31 * Converter myConverter = new MyConverter();
32 * DecoratedConverter decoratedConverter = new DecoratedConverter(myConverter);
33 *
34 * // now use decoratedConverter instead of myConverter
35 * StringBuffer buffer = decoratedConverter.convert(StringBuffer.class, "no locale needed");
36 * </pre>
37 *
38 * @author Matt Sgarlata
39 * @since Dec 5, 2004
40 */
41 public class ConverterDecorator extends BaseTransformer implements DecoratedConverter,
42 ExplicitTransformer {
43
44 private Converter nestedConverter;
45 private Locale defaultLocale;
46
47 /**
48 * Create a new ConverterDecorator.
49 */
50 public ConverterDecorator() {
51 super();
52 }
53
54 /**
55 * Create a new ConverterDecorator.
56 * @param converter
57 */
58 public ConverterDecorator(Converter converter) {
59 this(converter, null);
60 }
61
62 /**
63 * Create a new ConverterDecorator.
64 * @param converter
65 * @param defaultLocale
66 */
67 public ConverterDecorator(Converter converter, Locale defaultLocale) {
68 setNestedConverter(converter);
69 setDefaultLocale(defaultLocale);
70 }
71
72 /**
73 * @return Returns the converter.
74 */
75 public Converter getNestedConverter() {
76 return nestedConverter;
77 }
78
79 /**
80 * @param converter The converter to set.
81 */
82 public void setNestedConverter(Converter converter) {
83 this.nestedConverter = converter;
84 }
85
86 /**
87 * Get the defaultLocale.
88 * @return Locale
89 */
90 public Locale getDefaultLocale() {
91 return defaultLocale;
92 }
93
94 /**
95 * Set the defaultLocale.
96 * @param defaultLocale the Locale to set
97 */
98 public void setDefaultLocale(Locale defaultLocale) {
99 this.defaultLocale = defaultLocale;
100 }
101
102 /**
103 * {@inheritDoc}
104 * @see net.sf.morph.transform.transformers.BaseTransformer#convertImpl(java.lang.Class, java.lang.Object, java.util.Locale)
105 */
106 protected Object convertImpl(Class destinationClass, Object source, Locale locale)
107 throws Exception {
108 return getNestedConverter().convert(destinationClass, source, locale);
109 }
110
111 /**
112 * {@inheritDoc}
113 * @see net.sf.morph.transform.transformers.BaseTransformer#setSourceClasses(java.lang.Class[])
114 */
115 public synchronized void setSourceClasses(Class[] sourceClasses) {
116 super.setSourceClasses(sourceClasses);
117 }
118
119 /**
120 * {@inheritDoc}
121 * @see net.sf.morph.transform.transformers.BaseTransformer#getSourceClassesImpl()
122 */
123 protected Class[] getSourceClassesImpl() {
124 return getNestedConverter().getSourceClasses();
125 }
126
127 /**
128 * {@inheritDoc}
129 * @see net.sf.morph.transform.transformers.BaseTransformer#setDestinationClasses(java.lang.Class[])
130 */
131 public synchronized void setDestinationClasses(Class[] destinationClasses) {
132 super.setDestinationClasses(destinationClasses);
133 }
134
135 /**
136 * {@inheritDoc}
137 * @see net.sf.morph.transform.transformers.BaseTransformer#getDestinationClassesImpl()
138 */
139 protected Class[] getDestinationClassesImpl() {
140 return getNestedConverter().getDestinationClasses();
141 }
142
143 /**
144 * {@inheritDoc}
145 * @see net.sf.morph.transform.transformers.BaseTransformer#isAutomaticallyHandlingNulls()
146 */
147 protected boolean isAutomaticallyHandlingNulls() {
148 return false;
149 }
150
151 /**
152 * {@inheritDoc}
153 * @see net.sf.morph.transform.transformers.BaseTransformer#isWrappingRuntimeExceptions()
154 */
155 protected boolean isWrappingRuntimeExceptions() {
156
157
158 return false;
159 }
160
161 /**
162 * {@inheritDoc}
163 */
164 protected boolean isTransformableImpl(Class destinationType, Class sourceType)
165 throws Exception {
166 return TransformerUtils.isImplicitlyTransformable(this, destinationType,
167 sourceType)
168 && TransformerUtils.isTransformable(getNestedTransformer(),
169 destinationType, sourceType);
170 }
171
172 /**
173 * {@inheritDoc}
174 */
175 protected Locale getLocale() {
176 Locale locale = getDefaultLocale();
177 return locale == null ? super.getLocale() : locale;
178 }
179 }