11package com .fasterxml .jackson .databind .jsontype .impl ;
22
33import java .util .*;
4+ import java .util .concurrent .ConcurrentHashMap ;
45
56import com .fasterxml .jackson .annotation .JsonTypeInfo ;
67
1213public class TypeNameIdResolver extends TypeIdResolverBase
1314{
1415 /**
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)
1620 */
17- protected final Map <String , String > _typeToId ;
21+ protected final ConcurrentHashMap <String , String > _typeToId ;
1822
1923 /**
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}.
2127 */
2228 protected final Map <String , JavaType > _idToType ;
2329
2430 protected TypeNameIdResolver (JavaType baseType ,
25- Map <String , String > typeToId , Map <String , JavaType > idToType )
31+ ConcurrentHashMap <String , String > typeToId ,
32+ HashMap <String , JavaType > idToType )
2633 {
2734 super (baseType );
2835 _typeToId = typeToId ;
2936 _idToType = idToType ;
3037 }
31-
38+
3239 public static TypeNameIdResolver construct (MapperConfig <?> config , JavaType baseType ,
3340 Collection <NamedType > subtypes , boolean forSer , boolean forDeser )
3441 {
3542 // sanity check
3643 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 ;
3947
4048 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 <>();
4555 // 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 );
4859 }
4960 if (subtypes != null ) {
5061 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
5464 Class <?> cls = t .getType ();
5565 String id = t .hasName () ? t .getName () : _defaultTypeId (cls );
5666 if (forSer ) {
@@ -98,11 +108,7 @@ protected String idFromClass(DatabindContext ctxt, Class<?> cls)
98108// cls = _typeFactory.constructType(cls).getRawClass();
99109
100110 final String key = cls .getName ();
101- String name ;
102-
103- synchronized (_typeToId ) {
104- name = _typeToId .get (key );
105- }
111+ String name = _typeToId .get (key );
106112
107113 if (name == null ) {
108114 // 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)
115121 // And if still not found, let's choose default?
116122 name = _defaultTypeId (cls );
117123 }
118- synchronized (_typeToId ) {
119- _typeToId .put (key , name );
120- }
124+ _typeToId .put (key , name );
121125 }
122126 return name ;
123127 }
@@ -156,9 +160,9 @@ public String toString() {
156160 }
157161
158162 /*
159- /*********************************************************
163+ /**********************************************************************
160164 /* Helper methods
161- /*********************************************************
165+ /**********************************************************************
162166 */
163167
164168 /**
0 commit comments