欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Map工具类 博客分类: J2SE

程序员文章站 2024-03-23 09:01:40
...
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 
 * 2017-09-25
 */
public class MapConvertUtil implements Serializable{
	
	/**
	 * 序列化
	 */
	private static final long serialVersionUID = -1581756576497606672L;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(MapConvertUtil.class);
	
	public static Double getDoubleValue(Map<String, Object> map, String key) {
		BigDecimal value = getMapValue(map, key, BigDecimal.class);
		return value == null ? 0d : value.doubleValue();
	}
	
	public static Float getFloatValue(Map<String, Object> map, String key) {
		BigDecimal value = getMapValue(map, key, BigDecimal.class);
		return value == null ? 0f : value.floatValue();
	}

	public static Date getDateValue(Map<String, Object> map, String key) {
		Object value = map.get(key);
		return value == null ? new Date() : new Date(value.toString());
	}

	public static Integer getIntegerValue(Map<String, Object> map, String key) {
		Object value = map.get(key);
		if(value != null)
		{
			try {
				return Integer.valueOf(value.toString());
			} catch (Exception e) {
				LOGGER.error("exception message:", e);
				return 0;
			}
		}
		return 0;
	}
	
	public static Long getLongValue(Map<String, Object> map, String key) {
		Object value = map.get(key);
		if(value != null)
		{
			try {
				return Long.valueOf(value.toString());
			} catch (Exception e) {
				LOGGER.error("exception message:", e);
				return 0l;
			}
		}
		return 0l;
	}
	
	public static String getStringValue(Map<String, Object> map, String key) {
		String value = getMapValue(map, key, String.class);
		return value == null ? "" : value;
	}

	public static <T> T getMapValue(Map<String, Object> map, String key, Class<T> clazz) {
		Object value = map.get(key);
		if (value == null) {
			return null;
		}
		return clazz.cast(value);
	}

	public static Map<String, Object> PO2Map(Object o){
		Map<String, Object> map = new HashMap<String, Object>();
		Field[] fields = null;
		try {
			//子类属性
			fields = o.getClass().getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				String proName = field.getName();
				Object proValue = field.get(o);
				map.put(proName, proValue);
			}
			
			//父类属性
			fields = o.getClass().getSuperclass().getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				String proName = field.getName();
				Object proValue = field.get(o);
				map.put(proName, proValue);
			}
		} catch (Exception e) {
			LOGGER.error("exception message:", e);
		}
		return map;
	}

	public static Object map2PO(Map<String,Object> map,Object o){
		try {
			if (!map.isEmpty()) {
				for (Map.Entry<String,Object> m : map.entrySet()) {
					Object v = "";
					if (!m.getKey().isEmpty()) {
						v = m.getValue();
					}
					Field[] fields = null;
					//子类属性
					fields = o.getClass().getDeclaredFields();
					for (Field field : fields) {
						int mod = field.getModifiers();
						if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
							continue;
						}
						if (field.getName().equals(m.getKey())) {
							field.setAccessible(true);
							field.set(o, v);
						}
	
					}
					
					//父类属性
					fields = o.getClass().getSuperclass().getDeclaredFields();
					for (Field field : fields) {
						int mod = field.getModifiers();
						if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
							continue;
						}
						if (field.getName().equals(m.getKey())) {
							field.setAccessible(true);
							field.set(o, v);
						}
	
					}
				}
			}
		}catch (Exception e) {
			LOGGER.error("exception message:", e);
		}
		return o;
	}
	
	public static Map<String,Object> getMsgResultMap(final String msg)
	{
		return new HashMap<String, Object>(){/**
			 */
			private static final long serialVersionUID = 3883115499389307532L;

		{
		    put("code", "0");
		    put("msg", msg);
	    }};
	}
	
	public static Map<String,Object> getDataResultMap(final Object data)
	{
		return new HashMap<String, Object>(){/**
			 */
			private static final long serialVersionUID = 2024109428179733317L;

		{
		    put("code", "1");
		    put("msg", "success");
		    put("data", data);
	    }};
	}
}