rengb 5 anni fa
parent
commit
6e9c562f02
1 ha cambiato i file con 126 aggiunte e 0 eliminazioni
  1. 126 0
      public/src/main/java/com/lantone/qc/pub/util/MapUtil.java

+ 126 - 0
public/src/main/java/com/lantone/qc/pub/util/MapUtil.java

@@ -0,0 +1,126 @@
+package com.lantone.qc.pub.util;
+
+import com.google.common.collect.Maps;
+
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @Description: map工具类
+ * @author: gaodm
+ * @time: 2018/9/4 9:24
+ */
+public class MapUtil {
+    private static final String SP = ";";
+    private static final String SSP = ":";
+
+    /**
+     * 把Map转换成String。注意以英文分号字符';'开始和结束
+     *
+     * @param attrs
+     * @return
+     */
+    public static String toString(Map<String, String> attrs) {
+        StringBuilder sb = new StringBuilder();
+        if (null != attrs && !attrs.isEmpty()) {
+            sb.append(SP);
+            for (String key : attrs.keySet()) {
+                String val = attrs.get(key);
+                if (val != null && !"".equals(val)) {
+                    sb.append(key).append(SSP).append(val).append(SP);
+                }
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
+     * 把key:value;key:value格式的String转换成Map
+     *
+     * @param str
+     * @return
+     */
+    public static Map<String, String> fromString(String str) {
+        Map<String, String> attrs = new HashMap<String, String>();
+        if (str != null && !"".equals(str)) {
+            String[] arr = str.split(SP);
+            if (null != arr) {
+                for (String kv : arr) {
+                    if (kv != null && !"".equals(kv)) {
+                        String[] ar = kv.split(SSP);
+                        if (null != ar && ar.length == 2) {
+                            String key = ar[0];
+                            String val = ar[1];
+                            if (val != null && !"".equals(val)) {
+                                attrs.put(key, val);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return attrs;
+    }
+
+    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
+        if (map == null) {
+            return null;
+        }
+
+        Object obj = beanClass.newInstance();
+
+        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
+        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
+        for (PropertyDescriptor property : propertyDescriptors) {
+            Method setter = property.getWriteMethod();
+            if (setter != null) {
+                setter.invoke(obj, map.get(property.getName()));
+            }
+        }
+
+        return obj;
+    }
+
+    public static Map<String, Object> objectToMap(Object obj) throws Exception {
+        if (obj == null) {
+            return null;
+        }
+
+        Map<String, Object> map = new HashMap<String, Object>();
+
+        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
+        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
+        for (PropertyDescriptor property : propertyDescriptors) {
+            String key = property.getName();
+            if (key.compareToIgnoreCase("class") == 0) {
+                continue;
+            }
+            Method getter = property.getReadMethod();
+            Object value = getter != null ? getter.invoke(obj) : null;
+            map.put(key, value);
+        }
+        return map;
+    }
+
+    /**
+     * 复制一个map
+     *
+     * @param map
+     * @return
+     */
+    public static Map<String, Object> copyMap(Map<String, Object> map) {
+        if (map == null) {
+            return null;
+        }
+        Map<String, Object> retMap = Maps.newHashMap();
+        map.keySet().forEach(key -> {
+            retMap.put(key, map.get(key));
+        });
+        return retMap;
+    }
+
+}