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:
ramansahasi
2018-05-11 02:11:33 +05:30
committed by maibin
parent 387c325403
commit 07838a2d25
10 changed files with 297 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}