1
1
package com .fasterxml .jackson .databind .jsontype .impl ;
2
2
3
3
import java .util .*;
4
+ import java .util .concurrent .ConcurrentHashMap ;
4
5
5
6
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
6
7
12
13
public class TypeNameIdResolver extends TypeIdResolverBase
13
14
{
14
15
/**
15
- * Mappings from class name to type id, used for serialization
16
+ * Mappings from class name to type id, used for serialization.
17
+ *<p>
18
+ * Since lazily constructed will require synchronization (either internal
19
+ * by type, or external)
16
20
*/
17
- protected final Map <String , String > _typeToId ;
21
+ protected final ConcurrentHashMap <String , String > _typeToId ;
18
22
19
23
/**
20
- * Mappings from type id to JavaType, used for deserialization
24
+ * Mappings from type id to JavaType, used for deserialization.
25
+ *<p>
26
+ * Eagerly constructed, not modified, can use regular unsynchronized {@link Map}.
21
27
*/
22
28
protected final Map <String , JavaType > _idToType ;
23
29
24
30
protected TypeNameIdResolver (JavaType baseType ,
25
- Map <String , String > typeToId , Map <String , JavaType > idToType )
31
+ ConcurrentHashMap <String , String > typeToId ,
32
+ HashMap <String , JavaType > idToType )
26
33
{
27
34
super (baseType );
28
35
_typeToId = typeToId ;
29
36
_idToType = idToType ;
30
37
}
31
-
38
+
32
39
public static TypeNameIdResolver construct (MapperConfig <?> config , JavaType baseType ,
33
40
Collection <NamedType > subtypes , boolean forSer , boolean forDeser )
34
41
{
35
42
// sanity check
36
43
if (forSer == forDeser ) throw new IllegalArgumentException ();
37
- Map <String , String > typeToId = null ;
38
- Map <String , JavaType > idToType = null ;
44
+
45
+ final ConcurrentHashMap <String , String > typeToId ;
46
+ final HashMap <String , JavaType > idToType ;
39
47
40
48
if (forSer ) {
41
- typeToId = new HashMap <String , String >();
42
- }
43
- if (forDeser ) {
44
- idToType = new HashMap <String , JavaType >();
49
+ // Only need Class-to-id for serialization; but synchronized since may be
50
+ // lazily built (if adding type-id-mappings dynamically)
51
+ typeToId = new ConcurrentHashMap <>();
52
+ idToType = null ;
53
+ } else {
54
+ idToType = new HashMap <>();
45
55
// 14-Apr-2016, tatu: Apparently needed for special case of `defaultImpl`;
46
- // see [databind#1198] for details.
47
- typeToId = new TreeMap <String , String >();
56
+ // see [databind#1198] for details: but essentially we only need room
57
+ // for a single value.
58
+ typeToId = new ConcurrentHashMap <>(4 );
48
59
}
49
60
if (subtypes != null ) {
50
61
for (NamedType t : subtypes ) {
51
- /* no name? Need to figure out default; for now, let's just
52
- * use non-qualified class name
53
- */
62
+ // no name? Need to figure out default; for now, let's just
63
+ // use non-qualified class name
54
64
Class <?> cls = t .getType ();
55
65
String id = t .hasName () ? t .getName () : _defaultTypeId (cls );
56
66
if (forSer ) {
@@ -98,11 +108,7 @@ protected String idFromClass(DatabindContext ctxt, Class<?> cls)
98
108
// cls = _typeFactory.constructType(cls).getRawClass();
99
109
100
110
final String key = cls .getName ();
101
- String name ;
102
-
103
- synchronized (_typeToId ) {
104
- name = _typeToId .get (key );
105
- }
111
+ String name = _typeToId .get (key );
106
112
107
113
if (name == null ) {
108
114
// 24-Feb-2011, tatu: As per [JACKSON-498], may need to dynamically look up name
@@ -115,9 +121,7 @@ protected String idFromClass(DatabindContext ctxt, Class<?> cls)
115
121
// And if still not found, let's choose default?
116
122
name = _defaultTypeId (cls );
117
123
}
118
- synchronized (_typeToId ) {
119
- _typeToId .put (key , name );
120
- }
124
+ _typeToId .put (key , name );
121
125
}
122
126
return name ;
123
127
}
@@ -156,9 +160,9 @@ public String toString() {
156
160
}
157
161
158
162
/*
159
- /*********************************************************
163
+ /**********************************************************************
160
164
/* Helper methods
161
- /*********************************************************
165
+ /**********************************************************************
162
166
*/
163
167
164
168
/**
0 commit comments