NumberUtil增加重载parseXXX方法, 解析失败返回默认值

This commit is contained in:
Looly 2023-03-23 23:55:58 +08:00
parent 59470d1669
commit d192a53bbb
3 changed files with 17 additions and 24 deletions

View File

@ -9,6 +9,8 @@
* 【core 】 改进XmlUtil.xmlToBean支持xml转bean时父节点忽略大小写 * 【core 】 改进XmlUtil.xmlToBean支持xml转bean时父节点忽略大小写
* 【core 】 优化ArrayUtil的空判断pr#2969@Github * 【core 】 优化ArrayUtil的空判断pr#2969@Github
* 【extra 】 优化SpringUtil在非Spring环境下的异常issue#2835@Github * 【extra 】 优化SpringUtil在非Spring环境下的异常issue#2835@Github
* 【core 】 StrUtil增加commonPrefix和commonSuffix方法pr#3007@Github
* 【core 】 NumberUtil增加重载parseXXX方法, 解析失败返回默认值pr#3007@Github
### 🐞Bug修复 ### 🐞Bug修复
* 【crypto】 修复NoSuchMethodError未捕获问题issue#2966@Github * 【crypto】 修复NoSuchMethodError未捕获问题issue#2966@Github

View File

@ -2142,9 +2142,9 @@ public class CharSequenceUtil {
*/ */
public static String subWithLength(String input, int fromIndex, int length) { public static String subWithLength(String input, int fromIndex, int length) {
final int toIndex; final int toIndex;
if(fromIndex < 0){ if (fromIndex < 0) {
toIndex = fromIndex - length; toIndex = fromIndex - length;
}else{ } else {
toIndex = fromIndex + length; toIndex = fromIndex + length;
} }
return sub(input, fromIndex, toIndex); return sub(input, fromIndex, toIndex);
@ -4587,18 +4587,17 @@ public class CharSequenceUtil {
} }
/** /**
* 字符串1和字符串2的公共前缀
* *
* @param str1 字符串1 * @param str1 字符串1
* @param str2 字符串2 * @param str2 字符串2
* @return 字符串1和字符串2的公共前缀 * @return 字符串1和字符串2的公共前缀
*/ */
public static CharSequence commonPrefix(CharSequence str1, CharSequence str2) { public static CharSequence commonPrefix(CharSequence str1, CharSequence str2) {
if (isBlank(str1) || isBlank(str2)) { if (isBlank(str1) || isBlank(str2)) {
return EMPTY; return EMPTY;
} }
final int minLength = Math.min(str1.length(), str2.length());
int minLength = Math.min(str1.length(), str2.length());
int index = 0; int index = 0;
@ -4607,36 +4606,28 @@ public class CharSequenceUtil {
if (str1.charAt(index) != str2.charAt(index)) { if (str1.charAt(index) != str2.charAt(index)) {
break; break;
} }
} }
return str1.subSequence(0, index); return str1.subSequence(0, index);
} }
/** /**
* 字符串1和字符串2的公共后缀
* *
* @param str1 字符串1 * @param str1 字符串1
* @param str2 字符串2 * @param str2 字符串2
* @return 字符串1和字符串2的公共后缀 * @return 字符串1和字符串2的公共后缀
*/ */
public static CharSequence commonSuffix(CharSequence str1, CharSequence str2) { public static CharSequence commonSuffix(CharSequence str1, CharSequence str2) {
if (isBlank(str1) || isBlank(str2)) { if (isBlank(str1) || isBlank(str2)) {
return EMPTY; return EMPTY;
} }
int str1Index = str1.length() - 1; int str1Index = str1.length() - 1;
int str2Index = str2.length() - 1; int str2Index = str2.length() - 1;
for (; str1Index >= 0 && str2Index >= 0; str1Index--, str2Index--) { for (; str1Index >= 0 && str2Index >= 0; str1Index--, str2Index--) {
if (str1.charAt(str1Index) != str2.charAt(str2Index)) { if (str1.charAt(str1Index) != str2.charAt(str2Index)) {
break; break;
} }
} }
return str1.subSequence(str1Index + 1, str1.length()); return str1.subSequence(str1Index + 1, str1.length());
} }

View File

@ -413,18 +413,18 @@ public class NumberUtilTest {
// -------------------------- Parse failed ----------------------- // -------------------------- Parse failed -----------------------
Long v1 = NumberUtil.parseLong(null, null); final Long v1 = NumberUtil.parseLong(null, null);
assertThat(v1, nullValue()); assertThat(v1, nullValue());
Long v2 = NumberUtil.parseLong(StrUtil.EMPTY, null); final Long v2 = NumberUtil.parseLong(StrUtil.EMPTY, null);
assertThat(v2, nullValue()); assertThat(v2, nullValue());
Long v3 = NumberUtil.parseLong("L3221", 1233L); final Long v3 = NumberUtil.parseLong("L3221", 1233L);
assertThat(v3, equalTo(1233L)); assertThat(v3, equalTo(1233L));
// -------------------------- Parse success ----------------------- // -------------------------- Parse success -----------------------
Long v4 = NumberUtil.parseLong("1233L", null); final Long v4 = NumberUtil.parseLong("1233L", null);
assertThat(v4, equalTo(1233L)); assertThat(v4, equalTo(1233L));
} }