1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ezmorph;
18
19 import java.io.Serializable;
20 import java.lang.reflect.Method;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import net.sf.ezmorph.object.IdentityObjectMorpher;
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class MorpherRegistry implements Serializable
42 {
43 private static final long serialVersionUID = -3894767123320768419L;
44 private Map morphers = new HashMap();
45
46 public MorpherRegistry()
47 {
48
49 }
50
51
52
53
54 public synchronized void clear()
55 {
56 morphers.clear();
57 }
58
59
60
61
62
63
64 public synchronized void clear( Class type )
65 {
66 List registered = (List) morphers.get( type );
67 if( registered != null ){
68 morphers.remove( type );
69 }
70 }
71
72
73
74
75
76
77
78
79 public synchronized void deregisterMorpher( Morpher morpher )
80 {
81 List registered = (List) morphers.get( morpher.morphsTo() );
82 if( registered != null && !registered.isEmpty() ){
83 registered.remove( morpher );
84 if( registered.isEmpty() ){
85 morphers.remove( morpher.morphsTo() );
86 }
87 }
88 }
89
90
91
92
93
94
95
96
97 public synchronized Morpher getMorpherFor( Class clazz )
98 {
99 List registered = (List) morphers.get( clazz );
100 if( registered == null || registered.isEmpty() ){
101
102 return IdentityObjectMorpher.getInstance();
103 }else{
104 return (Morpher) registered.get( 0 );
105 }
106 }
107
108
109
110
111
112
113
114
115
116 public synchronized Morpher[] getMorphersFor( Class clazz )
117 {
118 List registered = (List) morphers.get( clazz );
119 if( registered == null || registered.isEmpty() ){
120
121 return new Morpher[] { IdentityObjectMorpher.getInstance() };
122 }else{
123 Morpher[] morphs = new Morpher[registered.size()];
124 int k = 0;
125 for( Iterator i = registered.iterator(); i.hasNext(); ){
126 morphs[k++] = (Morpher) i.next();
127 }
128 return morphs;
129 }
130 }
131
132
133
134
135
136
137
138
139
140
141
142 public Object morph( Class target, Object value )
143 {
144 if( value == null ){
145
146
147 Morpher morpher = getMorpherFor( target );
148 if( morpher instanceof ObjectMorpher ){
149 return ((ObjectMorpher) morpher).morph( value );
150 }else{
151 try{
152 Method morphMethod = morpher.getClass()
153 .getDeclaredMethod( "morph", new Class[] { Object.class } );
154 return morphMethod.invoke( morpher, new Object[] { value } );
155 }
156 catch( Exception e ){
157 throw new MorphException( e );
158 }
159 }
160 }
161
162 Morpher[] morphers = getMorphersFor( target );
163 for( int i = 0; i < morphers.length; i++ ){
164 Morpher morpher = morphers[i];
165 if( morpher.supports( value.getClass() ) ){
166 if( morpher instanceof ObjectMorpher ){
167 return ((ObjectMorpher) morpher).morph( value );
168 }else{
169 try{
170 Method morphMethod = morpher.getClass()
171 .getDeclaredMethod( "morph", new Class[] { Object.class } );
172 return morphMethod.invoke( morpher, new Object[] { value } );
173 }
174 catch( Exception e ){
175 throw new MorphException( e );
176 }
177 }
178 }
179 }
180 return value;
181 }
182
183
184
185
186
187
188
189
190
191 public void registerMorpher( Morpher morpher )
192 {
193 registerMorpher( morpher, false );
194 }
195
196
197
198
199
200
201
202
203
204
205
206 public synchronized void registerMorpher( Morpher morpher, boolean override )
207 {
208 List registered = (List) morphers.get( morpher.morphsTo() );
209 if( override || registered == null ){
210 registered = new ArrayList();
211 morphers.put( morpher.morphsTo(), registered );
212 }
213 if( !registered.contains( morpher ) ){
214 registered.add( morpher );
215 }
216 }
217 }