All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.jackson.map.ser.impl.ReadOnlyClassToSerializerMap Maven / Gradle / Ivy

Go to download

Data Mapper package is a high-performance data binding package built on Jackson JSON processor

There is a newer version: 1.9.13
Show newest version
package org.codehaus.jackson.map.ser.impl;

import java.util.*;

import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ser.SerializerCache.TypeKey;

/**
 * Optimized lookup table for accessing two types of serializers; typed
 * and non-typed. Only accessed from a single thread, so no synchronization
 * needed for accessors.
 * 
 * @since 1.7
 */
public final class ReadOnlyClassToSerializerMap
{
    /**
     * Actual mappings from type key to serializers
     */
    protected final JsonSerializerMap _map;

    /**
     * We'll reuse key class to avoid unnecessary instantiations; since
     * this is not shared between threads, we can just reuse single
     * instance.
     */
    protected final TypeKey _cacheKey = new TypeKey(getClass(), false);
    
    private ReadOnlyClassToSerializerMap(JsonSerializerMap map)
    {
        _map = map;
    }

    public ReadOnlyClassToSerializerMap instance()
    {
        return new ReadOnlyClassToSerializerMap(_map);
    }

    /**
     * Factory method for creating the "blueprint" lookup map. Such map
     * can not be used as is but just shared: to get an actual usable
     * instance, {@link #instance} has to be called first.
     */
    public static ReadOnlyClassToSerializerMap from(HashMap> src)
    {
        return new ReadOnlyClassToSerializerMap(new JsonSerializerMap(src));
    }

    public JsonSerializer typedValueSerializer(JavaType type)
    { 
        _cacheKey.resetTyped(type);
        return _map.find(_cacheKey);
    }

    public JsonSerializer typedValueSerializer(Class cls)
    { 
        _cacheKey.resetTyped(cls);
        return _map.find(_cacheKey);
    }
    
    public JsonSerializer untypedValueSerializer(Class cls)
    { 
        _cacheKey.resetUntyped(cls);
        return _map.find(_cacheKey);
    }

    public JsonSerializer untypedValueSerializer(JavaType type)
    { 
        _cacheKey.resetUntyped(type);
        return _map.find(_cacheKey);
    }
}