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

com.xiaoleilu.hutool.convert.impl.MapConverter Maven / Gradle / Ivy

package com.xiaoleilu.hutool.convert.impl;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.Map.Entry;

import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.convert.AbstractConverter;
import com.xiaoleilu.hutool.convert.ConverterRegistry;
import com.xiaoleilu.hutool.map.MapUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.xiaoleilu.hutool.util.TypeUtil;

/**
 * {@link Map} 转换器
 * 
 * @author Looly
 * @since 3.0.8
 */
public class MapConverter extends AbstractConverter> {
	
	/** Map类型 */
	private final Type mapType;
	/** 键类型 */
	private final Type keyType;
	/** 值类型 */
	private final Type valueType;
	
	/**
	 * 构造,Map的key和value泛型类型自动获取
	 * 
	 * @param mapType Map类型
	 */
	public MapConverter(Type mapType) {
		this(mapType, TypeUtil.getTypeArgument(mapType, 0), TypeUtil.getTypeArgument(mapType, 1));
	}
	
	/**
	 * 构造
	 * @param mapType Map类型
	 * @param keyType 键类型
	 * @param valueType 值类型
	 */
	public MapConverter(Type mapType, Type keyType, Type valueType) {
		this.mapType = mapType;
		this.keyType = keyType;
		this.valueType = valueType;
	}

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected Map convertInternal(Object value) {
		Map map = null;
		if(value instanceof Map){
			map = MapUtil.createMap(TypeUtil.getClass(this.mapType));
			convertMapToMap((Map)value, map);
		}else if(BeanUtil.isBean(value.getClass())){
			map = BeanUtil.beanToMap(value);
		}else{
			throw new UnsupportedOperationException(StrUtil.format("Unsupport toMap value type: {}", value.getClass().getName()));
		}
		return map;
	}

	/**
	 * Map转Map
	 * @param srcMap 源Map
	 * @param targetMap 目标Map
	 */
	private void convertMapToMap(Map srcMap, Map targetMap){
		final ConverterRegistry convert = ConverterRegistry.getInstance();
		Object key;
		Object value;
		for (Entry entry : srcMap.entrySet()) {
			key = (null == this.keyType) ? entry.getKey() : convert.convert(this.keyType, entry.getKey());
			value = (null == this.valueType) ? entry.getValue() : convert.convert(this.keyType, entry.getValue());
			targetMap.put(key, value);
		}
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public Class> getTargetType() {
		return (Class>) TypeUtil.getClass(this.mapType);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy