change MapUtil.get

This commit is contained in:
Looly 2021-07-02 01:08:02 +08:00
parent 8f487a7ec4
commit e7a5295460
3 changed files with 41 additions and 37 deletions

View File

@ -7,7 +7,10 @@
### 🐣新特性
* 【crypto 】 SmUtil.sm4统一返回类型issue#I3YKD4@Gitee
* 【core 】 修改MapUtil.get传入null返回默认值而非nullissue#I3YKBC@Gitee
### 🐞Bug修复
* 【core 】 修复RadixUtil.decode非static问题issue#I3YPEH@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -1186,7 +1186,7 @@ public class MapUtil {
* @since 5.3.11
*/
public static <T> T get(Map<?, ?> map, Object key, Class<T> type, T defaultValue) {
return null == map ? null : Convert.convert(type, map.get(key), defaultValue);
return null == map ? defaultValue : Convert.convert(type, map.get(key), defaultValue);
}
/**
@ -1201,7 +1201,7 @@ public class MapUtil {
* @since 5.5.3
*/
public static <T> T getQuietly(Map<?, ?> map, Object key, Class<T> type, T defaultValue) {
return null == map ? null : Convert.convertQuietly(type, map.get(key), defaultValue);
return null == map ? defaultValue : Convert.convertQuietly(type, map.get(key), defaultValue);
}
/**
@ -1230,7 +1230,7 @@ public class MapUtil {
* @since 5.3.11
*/
public static <T> T get(Map<?, ?> map, Object key, TypeReference<T> type, T defaultValue) {
return null == map ? null : Convert.convert(type, map.get(key), defaultValue);
return null == map ? defaultValue : Convert.convert(type, map.get(key), defaultValue);
}
/**
@ -1245,7 +1245,7 @@ public class MapUtil {
* @since 5.5.3
*/
public static <T> T getQuietly(Map<?, ?> map, Object key, TypeReference<T> type, T defaultValue) {
return null == map ? null : Convert.convertQuietly(type, map.get(key), defaultValue);
return null == map ? defaultValue : Convert.convertQuietly(type, map.get(key), defaultValue);
}
/**

View File

@ -51,7 +51,7 @@ public class RadixUtil {
* @param num 要转换的数值
* @return 自定义进制字符串
*/
public static String encode(final String radixs, final int num) {
public static String encode(String radixs, int num) {
//考虑到负数问题
long tmpNum = (num >= 0 ? num : (0x100000000L - (~num + 1)));
return encode(radixs, tmpNum, 32);
@ -64,7 +64,7 @@ public class RadixUtil {
* @param num 要转换的数值
* @return 自定义进制字符串
*/
public static String encode(final String radixs, final long num) {
public static String encode(String radixs, long num) {
if (num < 0) {
throw new RuntimeException("暂不支持负数!");
}
@ -72,7 +72,38 @@ public class RadixUtil {
return encode(radixs, num, 64);
}
private static String encode(final String radixs, long num, int maxLength) {
/**
* 把转换后的进制字符还原成int
*
* @param radixs 自定进制,需要和encode的保持一致
* @param encodeStr 需要转换成十进制的字符串
* @return int
*/
public static int decodeToInt(String radixs, String encodeStr) {
//还原负数
return (int) decode(radixs, encodeStr);
}
/**
* 把转换后进制的字符还原成long
*
* @param radixs 自定进制,需要和encode的保持一致
* @param encodeStr 需要转换成十进制的字符串
* @return long
*/
public static long decode(String radixs, String encodeStr) {
//目标是多少进制
int rl = radixs.length();
long res = 0L;
for (char c : encodeStr.toCharArray()) {
res = res * rl + radixs.indexOf(c);
}
return res;
}
// -------------------------------------------------------------------------------- Private methods
private static String encode(String radixs, long num, int maxLength) {
if (radixs.length() < 2) {
throw new RuntimeException("自定义进制最少两个字符哦!");
}
@ -92,34 +123,4 @@ public class RadixUtil {
//去掉前面的字符串trim比较耗时
return new String(aa, i, aa.length - i);
}
/**
* 把转换后的进制字符还原成int
*
* @param radixs 自定进制,需要和encode的保持一致
* @param encodeStr 需要转换成十进制的字符串
* @return int
*/
public int decodeToInt(final String radixs, final String encodeStr) {
//还原负数
return (int) decode(radixs, encodeStr);
}
/**
* 把转换后进制的字符还原成long
*
* @param radixs 自定进制,需要和encode的保持一致
* @param encodeStr 需要转换成十进制的字符串
* @return long
*/
public long decode(final String radixs, final String encodeStr) {
//目标是多少进制
int rl = radixs.length();
long res = 0L;
for (char c : encodeStr.toCharArray()) {
res = res * rl + radixs.indexOf(c);
}
return res;
}
}