Add NumberUtils、NumberUtilsTest;Modify NumberUtil dependency (#6227)
* Add NumberUtils、NumberUtilsTest;Modify NumberUtil dependency * delete "isNumber" function * [ISSUE #6221] Canonical code
This commit is contained in:
parent
89af6a97ae
commit
888f50b5e4
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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 <code>String</code> to an <code>int</code>, returning
|
||||
* <code>zero</code> if the conversion fails.
|
||||
*
|
||||
* @param str the string to convert, may be null
|
||||
* @return the int represented by the string, or <code>zero</code> if
|
||||
* conversion fails
|
||||
*/
|
||||
public static int toInt(String str) {
|
||||
return toInt(str, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>String</code> to an <code>int</code>, 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 <code>String</code> to a <code>long</code>, 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 <code>String</code> to a <code>double</code>, returning a
|
||||
* default value if the conversion fails.
|
||||
*
|
||||
* @param str the string to convert, may be <code>null</code>
|
||||
* @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 <code>String</code> contains only
|
||||
* digit characters.
|
||||
*
|
||||
* @param str the <code>String</code> to check
|
||||
* @return <code>true</code> 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 <code>String</code> to a <code>float</code>, returning
|
||||
* <code>0.0f</code> if the conversion fails.
|
||||
*
|
||||
* @param str the string to convert, may be <code>null</code>
|
||||
* @return the float represented by the string, or <code>0.0f</code>
|
||||
* if conversion fails
|
||||
*/
|
||||
public static float toFloat(final String str) {
|
||||
return toFloat(str, 0.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>String</code> to a <code>float</code>, 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user