|
|
@@ -1,18 +1,29 @@
|
|
|
-package com.jfinal.plugin.activerecord;
|
|
|
+package com.jfinal.kit;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
import java.time.temporal.Temporal;
|
|
|
-import com.jfinal.kit.TimeKit;
|
|
|
|
|
|
/**
|
|
|
- * FieldTypeKit
|
|
|
+ * TypeKit
|
|
|
*/
|
|
|
-class FieldValueKit {
|
|
|
+public class TypeKit {
|
|
|
|
|
|
- static Integer toInt(Object n) {
|
|
|
+ private static final String datePattern = "yyyy-MM-dd";
|
|
|
+ private static final int dateLen = datePattern.length();
|
|
|
+
|
|
|
+ private static final String dateTimeWithoutSecondPattern = "yyyy-MM-dd HH:mm";
|
|
|
+ private static final int dateTimeWithoutSecondLen = dateTimeWithoutSecondPattern.length();
|
|
|
+
|
|
|
+ private static final String dateTimePattern = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ public static String toStr(Object s) {
|
|
|
+ return s != null ? s.toString() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer toInt(Object n) {
|
|
|
if (n instanceof Integer) {
|
|
|
return (Integer)n;
|
|
|
} else if (n instanceof Number) {
|
|
|
@@ -22,7 +33,7 @@ class FieldValueKit {
|
|
|
return n != null ? Integer.parseInt(n.toString()) : null;
|
|
|
}
|
|
|
|
|
|
- static Long toLong(Object n) {
|
|
|
+ public static Long toLong(Object n) {
|
|
|
if (n instanceof Long) {
|
|
|
return (Long)n;
|
|
|
} else if (n instanceof Number) {
|
|
|
@@ -32,7 +43,7 @@ class FieldValueKit {
|
|
|
return n != null ? Long.parseLong(n.toString()) : null;
|
|
|
}
|
|
|
|
|
|
- static Double toDouble(Object n) {
|
|
|
+ public static Double toDouble(Object n) {
|
|
|
if (n instanceof Double) {
|
|
|
return (Double)n;
|
|
|
} else if (n instanceof Number) {
|
|
|
@@ -42,7 +53,7 @@ class FieldValueKit {
|
|
|
return n != null ? Double.parseDouble(n.toString()) : null;
|
|
|
}
|
|
|
|
|
|
- static BigDecimal toBigDecimal(Object n) {
|
|
|
+ public static BigDecimal toBigDecimal(Object n) {
|
|
|
if (n instanceof BigDecimal) {
|
|
|
return (BigDecimal)n;
|
|
|
} else if (n != null) {
|
|
|
@@ -52,7 +63,7 @@ class FieldValueKit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- static Float toFloat(Object n) {
|
|
|
+ public static Float toFloat(Object n) {
|
|
|
if (n instanceof Float) {
|
|
|
return (Float)n;
|
|
|
} else if (n instanceof Number) {
|
|
|
@@ -62,7 +73,7 @@ class FieldValueKit {
|
|
|
return n != null ? Float.parseFloat(n.toString()) : null;
|
|
|
}
|
|
|
|
|
|
- static Short toShort(Object n) {
|
|
|
+ public static Short toShort(Object n) {
|
|
|
if (n instanceof Short) {
|
|
|
return (Short)n;
|
|
|
} else if (n instanceof Number) {
|
|
|
@@ -72,7 +83,7 @@ class FieldValueKit {
|
|
|
return n != null ? Short.parseShort(n.toString()) : null;
|
|
|
}
|
|
|
|
|
|
- static Byte toByte(Object n) {
|
|
|
+ public static Byte toByte(Object n) {
|
|
|
if (n instanceof Byte) {
|
|
|
return (Byte)n;
|
|
|
} else if (n instanceof Number) {
|
|
|
@@ -82,25 +93,25 @@ class FieldValueKit {
|
|
|
return n != null ? Byte.parseByte(n.toString()) : null;
|
|
|
}
|
|
|
|
|
|
- static Boolean toBoolean(Object b) {
|
|
|
+ public static Boolean toBoolean(Object b) {
|
|
|
if (b instanceof Boolean) {
|
|
|
return (Boolean)b;
|
|
|
- } else if (b == null) {
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
- // 支持 String 类型转换,并且支持数字 1/0 与字符串 "1"/"0" 转换
|
|
|
- String s = b.toString();
|
|
|
- if ("true".equalsIgnoreCase(s) || "1".equals(s)) {
|
|
|
- return Boolean.TRUE;
|
|
|
- } else if ("false".equalsIgnoreCase(s) || "0".equals(s)) {
|
|
|
- return Boolean.FALSE;
|
|
|
+ if (b instanceof String) {
|
|
|
+ // 支持 String 类型转换,并且支持数字 1/0 与字符串 "1"/"0" 转换
|
|
|
+ String s = (String)b;
|
|
|
+ if ("true".equalsIgnoreCase(s) || "1".equals(s)) {
|
|
|
+ return Boolean.TRUE;
|
|
|
+ } else if ("false".equalsIgnoreCase(s) || "0".equals(s)) {
|
|
|
+ return Boolean.FALSE;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- throw new ClassCastException("无法转换为 Boolean 值,类型 : " + b.getClass() + " 值 : " + b);
|
|
|
+ return (Boolean)b;
|
|
|
}
|
|
|
|
|
|
- static Number toNumber(Object n) {
|
|
|
+ public static Number toNumber(Object n) {
|
|
|
if (n instanceof Number) {
|
|
|
return (Number)n;
|
|
|
} else if (n == null) {
|
|
|
@@ -112,7 +123,11 @@ class FieldValueKit {
|
|
|
return s.indexOf('.') != -1 ? Double.parseDouble(s) : Long.parseLong(s);
|
|
|
}
|
|
|
|
|
|
- static java.util.Date toDate(Object d) {
|
|
|
+ public static java.util.Date toDate(Object d) {
|
|
|
+ if (d instanceof java.util.Date) {
|
|
|
+ return (java.util.Date)d;
|
|
|
+ }
|
|
|
+
|
|
|
if (d instanceof Temporal) {
|
|
|
if (d instanceof LocalDateTime) {
|
|
|
return TimeKit.toDate((LocalDateTime)d);
|
|
|
@@ -125,13 +140,25 @@ class FieldValueKit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if (d instanceof String) {
|
|
|
+ String s = (String)d;
|
|
|
+ if (s.length() <= dateLen) {
|
|
|
+ return TimeKit.parse(s, datePattern);
|
|
|
+ } else if (s.length() <= dateTimeWithoutSecondLen) {
|
|
|
+ return TimeKit.parse(s, dateTimeWithoutSecondPattern);
|
|
|
+ } else {
|
|
|
+ return TimeKit.parse(s, dateTimePattern);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return (java.util.Date)d;
|
|
|
}
|
|
|
|
|
|
- static LocalDateTime toLocalDateTime(Object ldt) {
|
|
|
+ public static LocalDateTime toLocalDateTime(Object ldt) {
|
|
|
if (ldt instanceof LocalDateTime) {
|
|
|
return (LocalDateTime)ldt;
|
|
|
}
|
|
|
+
|
|
|
if (ldt instanceof LocalDate) {
|
|
|
return ((LocalDate)ldt).atStartOfDay();
|
|
|
}
|
|
|
@@ -142,6 +169,17 @@ class FieldValueKit {
|
|
|
return TimeKit.toLocalDateTime((java.util.Date)ldt);
|
|
|
}
|
|
|
|
|
|
+ if (ldt instanceof String) {
|
|
|
+ String s = (String)ldt;
|
|
|
+ if (s.length() <= dateLen) {
|
|
|
+ return TimeKit.parseLocalDateTime(s, datePattern);
|
|
|
+ } else if (s.length() <= dateTimeWithoutSecondLen) {
|
|
|
+ return TimeKit.parseLocalDateTime(s, dateTimeWithoutSecondPattern);
|
|
|
+ } else {
|
|
|
+ return TimeKit.parseLocalDateTime(s, dateTimePattern);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return (LocalDateTime)ldt;
|
|
|
}
|
|
|
}
|