测试完善 Kv.toAs

This commit is contained in:
2019-07-05 17:55:56 +08:00
parent 972fb387c7
commit ae32b3393d
2 changed files with 66 additions and 6 deletions

View File

@@ -96,7 +96,10 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
private static Predicate<Class> isNumber = (t) -> {
return t == Integer.class || t == int.class
|| t == Long.class || t == long.class
|| t == float.class || t == Float.class
|| t == Double.class || t == double.class
|| t == Short.class || t == short.class
|| t == Byte.class || t == byte.class
;
};
@@ -119,9 +122,11 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
case "Short": v1 = (short)(long) v; break;
case "float":
case "Float": v1 = (float)(long) v; break;
case "byte":
case "Byte": v1 = (byte)(long) v; break;
default: v1 = v;
}
} else if (v.getClass() == Double.class && clazz != Double.class) {
} else if (v.getClass() == Double.class) {
if (isNumber.test(clazz)) {
switch (clazz.getSimpleName()) {
case "long":
@@ -132,6 +137,8 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
case "Short": v1 = (short)(double) v; break;
case "float":
case "Float": v1 = (float)(double) v; break;
case "byte":
case "Byte": v1 = (byte)(double) v; break;
default: v1 = v;
}
} else if (clazz == String.class){
@@ -141,7 +148,6 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
switch (clazz.getSimpleName()) {
case "Date":
v1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) v); break;
//string ==> number
case "short":
case "Short": v1 = (short)Double.parseDouble((String) v); break;
case "float":
@@ -152,18 +158,32 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
case "Long": v1 = (long)Double.parseDouble((String) v); break;
case "double":
case "Double": v1 = Double.parseDouble((String) v); break;
case "byte":
case "Byte": v1 = Byte.parseByte((String) v); break;
default: v1 = v;
}
} else if (v.getClass() == Integer.class) {
switch (clazz.getSimpleName()) {
case "long":
case "Long": v1 = (long) (int) v; break;
case "int":
case "Integer": v1 = (int) v; break;
case "short":
case "Short": v1 = (short) (int) v; break;
case "float":
case "Float": v1 = (float) (int) v; break;
case "byte":
case "Byte": v1 = (byte)(int) v; break;
default: v1 = v;
}
} else if (v.getClass() == Float.class) {
switch (clazz.getSimpleName()) {
case "long":
case "Long": v1 = (long) (float) v; break;
case "int":
case "Integer": v1 = (int) (float)v; break;
case "short":
case "Short": v1 = (short) (float) v; break;
case "byte":
case "Byte": v1 = (byte)(float) v; break;
default: v1 = v;
}
}

View File

@@ -176,7 +176,7 @@ public class RunTest<T> {
//@Test
public void kvTest() {
Map map = new HashMap<>();
/*Map map = new HashMap<>();
map.put("name", "xxxx");
map.put("age", 12);
@@ -186,7 +186,47 @@ public class RunTest<T> {
Kv kv = Kv.toKv(user, "name", "abxx=age");
System.out.println(user);
System.out.println(kv);
System.out.println(kv);*/
Class[] clazzs = {
int.class, long.class, short.class, byte.class,
Integer.class, Long.class, Short.class, Byte.class, float.class, Float.class,
String.class,
};
Object[] ks = new Object[]{"1", (int)1, (Integer)1, 1l, 1.0, 1f, 1.0d};
for (Object k : ks) {
System.out.println("--------------------------------------------");
for (Class v : clazzs) {
System.out.printf("%s -> %s = ", k.getClass().getSimpleName(), v.getSimpleName());
Object o = Kv.toAs(k, v);
switch (v.getSimpleName()) {
case "int":
System.out.println((int)o);break;
case "Integer":
System.out.println((Integer)o);break;
case "long":
System.out.println((long)o);break;
case "Long":
System.out.println((Long)o);break;
case "short":
System.out.println((short)o);break;
case "Short":
System.out.println((Short)o);break;
case "byte":
System.out.println((byte)o);break;
case "float":
System.out.println((float)o);break;
case "Float":
System.out.println((Float)o);break;
case "Byte":
System.out.println((Byte)o);break;
case "String":
System.out.println((String)o);break;
}
}
}
}
//@Test