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

com.distelli.jackson.transform.TransformModule Maven / Gradle / Ivy

There is a newer version: 3.8.16
Show newest version
package com.distelli.jackson.transform;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * NOTE: Do NOT call ObjectMapper.registerModule() until all the
 * createTransform() methods are called!
 */
public class TransformModule extends SimpleModule {
    private Map _transforms = new HashMap<>();

    public TransformModule() {
        super("JacksonTransform");
    }

    private synchronized  Transform createTransform(Type type, Type builderType) {
        if ( null != _transforms.get(type) ) {
            throw new DuplicateTransformException("Transform for "+type+" already exists");
        }
        Transform result = new Transform(type, builderType, this);
        _transforms.put(type, result);
        return result;
    }

    public  Transform createTransform(Class type) {
        return createTransform(type, type);
    }

    public  Transform createTransform(TypeReference type) {
        return createTransform(type.getType(), type.getType());
    }

    public  Transform createTransform(TypeReference type, TypeReference builderType) {
        return createTransform(type.getType(), builderType.getType());
    }

    public  Transform createTransform(Class type, Class builderType) {
        return createTransform((Type)type, (Type)builderType);
    }

    public Transform getTransform(Type type) {
        return _transforms.get(type);
    }

    public  Transform getTransform(Class type) {
        return _transforms.get(type);
    }
}