add null check

This commit is contained in:
Looly
2022-05-27 18:03:50 +08:00
parent 400c74e82a
commit 705f65a207
2 changed files with 32 additions and 18 deletions

View File

@@ -35,7 +35,7 @@ import java.util.Set;
* <ul> * <ul>
* <li><a href="https://github.com/venusdrogon/feilong-core/wiki/one-jdk7-bug-thinking">one-jdk7-bug-thinking</a></li> * <li><a href="https://github.com/venusdrogon/feilong-core/wiki/one-jdk7-bug-thinking">one-jdk7-bug-thinking</a></li>
* </ul> * </ul>
* * <p>
* TODO 需整理精简方法,去掉无用的重载方法。 * TODO 需整理精简方法,去掉无用的重载方法。
* *
* @author Looly * @author Looly
@@ -1242,11 +1242,13 @@ public class NumberUtil {
* @return 是否为整数 * @return 是否为整数
*/ */
public static boolean isInteger(final String s) { public static boolean isInteger(final String s) {
if(StrUtil.isNotBlank(s)) {
try { try {
Integer.parseInt(s); Integer.parseInt(s);
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
return false; return false;
} }
}
return true; return true;
} }
@@ -1259,11 +1261,13 @@ public class NumberUtil {
* @since 4.0.0 * @since 4.0.0
*/ */
public static boolean isLong(final String s) { public static boolean isLong(final String s) {
if (StrUtil.isNotBlank(s)) {
try { try {
Long.parseLong(s); Long.parseLong(s);
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
return false; return false;
} }
}
return true; return true;
} }
@@ -1274,12 +1278,14 @@ public class NumberUtil {
* @return 是否为{@link Double}类型 * @return 是否为{@link Double}类型
*/ */
public static boolean isDouble(final String s) { public static boolean isDouble(final String s) {
if (StrUtil.isNotBlank(s)) {
try { try {
Double.parseDouble(s); Double.parseDouble(s);
return s.contains("."); return s.contains(".");
} catch (final NumberFormatException ignore) { } catch (final NumberFormatException ignore) {
// ignore // ignore
} }
}
return false; return false;
} }

View File

@@ -450,7 +450,15 @@ public class NumberUtilTest {
@Test @Test
public void divIntegerTest(){ public void divIntegerTest(){
System.out.println(NumberUtil.div(100101300, (Number) 100)); final BigDecimal div = NumberUtil.div(100101300, (Number) 100);
Assert.assertEquals(1001013, div.intValue());
}
@Test
public void isDoubleTest(){
Assert.assertFalse(NumberUtil.isDouble(null));
Assert.assertFalse(NumberUtil.isDouble(""));
Assert.assertFalse(NumberUtil.isDouble(" "));
} }
} }