From 888f50b5e494b0cfe97b53a10ddabf78b17bf72e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ZZQ=E7=9A=84?= <33930711+zzq1314zll@users.noreply.github.com> Date: Fri, 2 Jul 2021 17:46:32 +0800 Subject: [PATCH] =?UTF-8?q?Add=20NumberUtils=E3=80=81NumberUtilsTest;Modif?= =?UTF-8?q?y=20NumberUtil=20dependency=20(#6227)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add NumberUtils、NumberUtilsTest;Modify NumberUtil dependency * delete "isNumber" function * [ISSUE #6221] Canonical code --- .../nacos/common/utils/NumberUtil.java | 44 ------ .../nacos/common/utils/NumberUtils.java | 146 ++++++++++++++++++ .../nacos/common/utils/NumberUtilsTest.java | 93 +++++++++++ .../alibaba/nacos/core/utils/RemoteUtils.java | 6 +- 4 files changed, 242 insertions(+), 47 deletions(-) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/NumberUtil.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java create mode 100644 common/src/test/java/com/alibaba/nacos/common/utils/NumberUtilsTest.java diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtil.java b/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtil.java deleted file mode 100644 index 36c26bcbf..000000000 --- a/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtil.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 1999-2020 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.utils; - -/** - * Nacos number util. - * - * @author xiweng.yy - */ -public class NumberUtil { - - /** - * Whether all chars of input string is digit. - * - * @param input {@code String} checked - * @return {@code true} if all chars is digit, otherwise false - */ - public static boolean isDigits(String input) { - if (StringUtils.isEmpty(input)) { - return false; - } - for (int i = 0; i < input.length(); i++) { - if (!Character.isDigit(input.charAt(i))) { - return false; - } - } - return true; - } - -} diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java new file mode 100644 index 000000000..04cfc7a9a --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java @@ -0,0 +1,146 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.utils; + +/** + * Number utils. + * @author zzq + */ +public class NumberUtils { + + /** + * Convert a String to an int, returning + * zero if the conversion fails. + * + * @param str the string to convert, may be null + * @return the int represented by the string, or zero if + * conversion fails + */ + public static int toInt(String str) { + return toInt(str, 0); + } + + /** + * Convert a String to an int, returning a + * default value if the conversion fails. + * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the int represented by the string, or the default if conversion fails + */ + public static int toInt(String str, int defaultValue) { + if (str == null) { + return defaultValue; + } + try { + return Integer.parseInt(str); + } catch (NumberFormatException nfe) { + return defaultValue; + } + } + + /** + * Convert a String to a long, returning a + * default value if the conversion fails. + * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the long represented by the string, or the default if conversion fails + */ + public static long toLong(String str, long defaultValue) { + if (str == null) { + return defaultValue; + } + try { + return Long.parseLong(str); + } catch (NumberFormatException nfe) { + return defaultValue; + } + } + + /** + * Convert a String to a double, returning a + * default value if the conversion fails. + * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the double represented by the string, or defaultValue + * if conversion fails + */ + public static double toDouble(String str, double defaultValue) { + if (str == null) { + return defaultValue; + } + try { + return Double.parseDouble(str); + } catch (NumberFormatException nfe) { + return defaultValue; + } + } + + /** + * Checks whether the String contains only + * digit characters. + * + * @param str the String to check + * @return true if str contains only unicode numeric + */ + public static boolean isDigits(String str) { + if (StringUtils.isEmpty(str)) { + return false; + } + for (int i = 0; i < str.length(); i++) { + if (!Character.isDigit(str.charAt(i))) { + return false; + } + } + return true; + } + + /** + * Convert a String to a float, returning + * 0.0f if the conversion fails. + * + * @param str the string to convert, may be null + * @return the float represented by the string, or 0.0f + * if conversion fails + */ + public static float toFloat(final String str) { + return toFloat(str, 0.0f); + } + + /** + * Convert a String to a float, returning a + * default value if the conversion fails. + * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the float represented by the string, or defaultValue + * if conversion fails + */ + public static float toFloat(final String str, final float defaultValue) { + if (str == null) { + return defaultValue; + } + try { + return Float.parseFloat(str); + } catch (final NumberFormatException nfe) { + return defaultValue; + } + } + +} diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/NumberUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/NumberUtilsTest.java new file mode 100644 index 000000000..8016e0b47 --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/utils/NumberUtilsTest.java @@ -0,0 +1,93 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.utils; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Number utils. + * @author zzq + */ +public class NumberUtilsTest { + + @Test + public void testToInt() { + Assert.assertEquals(0, NumberUtils.toInt(null)); + Assert.assertEquals(0, NumberUtils.toInt(StringUtils.EMPTY)); + Assert.assertEquals(1, NumberUtils.toInt("1")); + } + + @Test + public void testTestToInt() { + Assert.assertEquals(1, NumberUtils.toInt(null, 1)); + Assert.assertEquals(1, NumberUtils.toInt("", 1)); + Assert.assertEquals(1, NumberUtils.toInt("1", 0)); + } + + @Test + public void testToLong() { + Assert.assertEquals(1L, NumberUtils.toLong(null, 1L)); + Assert.assertEquals(1L, NumberUtils.toLong("", 1L)); + Assert.assertEquals(1L, NumberUtils.toLong("1", 0L)); + } + + @Test + public void testToDouble() { + Assert.assertEquals(1.1d, NumberUtils.toDouble(null, 1.1d), 0); + Assert.assertEquals(1.1d, NumberUtils.toDouble("", 1.1d), 0); + Assert.assertEquals(1.5d, NumberUtils.toDouble("1.5", 0.0d), 0); + } + + @Test + public void testIsDigits() { + Assert.assertFalse(NumberUtils.isDigits(null)); + Assert.assertFalse(NumberUtils.isDigits("")); + Assert.assertTrue(NumberUtils.isDigits("12345")); + Assert.assertFalse(NumberUtils.isDigits("1234.5")); + Assert.assertFalse(NumberUtils.isDigits("1ab")); + Assert.assertFalse(NumberUtils.isDigits("abc")); + } + + @Test + public void testToFloatString() { + Assert.assertEquals(NumberUtils.toFloat("-1.2345"), -1.2345f, 0); + Assert.assertEquals(1.2345f, NumberUtils.toFloat("1.2345"), 0); + Assert.assertEquals(0.0f, NumberUtils.toFloat("abc"), 0); + + Assert.assertEquals(NumberUtils.toFloat("-001.2345"), -1.2345f, 0); + Assert.assertEquals(1.2345f, NumberUtils.toFloat("+001.2345"), 0); + Assert.assertEquals(1.2345f, NumberUtils.toFloat("001.2345"), 0); + Assert.assertEquals(0f, NumberUtils.toFloat("000.00"), 0); + + Assert.assertEquals(NumberUtils.toFloat(Float.MAX_VALUE + ""), Float.MAX_VALUE, 0); + Assert.assertEquals(NumberUtils.toFloat(Float.MIN_VALUE + ""), Float.MIN_VALUE, 0); + Assert.assertEquals(0.0f, NumberUtils.toFloat(""), 0); + Assert.assertEquals(0.0f, NumberUtils.toFloat(null), 0); + } + + @Test + public void testToFloatStringString() { + Assert.assertEquals(1.2345f, NumberUtils.toFloat("1.2345", 5.1f), 0); + Assert.assertEquals(5.0f, NumberUtils.toFloat("a", 5.0f), 0); + // LANG-1060 + Assert.assertEquals(5.0f, NumberUtils.toFloat("-001Z.2345", 5.0f), 0); + Assert.assertEquals(5.0f, NumberUtils.toFloat("+001AB.2345", 5.0f), 0); + Assert.assertEquals(5.0f, NumberUtils.toFloat("001Z.2345", 5.0f), 0); + } + +} diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/RemoteUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/RemoteUtils.java index 2993a8793..863ee2872 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/RemoteUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/RemoteUtils.java @@ -16,7 +16,7 @@ package com.alibaba.nacos.core.utils; -import com.alibaba.nacos.common.utils.NumberUtil; +import com.alibaba.nacos.common.utils.NumberUtils; /** * util of remote. @@ -45,7 +45,7 @@ public class RemoteUtils { */ public static int getRemoteExecutorTimesOfProcessors() { String timesString = System.getProperty("remote.executor.times.of.processors"); - if (NumberUtil.isDigits(timesString)) { + if (NumberUtils.isDigits(timesString)) { int times = Integer.parseInt(timesString); return times > 0 ? times : REMOTE_EXECUTOR_TIMES_OF_PROCESSORS; } else { @@ -55,7 +55,7 @@ public class RemoteUtils { public static int getRemoteExecutorQueueSize() { String queueSizeString = System.getProperty("remote.executor.queue.size"); - if (NumberUtil.isDigits(queueSizeString)) { + if (NumberUtils.isDigits(queueSizeString)) { int size = Integer.parseInt(queueSizeString); return size > 0 ? size : REMOTE_EXECUTOR_QUEUE_SIZE; } else {