Skip to content

Commit 3c7729f

Browse files
committed
Minor tweaks after #2556 (no functional changes)
1 parent 044caee commit 3c7729f

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeNameIdResolver.java

+27-18
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,55 @@ public class TypeNameIdResolver extends TypeIdResolverBase
1515
protected final MapperConfig<?> _config;
1616

1717
/**
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)
1922
*/
20-
protected final Map<String, String> _typeToId;
23+
protected final ConcurrentHashMap<String, String> _typeToId;
2124

2225
/**
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}.
2429
*/
2530
protected final Map<String, JavaType> _idToType;
2631

2732
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)
2935
{
3036
super(baseType, config.getTypeFactory());
3137
_config = config;
32-
_typeToId = new ConcurrentHashMap<String, String>(typeToId);
33-
_idToType = new ConcurrentHashMap<String, JavaType>(idToType);
38+
_typeToId = typeToId;
39+
_idToType = idToType;
3440
}
35-
41+
3642
public static TypeNameIdResolver construct(MapperConfig<?> config, JavaType baseType,
3743
Collection<NamedType> subtypes, boolean forSer, boolean forDeser)
3844
{
3945
// sanity check
4046
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;
4350

4451
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<>();
4958
// 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);
5262
}
5363
if (subtypes != null) {
5464
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
5867
Class<?> cls = t.getType();
5968
String id = t.hasName() ? t.getName() : _defaultTypeId(cls);
6069
if (forSer) {

src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId198Test.java renamed to src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId1198Test.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import com.fasterxml.jackson.databind.*;
88

9-
public class ExternalTypeId198Test extends BaseMapTest
9+
// [databind#1198]
10+
public class ExternalTypeId1198Test extends BaseMapTest
1011
{
1112
public enum Attacks { KICK, PUNCH }
1213

0 commit comments

Comments
 (0)