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

com.github.xphsc.convert.ConverterRegistry Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
package com.github.xphsc.convert;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by ${huipei.x} on 2017-5-25.
 */
public class ConverterRegistry {

    private Map, Converter> defaultConverterMap;
    private Map, Converter> customConverterMap;

    public static ConverterRegistry getInstance() {
        return ConverterRegistry.SingletonHolder.instance;
    }





    public ConverterRegistry putCustom(Class clazz, Converter converter) {
        if(null == this.customConverterMap) {
            synchronized(this) {
                if(null == this.customConverterMap) {
                    this.customConverterMap = new ConcurrentHashMap();
                }
            }
        }

        this.customConverterMap.put(clazz, converter);
        return this;
    }

    public  Converter getConverter(Class type, boolean isCustomFirst) {
        Converter converter = null;
        if(isCustomFirst) {
            converter = this.getCustomConverter(type);
            if(null == converter) {
                converter = this.getDefaultConverter(type);
            }
        } else {
            converter = this.getDefaultConverter(type);
            if(null == converter) {
                converter = this.getCustomConverter(type);
            }
        }

        return converter;
    }

    public  Converter getDefaultConverter(Class type) {
        return null == this.defaultConverterMap?null:(Converter)this.defaultConverterMap.get(type);
    }

    public  Converter getCustomConverter(Class type) {
        return null == this.customConverterMap?null:(Converter)this.customConverterMap.get(type);
    }







    private static class SingletonHolder {
        private static ConverterRegistry instance = new ConverterRegistry();

        private SingletonHolder() {
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy