18
18
import com .thoughtworks .xstream .core .util .PrioritizedList ;
19
19
import com .thoughtworks .xstream .mapper .Mapper ;
20
20
21
- import java .util .Collections ;
22
- import java .util .HashMap ;
23
21
import java .util .Iterator ;
24
22
import java .util .Map ;
25
23
import java .util .WeakHashMap ;
34
32
public class DefaultConverterLookup implements ConverterLookup , ConverterRegistry , Caching {
35
33
36
34
private final PrioritizedList converters = new PrioritizedList ();
37
- private transient Map typeToConverterMap = Collections . synchronizedMap ( new WeakHashMap ()) ;
35
+ private transient Map typeToConverterMap ;
38
36
39
37
public DefaultConverterLookup () {
40
38
}
@@ -45,7 +43,10 @@ public DefaultConverterLookup() {
45
43
public DefaultConverterLookup (Mapper mapper ) {
46
44
}
47
45
48
- public Converter lookupConverterForType (Class type ) {
46
+ public synchronized Converter lookupConverterForType (Class type ) {
47
+ if (typeToConverterMap == null ) {
48
+ typeToConverterMap = new WeakHashMap ();
49
+ }
49
50
Converter cachedConverter = (Converter ) typeToConverterMap .get (type );
50
51
if (cachedConverter != null ) {
51
52
return cachedConverter ;
@@ -61,7 +62,10 @@ public Converter lookupConverterForType(Class type) {
61
62
throw new ConversionException ("No converter specified for " + type );
62
63
}
63
64
64
- public void registerConverter (Converter converter , int priority ) {
65
+ public synchronized void registerConverter (Converter converter , int priority ) {
66
+ if (typeToConverterMap == null ) {
67
+ typeToConverterMap = new WeakHashMap ();
68
+ }
65
69
converters .add (converter , priority );
66
70
for (Iterator iter = typeToConverterMap .keySet ().iterator (); iter .hasNext ();) {
67
71
Class type = (Class ) iter .next ();
@@ -71,7 +75,10 @@ public void registerConverter(Converter converter, int priority) {
71
75
}
72
76
}
73
77
74
- public void flushCache () {
78
+ public synchronized void flushCache () {
79
+ if (typeToConverterMap == null ) {
80
+ typeToConverterMap = new WeakHashMap ();
81
+ }
75
82
typeToConverterMap .clear ();
76
83
Iterator iterator = converters .iterator ();
77
84
while (iterator .hasNext ()) {
@@ -82,8 +89,4 @@ public void flushCache() {
82
89
}
83
90
}
84
91
85
- private Object readResolve () {
86
- typeToConverterMap = Collections .synchronizedMap (new HashMap ());
87
- return this ;
88
- }
89
92
}
0 commit comments