.
This commit is contained in:
@@ -5,19 +5,14 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou@eversec.cn at 2018/3/12 14:17.
|
||||
*/
|
||||
public class Kv<K,V> extends LinkedHashMap {
|
||||
public class Kv<K,V> extends LinkedHashMap<K,V> {
|
||||
public static Kv of(){
|
||||
return new Kv();
|
||||
}
|
||||
@@ -77,69 +72,31 @@ public class Kv<K,V> extends LinkedHashMap {
|
||||
|| t == Double.class || t == double.class
|
||||
;
|
||||
};
|
||||
public static <T> T toBean(Map<String, Object> m, Class<T> type) {
|
||||
public static <T> T toBean(Map m, Class<T> type) {
|
||||
try {
|
||||
Object obj = type.newInstance();
|
||||
m.forEach((k, v) -> {
|
||||
String methodName = "set" + upFirst.apply(k);
|
||||
Method method = null;
|
||||
String methodName = "set" + upFirst.apply(k+"");
|
||||
Class[] clazzs = clazzMap.get(v == null ? null : v.getClass());
|
||||
if (clazzs == null) {
|
||||
//doc.set(k, v);
|
||||
} else {
|
||||
for (Class clazz : clazzs) {
|
||||
Method method = null;
|
||||
try {
|
||||
method = type.getDeclaredMethod(methodName, clazz);
|
||||
} catch (NoSuchMethodException e) {
|
||||
}
|
||||
if (method != null) {
|
||||
try {
|
||||
if (v == null || "".equals(v)) {
|
||||
|
||||
} else if (v.getClass() == Long.class && clazz != Long.class) {//多种数值类型的处理
|
||||
Object _v;
|
||||
switch (clazz.getSimpleName()) {
|
||||
case "int":
|
||||
case "Long": _v = v; break;
|
||||
case "Integer": _v = (int)((long) v); break;
|
||||
case "short":
|
||||
case "Short": _v = (short)((long) v); break;
|
||||
default: _v = v;
|
||||
}
|
||||
method.invoke(obj, _v);
|
||||
} else if (v.getClass() == Double.class) {
|
||||
Object _v = null;
|
||||
if (isNumber.test(clazz)) {
|
||||
switch (clazz.getSimpleName()) {
|
||||
case "long":
|
||||
case "Long": _v = (long)(double) v; break;
|
||||
case "int":
|
||||
case "Integer": _v = (int)((double) v); break;
|
||||
case "short":
|
||||
case "Short": _v = (int)(double) v; break;
|
||||
default: _v = v;
|
||||
}
|
||||
} else if (clazz == String.class){
|
||||
_v = String.valueOf(v);
|
||||
}
|
||||
method.invoke(obj, _v);
|
||||
} else if (v.getClass() == String.class && clazz == Date.class) {
|
||||
Date _v = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) v);
|
||||
method.invoke(obj, _v);
|
||||
} else if (v.getClass() == String.class && clazz == Integer.class) {
|
||||
Object _v = (int)Double.parseDouble((String) v);
|
||||
method.invoke(obj, _v);
|
||||
}
|
||||
|
||||
else {
|
||||
method.invoke(obj, v);
|
||||
}
|
||||
Object _v = toAs(v, clazz);
|
||||
//如发现 映射异常,打开下面的注释查看 进行映射的值,并对上面的值转换过程升级
|
||||
//System.out.printf("%s:%s %s%n", k,v, v.getClass());
|
||||
method.invoke(obj, _v);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -153,4 +110,70 @@ public class Kv<K,V> extends LinkedHashMap {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T> T toAs(Object v, Class<T> clazz) {
|
||||
if (v == null) {
|
||||
return null;
|
||||
}
|
||||
if (v.getClass() == clazz) {
|
||||
return (T) v;
|
||||
}
|
||||
|
||||
Object _v = null;
|
||||
try {
|
||||
if (v == null || "".equals(v)) {
|
||||
|
||||
} else if (v.getClass() == Long.class && clazz != Long.class) {//多种数值类型的处理: Long => x
|
||||
switch (clazz.getSimpleName()) {
|
||||
case "int":
|
||||
case "Integer": _v = (int)(long) v; break;
|
||||
case "short":
|
||||
case "Short": _v = (short)(long) v; break;
|
||||
case "float":
|
||||
case "Float": _v = (float)(long) v; break;
|
||||
default: _v = v;
|
||||
}
|
||||
} else if (v.getClass() == Double.class && clazz != Double.class) {
|
||||
if (isNumber.test(clazz)) {
|
||||
switch (clazz.getSimpleName()) {
|
||||
case "long":
|
||||
case "Long": _v = (long)(double) v; break;
|
||||
case "int":
|
||||
case "Integer": _v = (int)(double) v; break;
|
||||
case "short":
|
||||
case "Short": _v = (short)(double) v; break;
|
||||
case "float":
|
||||
case "Float": _v = (float)(double) v; break;
|
||||
default: _v = v;
|
||||
}
|
||||
} else if (clazz == String.class){
|
||||
_v = String.valueOf(v);
|
||||
}
|
||||
} else if (v.getClass() == String.class) {
|
||||
switch (clazz.getSimpleName()) {
|
||||
case "Date":
|
||||
_v = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) v); break;
|
||||
//string ==> number
|
||||
case "short":
|
||||
case "Short": _v = (short)Double.parseDouble((String) v); break;
|
||||
case "float":
|
||||
case "Float": _v = (float)Double.parseDouble((String) v); break;
|
||||
case "int":
|
||||
case "Integer": _v = (int)Double.parseDouble((String) v); break;
|
||||
case "long":
|
||||
case "Long": _v = (long)Double.parseDouble((String) v); break;
|
||||
case "double":
|
||||
case "Double": _v = Double.parseDouble((String) v); break;
|
||||
default: _v = v;
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
_v = v;
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return (T) _v;
|
||||
}
|
||||
}
|
||||
19
src/main/java/net/tccn/base/RealType.java
Normal file
19
src/main/java/net/tccn/base/RealType.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package net.tccn.base;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/15 16:39.
|
||||
*/
|
||||
public class RealType<T> {
|
||||
public Class<T> getType() {
|
||||
|
||||
Type type = this.getClass().getGenericSuperclass();
|
||||
ParameterizedType p=(ParameterizedType)type;
|
||||
|
||||
Class<T> c=(Class<T>) p.getActualTypeArguments()[0];
|
||||
System.out.println(c.getName());
|
||||
return c;
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ public abstract class Doc<T extends Doc> {
|
||||
return (V)attr.get(k);
|
||||
}
|
||||
|
||||
public T setShows(String... show) {
|
||||
/*public T setShows(String... show) {
|
||||
if (_shows == null) {
|
||||
_shows = new HashSet<>();
|
||||
}
|
||||
@@ -63,15 +63,15 @@ public abstract class Doc<T extends Doc> {
|
||||
_shows.add(s);
|
||||
}
|
||||
return (T) this;
|
||||
}
|
||||
}*/
|
||||
|
||||
public Set<String> get_Shows() {
|
||||
return _shows;
|
||||
}
|
||||
|
||||
public void set_Shows(Set<String> shows) {
|
||||
/*public void set_Shows(Set<String> shows) {
|
||||
this._shows = shows;
|
||||
}
|
||||
}*/
|
||||
|
||||
public T setOrder(String col, int desc) {
|
||||
if (_order == null) {
|
||||
|
||||
Reference in New Issue
Block a user