|
@@ -138,7 +138,9 @@ public class ConvertUtil {
|
|
* @return 十六进制字符串 (小写)
|
|
* @return 十六进制字符串 (小写)
|
|
*/
|
|
*/
|
|
public static String toHex(byte[] bytes) {
|
|
public static String toHex(byte[] bytes) {
|
|
- if (bytes == null) return "";
|
|
|
|
|
|
+ if (bytes == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
|
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
|
for (byte b : bytes) {
|
|
for (byte b : bytes) {
|
|
sb.append(String.format("%02x", b));
|
|
sb.append(String.format("%02x", b));
|
|
@@ -205,7 +207,9 @@ public class ConvertUtil {
|
|
* @return 转换后的Byte值
|
|
* @return 转换后的Byte值
|
|
*/
|
|
*/
|
|
public static Byte toByte(Object value, Byte defaultValue) {
|
|
public static Byte toByte(Object value, Byte defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof Number) {
|
|
if (value instanceof Number) {
|
|
return ((Number) value).byteValue();
|
|
return ((Number) value).byteValue();
|
|
}
|
|
}
|
|
@@ -255,7 +259,9 @@ public class ConvertUtil {
|
|
* @return 转换后的Integer值
|
|
* @return 转换后的Integer值
|
|
*/
|
|
*/
|
|
public static Integer toInt(Object value, Integer defaultValue) {
|
|
public static Integer toInt(Object value, Integer defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof Number) {
|
|
if (value instanceof Number) {
|
|
return ((Number) value).intValue();
|
|
return ((Number) value).intValue();
|
|
}
|
|
}
|
|
@@ -284,7 +290,9 @@ public class ConvertUtil {
|
|
* @return 转换后的Long值
|
|
* @return 转换后的Long值
|
|
*/
|
|
*/
|
|
public static Long toLong(Object value, Long defaultValue) {
|
|
public static Long toLong(Object value, Long defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof Number) {
|
|
if (value instanceof Number) {
|
|
return ((Number) value).longValue();
|
|
return ((Number) value).longValue();
|
|
}
|
|
}
|
|
@@ -313,7 +321,9 @@ public class ConvertUtil {
|
|
* @return 转换后的Short值
|
|
* @return 转换后的Short值
|
|
*/
|
|
*/
|
|
public static Short toShort(Object value, Short defaultValue) {
|
|
public static Short toShort(Object value, Short defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof Number) {
|
|
if (value instanceof Number) {
|
|
return ((Number) value).shortValue();
|
|
return ((Number) value).shortValue();
|
|
}
|
|
}
|
|
@@ -342,7 +352,9 @@ public class ConvertUtil {
|
|
* @return 转换后的Float值
|
|
* @return 转换后的Float值
|
|
*/
|
|
*/
|
|
public static Float toFloat(Object value, Float defaultValue) {
|
|
public static Float toFloat(Object value, Float defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof Number) {
|
|
if (value instanceof Number) {
|
|
return ((Number) value).floatValue();
|
|
return ((Number) value).floatValue();
|
|
}
|
|
}
|
|
@@ -371,7 +383,9 @@ public class ConvertUtil {
|
|
* @return 转换后的Double值
|
|
* @return 转换后的Double值
|
|
*/
|
|
*/
|
|
public static Double toDouble(Object value, Double defaultValue) {
|
|
public static Double toDouble(Object value, Double defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof Number) {
|
|
if (value instanceof Number) {
|
|
return ((Number) value).doubleValue();
|
|
return ((Number) value).doubleValue();
|
|
}
|
|
}
|
|
@@ -406,7 +420,9 @@ public class ConvertUtil {
|
|
* @return 转换后的Boolean值
|
|
* @return 转换后的Boolean值
|
|
*/
|
|
*/
|
|
public static Boolean toBoolean(Object value, Boolean defaultValue) {
|
|
public static Boolean toBoolean(Object value, Boolean defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof Boolean) {
|
|
if (value instanceof Boolean) {
|
|
return (Boolean) value;
|
|
return (Boolean) value;
|
|
}
|
|
}
|
|
@@ -415,10 +431,18 @@ public class ConvertUtil {
|
|
}
|
|
}
|
|
if (value instanceof String) {
|
|
if (value instanceof String) {
|
|
String strVal = ((String) value).trim().toLowerCase();
|
|
String strVal = ((String) value).trim().toLowerCase();
|
|
- if ("true".equals(strVal)) return true;
|
|
|
|
- if ("false".equals(strVal)) return false;
|
|
|
|
- if ("1".equals(strVal)) return true;
|
|
|
|
- if ("0".equals(strVal)) return false;
|
|
|
|
|
|
+ if ("true".equals(strVal)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ if ("false".equals(strVal)) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ if ("1".equals(strVal)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ if ("0".equals(strVal)) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
return Boolean.parseBoolean(value.toString());
|
|
return Boolean.parseBoolean(value.toString());
|
|
@@ -445,7 +469,9 @@ public class ConvertUtil {
|
|
* @return 转换后的Character值
|
|
* @return 转换后的Character值
|
|
*/
|
|
*/
|
|
public static Character toCharacter(Object value, Character defaultValue) {
|
|
public static Character toCharacter(Object value, Character defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof Character) {
|
|
if (value instanceof Character) {
|
|
return (Character) value;
|
|
return (Character) value;
|
|
}
|
|
}
|
|
@@ -479,7 +505,9 @@ public class ConvertUtil {
|
|
* @return 转换后的BigDecimal值
|
|
* @return 转换后的BigDecimal值
|
|
*/
|
|
*/
|
|
public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
|
|
public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof BigDecimal) {
|
|
if (value instanceof BigDecimal) {
|
|
return (BigDecimal) value;
|
|
return (BigDecimal) value;
|
|
}
|
|
}
|
|
@@ -514,7 +542,9 @@ public class ConvertUtil {
|
|
* @return 转换后的BigInteger值
|
|
* @return 转换后的BigInteger值
|
|
*/
|
|
*/
|
|
public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
|
|
public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof BigInteger) {
|
|
if (value instanceof BigInteger) {
|
|
return (BigInteger) value;
|
|
return (BigInteger) value;
|
|
}
|
|
}
|
|
@@ -551,7 +581,9 @@ public class ConvertUtil {
|
|
* @return 转换后的String值
|
|
* @return 转换后的String值
|
|
*/
|
|
*/
|
|
public static String toStr(Object value, String defaultValue) {
|
|
public static String toStr(Object value, String defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
if (value instanceof String) {
|
|
if (value instanceof String) {
|
|
return (String) value;
|
|
return (String) value;
|
|
}
|
|
}
|
|
@@ -779,7 +811,9 @@ public class ConvertUtil {
|
|
* @return 转换后的枚举值
|
|
* @return 转换后的枚举值
|
|
*/
|
|
*/
|
|
public static <E extends Enum<E>> E toEnum(Class<E> enumClass, Object value, E defaultValue) {
|
|
public static <E extends Enum<E>> E toEnum(Class<E> enumClass, Object value, E defaultValue) {
|
|
- if (value == null) return defaultValue;
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return defaultValue;
|
|
|
|
+ }
|
|
|
|
|
|
// 尝试通过名称匹配
|
|
// 尝试通过名称匹配
|
|
if (value instanceof String) {
|
|
if (value instanceof String) {
|
|
@@ -935,7 +969,9 @@ public class ConvertUtil {
|
|
* @return Unicode编码字符串 (格式: \u4e2d\u6587)
|
|
* @return Unicode编码字符串 (格式: \u4e2d\u6587)
|
|
*/
|
|
*/
|
|
public static String strToUnicode(String str) {
|
|
public static String strToUnicode(String str) {
|
|
- if (str == null) return null;
|
|
|
|
|
|
+ if (str == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
for (char c : str.toCharArray()) {
|
|
for (char c : str.toCharArray()) {
|
|
sb.append("\\u").append(String.format("%04x", (int) c));
|
|
sb.append("\\u").append(String.format("%04x", (int) c));
|
|
@@ -950,11 +986,15 @@ public class ConvertUtil {
|
|
* @return 解码后的字符串
|
|
* @return 解码后的字符串
|
|
*/
|
|
*/
|
|
public static String unicodeToStr(String unicode) {
|
|
public static String unicodeToStr(String unicode) {
|
|
- if (unicode == null) return null;
|
|
|
|
|
|
+ if (unicode == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
String[] hex = unicode.split("\\\\u");
|
|
String[] hex = unicode.split("\\\\u");
|
|
for (int i = 1; i < hex.length; i++) {
|
|
for (int i = 1; i < hex.length; i++) {
|
|
- if (hex[i].isEmpty()) continue;
|
|
|
|
|
|
+ if (hex[i].isEmpty()) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
try {
|
|
try {
|
|
int data = Integer.parseInt(hex[i].substring(0, 4), 16);
|
|
int data = Integer.parseInt(hex[i].substring(0, 4), 16);
|
|
sb.append((char) data);
|
|
sb.append((char) data);
|
|
@@ -982,7 +1022,9 @@ public class ConvertUtil {
|
|
* @return 转换后的int数组
|
|
* @return 转换后的int数组
|
|
*/
|
|
*/
|
|
public static int[] toIntArray(Object value) {
|
|
public static int[] toIntArray(Object value) {
|
|
- if (value == null) return new int[0];
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return new int[0];
|
|
|
|
+ }
|
|
if (value instanceof int[]) {
|
|
if (value instanceof int[]) {
|
|
return (int[]) value;
|
|
return (int[]) value;
|
|
}
|
|
}
|
|
@@ -1017,7 +1059,9 @@ public class ConvertUtil {
|
|
* @return 转换后的String数组
|
|
* @return 转换后的String数组
|
|
*/
|
|
*/
|
|
public static String[] toStrArray(Object value) {
|
|
public static String[] toStrArray(Object value) {
|
|
- if (value == null) return new String[0];
|
|
|
|
|
|
+ if (value == null) {
|
|
|
|
+ return new String[0];
|
|
|
|
+ }
|
|
if (value instanceof String[]) {
|
|
if (value instanceof String[]) {
|
|
return (String[]) value;
|
|
return (String[]) value;
|
|
}
|
|
}
|