package com.yonge.cooleshow.common.config;

import com.google.common.collect.Maps;
import com.yonge.toolset.base.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class EnumConverterFactory implements ConverterFactory<Object, BaseEnum<?, ?>> {
	@SuppressWarnings("rawtypes")
	private static final Map<Class, Converter> CONVERTERS = Maps.newHashMap();

	@Override
	public <T extends BaseEnum<?, ?>> Converter<Object, T> getConverter(Class<T> targetType) {
		Converter<Object, T> converter = CONVERTERS.get(targetType);
		if (converter == null) {
			converter = new EnumConverter<>(targetType);
			CONVERTERS.put(targetType, converter);
		}
		return converter;

	}

}
