@@ -15,46 +15,55 @@ public class TypeNameIdResolver extends TypeIdResolverBase
15
15
protected final MapperConfig <?> _config ;
16
16
17
17
/**
18
- * Mappings from class name to type id, used for serialization
18
+ * Mappings from class name to type id, used for serialization.
19
+ *<p>
20
+ * Since lazily constructed will require synchronization (either internal
21
+ * by type, or external)
19
22
*/
20
- protected final Map <String , String > _typeToId ;
23
+ protected final ConcurrentHashMap <String , String > _typeToId ;
21
24
22
25
/**
23
- * Mappings from type id to JavaType, used for deserialization
26
+ * Mappings from type id to JavaType, used for deserialization.
27
+ *<p>
28
+ * Eagerly constructed, not modified, can use regular unsynchronized {@link Map}.
24
29
*/
25
30
protected final Map <String , JavaType > _idToType ;
26
31
27
32
protected TypeNameIdResolver (MapperConfig <?> config , JavaType baseType ,
28
- Map <String , String > typeToId , Map <String , JavaType > idToType )
33
+ ConcurrentHashMap <String , String > typeToId ,
34
+ HashMap <String , JavaType > idToType )
29
35
{
30
36
super (baseType , config .getTypeFactory ());
31
37
_config = config ;
32
- _typeToId = new ConcurrentHashMap < String , String >( typeToId ) ;
33
- _idToType = new ConcurrentHashMap < String , JavaType >( idToType ) ;
38
+ _typeToId = typeToId ;
39
+ _idToType = idToType ;
34
40
}
35
-
41
+
36
42
public static TypeNameIdResolver construct (MapperConfig <?> config , JavaType baseType ,
37
43
Collection <NamedType > subtypes , boolean forSer , boolean forDeser )
38
44
{
39
45
// sanity check
40
46
if (forSer == forDeser ) throw new IllegalArgumentException ();
41
- Map <String , String > typeToId = null ;
42
- Map <String , JavaType > idToType = null ;
47
+
48
+ final ConcurrentHashMap <String , String > typeToId ;
49
+ final HashMap <String , JavaType > idToType ;
43
50
44
51
if (forSer ) {
45
- typeToId = new HashMap <String , String >();
46
- }
47
- if (forDeser ) {
48
- idToType = new HashMap <String , JavaType >();
52
+ // Only need Class-to-id for serialization; but synchronized since may be
53
+ // lazily built (if adding type-id-mappings dynamically)
54
+ typeToId = new ConcurrentHashMap <>();
55
+ idToType = null ;
56
+ } else {
57
+ idToType = new HashMap <>();
49
58
// 14-Apr-2016, tatu: Apparently needed for special case of `defaultImpl`;
50
- // see [databind#1198] for details.
51
- typeToId = new TreeMap <String , String >();
59
+ // see [databind#1198] for details: but essentially we only need room
60
+ // for a single value.
61
+ typeToId = new ConcurrentHashMap <>(4 );
52
62
}
53
63
if (subtypes != null ) {
54
64
for (NamedType t : subtypes ) {
55
- /* no name? Need to figure out default; for now, let's just
56
- * use non-qualified class name
57
- */
65
+ // no name? Need to figure out default; for now, let's just
66
+ // use non-qualified class name
58
67
Class <?> cls = t .getType ();
59
68
String id = t .hasName () ? t .getName () : _defaultTypeId (cls );
60
69
if (forSer ) {
0 commit comments