From 91ad2c1f5caf049932456af3242e6cbb762eeb9d Mon Sep 17 00:00:00 2001 From: zzq1314zll Date: Thu, 1 Jul 2021 15:47:20 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Add=20NumberUtils=E3=80=81NumberUtilsTest;M?= =?UTF-8?q?odify=20NumberUtil=20dependency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos/common/utils/NumberUtil.java | 44 --- .../nacos/common/utils/NumberUtils.java | 262 ++++++++++++++++++ .../nacos/common/utils/NumberUtilsTest.java | 101 +++++++ .../alibaba/nacos/core/utils/RemoteUtils.java | 6 +- 4 files changed, 366 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 36c26bcbfb1..00000000000 --- 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 00000000000..0bf979129ef --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java @@ -0,0 +1,262 @@ +/* + * 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.

+ * + *

Null and empty String will return + * false.

+ * + * @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.

+ * + *

If the string str is null, + * 0.0f is returned.

+ * + * @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.

+ * + *

If the string str is null, the default + * value is returned.

+ * + * @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; + } + } + + /** + *

Checks whether the String a valid Java number.

+ * + *

Support the following types:

+ *
  • 10 hex
  • + *
  • Hexadecimal
  • + *
  • Scientific notation
  • + *
  • Type identification form
  • + *
  • Positive and negative number identification form
  • + * + * @param str the String to check + * @return true if the string is a correctly formatted number + */ + @SuppressWarnings({"PMD.UndefineMagicConstantRule", "PMD.AvoidComplexConditionRule"}) + public static boolean isNumber(String str) { + if (StringUtils.isBlank(str)) { + return false; + } + char[] chars = str.toString().toCharArray(); + int sz = chars.length; + boolean hasExp = false; + boolean hasDecPoint = false; + boolean allowSigns = false; + boolean foundDigit = false; + + int start = (chars[0] == '-' || chars[0] == '+') ? 1 : 0; + if (sz > start + 1) { + if (chars[start] == '0' && (chars[start + 1] == 'x' || chars[start + 1] == 'X')) { + int i = start + 2; + if (i == sz) { + // str == "0x" + return false; + } + + for (; i < chars.length; i++) { + if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A' || chars[i] > 'F')) { + return false; + } + } + return true; + } + } + sz--; + + int i = start; + + while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { + if (chars[i] >= '0' && chars[i] <= '9') { + foundDigit = true; + allowSigns = false; + + } else if (chars[i] == '.') { + if (hasDecPoint || hasExp) { + + return false; + } + hasDecPoint = true; + } else if (chars[i] == 'e' || chars[i] == 'E') { + + if (hasExp) { + + return false; + } + if (false == foundDigit) { + return false; + } + hasExp = true; + allowSigns = true; + } else if (chars[i] == '+' || chars[i] == '-') { + if (!allowSigns) { + return false; + } + allowSigns = false; + foundDigit = false; + } else { + return false; + } + i++; + } + if (i < chars.length) { + if (chars[i] >= '0' && chars[i] <= '9') { + + return true; + } + if (chars[i] == 'e' || chars[i] == 'E') { + + return false; + } + if (chars[i] == '.') { + if (hasDecPoint || hasExp) { + + return false; + } + + return foundDigit; + } + if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) { + return foundDigit; + } + if (chars[i] == 'l' || chars[i] == 'L') { + return foundDigit && !hasExp; + } + return false; + } + return false == allowSigns && foundDigit; + } + +} 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 00000000000..c6f92be129e --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/utils/NumberUtilsTest.java @@ -0,0 +1,101 @@ +/* + * 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); + } + + @Test + public void testIsNumber() { + Assert.assertTrue(NumberUtils.isNumber("28.55")); + Assert.assertTrue(NumberUtils.isNumber("0")); + Assert.assertTrue(NumberUtils.isNumber("+100.10")); + Assert.assertTrue(NumberUtils.isNumber("-22.022")); + Assert.assertTrue(NumberUtils.isNumber("0X22")); + } +} 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 2993a87937d..863ee287249 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 static int getRemoteExecutorTimesOfProcessors() { 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 { From 175e98b5571e1b51f9588358eed2c790211c9d39 Mon Sep 17 00:00:00 2001 From: zzq1314zll Date: Fri, 2 Jul 2021 10:01:55 +0800 Subject: [PATCH 2/3] delete "isNumber" function --- .../nacos/common/utils/NumberUtils.java | 107 ------------------ .../nacos/common/utils/NumberUtilsTest.java | 8 -- 2 files changed, 115 deletions(-) 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 index 0bf979129ef..28f5ae338be 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java @@ -152,111 +152,4 @@ public static float toFloat(final String str, final float defaultValue) { } } - /** - *

    Checks whether the String a valid Java number.

    - * - *

    Support the following types:

    - *
  • 10 hex
  • - *
  • Hexadecimal
  • - *
  • Scientific notation
  • - *
  • Type identification form
  • - *
  • Positive and negative number identification form
  • - * - * @param str the String to check - * @return true if the string is a correctly formatted number - */ - @SuppressWarnings({"PMD.UndefineMagicConstantRule", "PMD.AvoidComplexConditionRule"}) - public static boolean isNumber(String str) { - if (StringUtils.isBlank(str)) { - return false; - } - char[] chars = str.toString().toCharArray(); - int sz = chars.length; - boolean hasExp = false; - boolean hasDecPoint = false; - boolean allowSigns = false; - boolean foundDigit = false; - - int start = (chars[0] == '-' || chars[0] == '+') ? 1 : 0; - if (sz > start + 1) { - if (chars[start] == '0' && (chars[start + 1] == 'x' || chars[start + 1] == 'X')) { - int i = start + 2; - if (i == sz) { - // str == "0x" - return false; - } - - for (; i < chars.length; i++) { - if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A' || chars[i] > 'F')) { - return false; - } - } - return true; - } - } - sz--; - - int i = start; - - while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { - if (chars[i] >= '0' && chars[i] <= '9') { - foundDigit = true; - allowSigns = false; - - } else if (chars[i] == '.') { - if (hasDecPoint || hasExp) { - - return false; - } - hasDecPoint = true; - } else if (chars[i] == 'e' || chars[i] == 'E') { - - if (hasExp) { - - return false; - } - if (false == foundDigit) { - return false; - } - hasExp = true; - allowSigns = true; - } else if (chars[i] == '+' || chars[i] == '-') { - if (!allowSigns) { - return false; - } - allowSigns = false; - foundDigit = false; - } else { - return false; - } - i++; - } - if (i < chars.length) { - if (chars[i] >= '0' && chars[i] <= '9') { - - return true; - } - if (chars[i] == 'e' || chars[i] == 'E') { - - return false; - } - if (chars[i] == '.') { - if (hasDecPoint || hasExp) { - - return false; - } - - return foundDigit; - } - if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) { - return foundDigit; - } - if (chars[i] == 'l' || chars[i] == 'L') { - return foundDigit && !hasExp; - } - return false; - } - return false == allowSigns && foundDigit; - } - } 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 index c6f92be129e..8016e0b47d1 100644 --- a/common/src/test/java/com/alibaba/nacos/common/utils/NumberUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/utils/NumberUtilsTest.java @@ -90,12 +90,4 @@ public void testToFloatStringString() { Assert.assertEquals(5.0f, NumberUtils.toFloat("001Z.2345", 5.0f), 0); } - @Test - public void testIsNumber() { - Assert.assertTrue(NumberUtils.isNumber("28.55")); - Assert.assertTrue(NumberUtils.isNumber("0")); - Assert.assertTrue(NumberUtils.isNumber("+100.10")); - Assert.assertTrue(NumberUtils.isNumber("-22.022")); - Assert.assertTrue(NumberUtils.isNumber("0X22")); - } } From 2c95d043e47ebeecec0f5a5075ac4afe670cb047 Mon Sep 17 00:00:00 2001 From: zzq1314zll Date: Fri, 2 Jul 2021 10:39:00 +0800 Subject: [PATCH 3/3] [ISSUE #6221] Canonical code --- .../nacos/common/utils/NumberUtils.java | 39 +++++++------------ 1 file changed, 15 insertions(+), 24 deletions(-) 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 index 28f5ae338be..04cfc7a9a66 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/NumberUtils.java @@ -23,8 +23,8 @@ public class NumberUtils { /** - *

    Convert a String to an int, returning - * zero if the conversion fails.

    + * 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 @@ -35,8 +35,8 @@ public static int toInt(String str) { } /** - *

    Convert a String to an int, returning a - * default value if the conversion fails.

    + * 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 @@ -54,8 +54,8 @@ public static int toInt(String str, int defaultValue) { } /** - *

    Convert a String to a long, returning a - * default value if the conversion fails.

    + * 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 @@ -73,8 +73,8 @@ public static long toLong(String str, long defaultValue) { } /** - *

    Convert a String to a double, returning a - * default value if the conversion fails.

    + * 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 @@ -93,11 +93,8 @@ public static double toDouble(String str, double defaultValue) { } /** - *

    Checks whether the String contains only - * digit characters.

    - * - *

    Null and empty String will return - * false.

    + * Checks whether the String contains only + * digit characters. * * @param str the String to check * @return true if str contains only unicode numeric @@ -115,11 +112,8 @@ public static boolean isDigits(String str) { } /** - *

    Convert a String to a float, returning - * 0.0f if the conversion fails.

    - * - *

    If the string str is null, - * 0.0f is returned.

    + * 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 @@ -130,13 +124,10 @@ public static float toFloat(final String str) { } /** - *

    Convert a String to a float, returning a - * default value if the conversion fails.

    + * Convert a String to a float, returning a + * default value if the conversion fails. * - *

    If the string str is null, the default - * value is returned.

    - * - * @param str the string to convert, may be null + * @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