1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.integration.commons.collections;
17
18 import java.util.Iterator;
19 import java.util.Locale;
20 import java.util.Map;
21
22 import net.sf.composite.util.ObjectUtils;
23 import net.sf.morph.Defaults;
24 import net.sf.morph.transform.DecoratedConverter;
25
26 import org.apache.commons.collections.Transformer;
27
28 /**
29 * Adapts a Morph DecoratedConverter to the org.apache.commons.collections.Transformer interface.
30 * @author mbenson
31 * @since Morph 1.1
32 */
33 public class DecoratedConverterToTransformerAdapter implements Transformer {
34 private Class destinationClass;
35 private Map destinationClassMap;
36 private DecoratedConverter delegate;
37 private Locale locale;
38
39 /**
40 * Create a new DecoratedConverterToTransformerAdapterTestCase.
41 */
42 public DecoratedConverterToTransformerAdapter() {
43 }
44
45 /**
46 * Create a new DecoratedConverterToTransformerAdapterTestCase.
47 * @param delegate
48 */
49 public DecoratedConverterToTransformerAdapter(DecoratedConverter delegate) {
50 this();
51 setDelegate(delegate);
52 }
53
54 /**
55 * Create a new DecoratedConverterToTransformerAdapter.
56 * @param delegate
57 * @param destinationClass
58 */
59 public DecoratedConverterToTransformerAdapter(DecoratedConverter delegate, Class destinationClass) {
60 this(delegate);
61 setDestinationClass(destinationClass);
62 }
63
64 /**
65 * Create a new DecoratedConverterToTransformerAdapter.
66 * @param destinationClass
67 */
68 public DecoratedConverterToTransformerAdapter(Class destinationClass) {
69 this();
70 setDestinationClass(destinationClass);
71 }
72
73 /**
74 * {@inheritDoc}
75 * @see org.apache.commons.collections.Transformer#transform(java.lang.Object)
76 */
77 public Object transform(Object source) {
78 return getDelegate().convert(determineDestinationClass(source), source, locale);
79 }
80
81 private Class determineDestinationClass(Object source) {
82 if (!ObjectUtils.isEmpty(destinationClassMap)) {
83 for (Iterator it = destinationClassMap.keySet().iterator(); it.hasNext();) {
84 Class c = (Class) it.next();
85 if (c.isAssignableFrom(source.getClass())) {
86 return (Class) destinationClassMap.get(c);
87 }
88 }
89 }
90 return destinationClass == null ? Object.class : destinationClass;
91 }
92
93 /**
94 * Get the DecoratedConverter delegate.
95 * @return DecoratedConverter
96 */
97 public synchronized DecoratedConverter getDelegate() {
98 if (delegate == null) {
99 setDelegate(Defaults.createConverter());
100 }
101 return delegate;
102 }
103
104 /**
105 * Set the DecoratedConverter delegate.
106 * @param delegate DecoratedConverter
107 */
108 public synchronized void setDelegate(DecoratedConverter delegate) {
109 this.delegate = delegate;
110 }
111
112 /**
113 * Get the Class destinationClass.
114 * @return Class
115 */
116 public Class getDestinationClass() {
117 return destinationClass;
118 }
119
120 /**
121 * Set the Class destinationClass.
122 * @param destinationClass Class
123 */
124 public void setDestinationClass(Class destinationClass) {
125 this.destinationClass = destinationClass;
126 }
127
128 /**
129 * Get the Map destinationClassMap.
130 * @return Map
131 */
132 public Map getDestinationClassMap() {
133 return destinationClassMap;
134 }
135
136 /**
137 * Set the Map destinationClassMap.
138 * @param destinationClassMap Map
139 */
140 public void setDestinationClassMap(Map destinationClassMap) {
141 this.destinationClassMap = destinationClassMap;
142 }
143
144 /**
145 * Get the Locale locale.
146 * @return Locale
147 */
148 public Locale getLocale() {
149 return locale;
150 }
151
152 /**
153 * Set the Locale locale.
154 * @param locale Locale
155 */
156 public void setLocale(Locale locale) {
157 this.locale = locale;
158 }
159 }