BAEL-1739 - Check If a String is Numeric in Java (#4209)
* BAEL-1725 Java Pass-by-reference vs Pass-by-value - First commit * updated test cases * First commit consisting only of test cases * First commit with test cases * Introduced JMH Michrobenchmarking * Minor corrections
This commit is contained in:
parent
387c325403
commit
07838a2d25
|
@ -0,0 +1,75 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
import org.openjdk.jmh.runner.Runner;
|
||||||
|
import org.openjdk.jmh.runner.RunnerException;
|
||||||
|
import org.openjdk.jmh.runner.options.Options;
|
||||||
|
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||||
|
|
||||||
|
|
||||||
|
public class Benchmarking {
|
||||||
|
public static void main(String[] args) throws RunnerException {
|
||||||
|
Options opt = new OptionsBuilder()
|
||||||
|
.include(Benchmarking.class.getSimpleName())
|
||||||
|
.forks(1)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
new Runner(opt).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@State(Scope.Thread)
|
||||||
|
public static class ExecutionPlan {
|
||||||
|
public String number = Integer.toString(Integer.MAX_VALUE);
|
||||||
|
public boolean isNumber = false;
|
||||||
|
public IsNumeric isNumeric= new IsNumeric();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void usingCoreJava(ExecutionPlan plan) {
|
||||||
|
plan.isNumber = plan.isNumeric.usingCoreJava(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void usingRegularExpressions(ExecutionPlan plan) {
|
||||||
|
plan.isNumber = plan.isNumeric.usingRegularExpressions(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void usingNumberUtils_isCreatable(ExecutionPlan plan) {
|
||||||
|
plan.isNumber = plan.isNumeric.usingNumberUtils_isCreatable(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void usingNumberUtils_isParsable(ExecutionPlan plan) {
|
||||||
|
plan.isNumber = plan.isNumeric.usingNumberUtils_isParsable(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void usingStringUtils_isNumeric(ExecutionPlan plan) {
|
||||||
|
plan.isNumber = plan.isNumeric.usingStringUtils_isNumeric(plan.number);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public void usingStringUtils_isNumericSpace(ExecutionPlan plan) {
|
||||||
|
plan.isNumber = plan.isNumeric.usingStringUtils_isNumericSpace(plan.number);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.commons.lang3.math.NumberUtils;
|
||||||
|
|
||||||
|
public class IsNumeric {
|
||||||
|
public boolean usingCoreJava(String strNum) {
|
||||||
|
try {
|
||||||
|
double d = Double.parseDouble(strNum);
|
||||||
|
} catch (NumberFormatException | NullPointerException nfe) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean usingRegularExpressions(String strNum) {
|
||||||
|
return strNum.matches("-?\\d+(\\.\\d+)?");
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean usingNumberUtils_isCreatable(String strNum) {
|
||||||
|
return NumberUtils.isCreatable(strNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean usingNumberUtils_isParsable(String strNum) {
|
||||||
|
return NumberUtils.isParsable(strNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean usingStringUtils_isNumeric(String strNum) {
|
||||||
|
return StringUtils.isNumeric(strNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean usingStringUtils_isNumericSpace(String strNum) {
|
||||||
|
return StringUtils.isNumericSpace(strNum);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||||
|
|
||||||
|
public class IsNumericDriver {
|
||||||
|
private static IsNumeric isNumeric;
|
||||||
|
|
||||||
|
static {
|
||||||
|
isNumeric =new IsNumeric();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
LOG.info("Testing all methods...");
|
||||||
|
|
||||||
|
boolean res = isNumeric.usingCoreJava("1001");
|
||||||
|
LOG.info("Using Core Java : " + res);
|
||||||
|
|
||||||
|
res = isNumeric.usingRegularExpressions("1001");
|
||||||
|
LOG.info("Using Regular Expressions : " + res);
|
||||||
|
|
||||||
|
res =isNumeric.usingNumberUtils_isCreatable("1001");
|
||||||
|
LOG.info("Using NumberUtils.isCreatable : " + res);
|
||||||
|
|
||||||
|
res =isNumeric.usingNumberUtils_isParsable("1001");
|
||||||
|
LOG.info("Using NumberUtils.isParsable : " + res);
|
||||||
|
|
||||||
|
res =isNumeric.usingStringUtils_isNumeric("1001");
|
||||||
|
LOG.info("Using StringUtils.isNumeric : " + res);
|
||||||
|
|
||||||
|
res =isNumeric.usingStringUtils_isNumericSpace("1001");
|
||||||
|
LOG.info("Using StringUtils.isNumericSpace : " + res);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class CoreJavaIsNumericUnitTest {
|
||||||
|
public static boolean isNumeric(String strNum) {
|
||||||
|
try {
|
||||||
|
double d = Double.parseDouble(strNum);
|
||||||
|
} catch (NumberFormatException | NullPointerException nfe) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingCoreJava_thenTrue() {
|
||||||
|
// Valid Numbers
|
||||||
|
assertThat(isNumeric("22")).isTrue();
|
||||||
|
assertThat(isNumeric("5.05")).isTrue();
|
||||||
|
assertThat(isNumeric("-200")).isTrue();
|
||||||
|
assertThat(isNumeric("10.0d")).isTrue();
|
||||||
|
assertThat(isNumeric(" 22 ")).isTrue();
|
||||||
|
|
||||||
|
// Invalid Numbers
|
||||||
|
assertThat(isNumeric(null)).isFalse();
|
||||||
|
assertThat(isNumeric("")).isFalse();
|
||||||
|
assertThat(isNumeric("abc")).isFalse();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.math.NumberUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class NumberUtilsIsCreatableUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenApacheCommons_whenUsingIsParsable_thenTrue() {
|
||||||
|
// Valid Numbers
|
||||||
|
assertThat(NumberUtils.isCreatable("22")).isTrue();
|
||||||
|
assertThat(NumberUtils.isCreatable("5.05")).isTrue();
|
||||||
|
assertThat(NumberUtils.isCreatable("-200")).isTrue();
|
||||||
|
assertThat(NumberUtils.isCreatable("10.0d")).isTrue();
|
||||||
|
assertThat(NumberUtils.isCreatable("1000L")).isTrue();
|
||||||
|
assertThat(NumberUtils.isCreatable("0xFF")).isTrue();
|
||||||
|
assertThat(NumberUtils.isCreatable("07")).isTrue();
|
||||||
|
assertThat(NumberUtils.isCreatable("2.99e+8")).isTrue();
|
||||||
|
|
||||||
|
// Invalid Numbers
|
||||||
|
assertThat(NumberUtils.isCreatable(null)).isFalse();
|
||||||
|
assertThat(NumberUtils.isCreatable("")).isFalse();
|
||||||
|
assertThat(NumberUtils.isCreatable("abc")).isFalse();
|
||||||
|
assertThat(NumberUtils.isCreatable(" 22 ")).isFalse();
|
||||||
|
assertThat(NumberUtils.isCreatable("09")).isFalse();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.math.NumberUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class NumberUtilsIsParsableUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenApacheCommons_whenUsingIsParsable_thenTrue() {
|
||||||
|
// Valid Numbers
|
||||||
|
assertThat(NumberUtils.isParsable("22")).isTrue();
|
||||||
|
assertThat(NumberUtils.isParsable("-23")).isTrue();
|
||||||
|
assertThat(NumberUtils.isParsable("2.2")).isTrue();
|
||||||
|
assertThat(NumberUtils.isParsable("09")).isTrue();
|
||||||
|
|
||||||
|
// Invalid Numbers
|
||||||
|
assertThat(NumberUtils.isParsable(null)).isFalse();
|
||||||
|
assertThat(NumberUtils.isParsable("")).isFalse();
|
||||||
|
assertThat(NumberUtils.isParsable("6.2f")).isFalse();
|
||||||
|
assertThat(NumberUtils.isParsable("9.8d")).isFalse();
|
||||||
|
assertThat(NumberUtils.isParsable("22L")).isFalse();
|
||||||
|
assertThat(NumberUtils.isParsable("0xFF")).isFalse();
|
||||||
|
assertThat(NumberUtils.isParsable("2.99e+8")).isFalse();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class RegularExpressionsUnitTest {
|
||||||
|
public static boolean isNumeric(String strNum) {
|
||||||
|
return strNum.matches("-?\\d+(\\.\\d+)?");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingRegularExpressions_thenTrue() {
|
||||||
|
// Valid Numbers
|
||||||
|
assertThat(isNumeric("22")).isTrue();
|
||||||
|
assertThat(isNumeric("5.05")).isTrue();
|
||||||
|
assertThat(isNumeric("-200")).isTrue();
|
||||||
|
|
||||||
|
// Invalid Numbers
|
||||||
|
assertThat(isNumeric("abc")).isFalse();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class StringUtilsIsNumericSpaceUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenApacheCommons_whenUsingIsNumericSpace_thenTrue() {
|
||||||
|
// Valid Numbers
|
||||||
|
assertThat(StringUtils.isNumericSpace("123")).isTrue();
|
||||||
|
assertThat(StringUtils.isNumericSpace("١٢٣")).isTrue();
|
||||||
|
assertThat(StringUtils.isNumericSpace("")).isTrue();
|
||||||
|
assertThat(StringUtils.isNumericSpace(" ")).isTrue();
|
||||||
|
assertThat(StringUtils.isNumericSpace("12 3")).isTrue();
|
||||||
|
|
||||||
|
// Invalid Numbers
|
||||||
|
assertThat(StringUtils.isNumericSpace(null)).isFalse();
|
||||||
|
assertThat(StringUtils.isNumericSpace("ab2c")).isFalse();
|
||||||
|
assertThat(StringUtils.isNumericSpace("12.3")).isFalse();
|
||||||
|
assertThat(StringUtils.isNumericSpace("-123")).isFalse();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.stringisnumeric;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class StringUtilsIsNumericUnitTest {
|
||||||
|
@Test
|
||||||
|
public void givenApacheCommons_whenUsingIsNumeric_thenTrue() {
|
||||||
|
// Valid Numbers
|
||||||
|
assertThat(StringUtils.isNumeric("123")).isTrue();
|
||||||
|
assertThat(StringUtils.isNumeric("١٢٣")).isTrue();
|
||||||
|
assertThat(StringUtils.isNumeric("१२३")).isTrue();
|
||||||
|
|
||||||
|
// Invalid Numbers
|
||||||
|
assertThat(StringUtils.isNumeric(null)).isFalse();
|
||||||
|
assertThat(StringUtils.isNumeric("")).isFalse();
|
||||||
|
assertThat(StringUtils.isNumeric(" ")).isFalse();
|
||||||
|
assertThat(StringUtils.isNumeric("12 3")).isFalse();
|
||||||
|
assertThat(StringUtils.isNumeric("ab2c")).isFalse();
|
||||||
|
assertThat(StringUtils.isNumeric("12.3")).isFalse();
|
||||||
|
assertThat(StringUtils.isNumeric("-123")).isFalse();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue