Skip to content

Commit

Permalink
Minor tweaks after #2556 (no functional changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 3, 2019
1 parent 044caee commit 3c7729f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*<p>
* Since lazily constructed will require synchronization (either internal
* by type, or external)
*/
protected final Map<String, String> _typeToId;
protected final ConcurrentHashMap<String, String> _typeToId;

/**
* Mappings from type id to JavaType, used for deserialization
* Mappings from type id to JavaType, used for deserialization.
*<p>
* Eagerly constructed, not modified, can use regular unsynchronized {@link Map}.
*/
protected final Map<String, JavaType> _idToType;

protected TypeNameIdResolver(MapperConfig<?> config, JavaType baseType,
Map<String, String> typeToId, Map<String, JavaType> idToType)
ConcurrentHashMap<String, String> typeToId,
HashMap<String, JavaType> idToType)
{
super(baseType, config.getTypeFactory());
_config = config;
_typeToId = new ConcurrentHashMap<String, String>(typeToId);
_idToType = new ConcurrentHashMap<String, JavaType>(idToType);
_typeToId = typeToId;
_idToType = idToType;
}

public static TypeNameIdResolver construct(MapperConfig<?> config, JavaType baseType,
Collection<NamedType> subtypes, boolean forSer, boolean forDeser)
{
// sanity check
if (forSer == forDeser) throw new IllegalArgumentException();
Map<String, String> typeToId = null;
Map<String, JavaType> idToType = null;

final ConcurrentHashMap<String, String> typeToId;
final HashMap<String, JavaType> idToType;

if (forSer) {
typeToId = new HashMap<String, String>();
}
if (forDeser) {
idToType = new HashMap<String, JavaType>();
// 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<String, String>();
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit 3c7729f

Please sign in to comment.