From 3c7729f68061235c46dfb16b6bfe472cc8e921f8 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 2 Dec 2019 21:14:19 -0800 Subject: [PATCH] Minor tweaks after #2556 (no functional changes) --- .../jsontype/impl/TypeNameIdResolver.java | 45 +++++++++++-------- ...8Test.java => ExternalTypeId1198Test.java} | 3 +- 2 files changed, 29 insertions(+), 19 deletions(-) rename src/test/java/com/fasterxml/jackson/databind/jsontype/ext/{ExternalTypeId198Test.java => ExternalTypeId1198Test.java} (96%) diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeNameIdResolver.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeNameIdResolver.java index e3f8244ab3..1d2d785ae2 100644 --- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeNameIdResolver.java +++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeNameIdResolver.java @@ -15,46 +15,55 @@ public class TypeNameIdResolver extends TypeIdResolverBase protected final MapperConfig _config; /** - * Mappings from class name to type id, used for serialization + * Mappings from class name to type id, used for serialization. + *

+ * Since lazily constructed will require synchronization (either internal + * by type, or external) */ - protected final Map _typeToId; + protected final ConcurrentHashMap _typeToId; /** - * Mappings from type id to JavaType, used for deserialization + * Mappings from type id to JavaType, used for deserialization. + *

+ * Eagerly constructed, not modified, can use regular unsynchronized {@link Map}. */ protected final Map _idToType; protected TypeNameIdResolver(MapperConfig config, JavaType baseType, - Map typeToId, Map idToType) + ConcurrentHashMap typeToId, + HashMap idToType) { super(baseType, config.getTypeFactory()); _config = config; - _typeToId = new ConcurrentHashMap(typeToId); - _idToType = new ConcurrentHashMap(idToType); + _typeToId = typeToId; + _idToType = idToType; } - + public static TypeNameIdResolver construct(MapperConfig config, JavaType baseType, Collection subtypes, boolean forSer, boolean forDeser) { // sanity check if (forSer == forDeser) throw new IllegalArgumentException(); - Map typeToId = null; - Map idToType = null; + + final ConcurrentHashMap typeToId; + final HashMap idToType; if (forSer) { - typeToId = new HashMap(); - } - if (forDeser) { - idToType = new HashMap(); + // Only need Class-to-id for serialization; but synchronized since may be + // lazily built (if adding type-id-mappings dynamically) + typeToId = new ConcurrentHashMap<>(); + idToType = null; + } else { + idToType = new HashMap<>(); // 14-Apr-2016, tatu: Apparently needed for special case of `defaultImpl`; - // see [databind#1198] for details. - typeToId = new TreeMap(); + // see [databind#1198] for details: but essentially we only need room + // for a single value. + typeToId = new ConcurrentHashMap<>(4); } if (subtypes != null) { for (NamedType t : subtypes) { - /* no name? Need to figure out default; for now, let's just - * use non-qualified class name - */ + // no name? Need to figure out default; for now, let's just + // use non-qualified class name Class cls = t.getType(); String id = t.hasName() ? t.getName() : _defaultTypeId(cls); if (forSer) { diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId198Test.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId1198Test.java similarity index 96% rename from src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId198Test.java rename to src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId1198Test.java index c34793f199..38f32c272f 100644 --- a/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId198Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId1198Test.java @@ -6,7 +6,8 @@ import com.fasterxml.jackson.databind.*; -public class ExternalTypeId198Test extends BaseMapTest +// [databind#1198] +public class ExternalTypeId1198Test extends BaseMapTest { public enum Attacks { KICK, PUNCH }