BAEL-6705: Check if a String is Strictly Alphanumeric With Java (#14865)
* BAEL-6705: Check if a String is Strictly Alphanumeric With Java * BAEL-6705: Rename a Class * BAEL-6705: Fix isAlpanumeric Logic * BAEL-6705: Fix alphanumericIterationWithCharacterChecks Logic * BAEL-6705: Expose isAlphanumeric method * BAEL-6705: Added isAlphanumeric test
This commit is contained in:
parent
204555a479
commit
ed4b43ce98
|
@ -0,0 +1,91 @@
|
|||
package com.baeldung.alphanumeric;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
@Warmup(iterations = 1)
|
||||
@Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.MINUTES)
|
||||
@Fork(1)
|
||||
public class AlphanumericPerformanceBenchmark {
|
||||
|
||||
private static final String TEST_STRING = "ABC123abc123";
|
||||
private static final String REGEX = "[^[a-zA-Z0-9]*$]";
|
||||
private static final Pattern PATTERN = Pattern.compile(REGEX);
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void alphanumericRegex(Blackhole blackhole) {
|
||||
final Matcher matcher = PATTERN.matcher(TEST_STRING);
|
||||
boolean result = matcher.matches();
|
||||
blackhole.consume(result);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void alphanumericRegexDirectlyOnString(Blackhole blackhole) {
|
||||
boolean result = TEST_STRING.matches(REGEX);
|
||||
blackhole.consume(result);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void alphanumericIteration(Blackhole blackhole) {
|
||||
boolean result = true;
|
||||
for (int i = 0; i < TEST_STRING.length(); ++i) {
|
||||
final int codePoint = TEST_STRING.codePointAt(i);
|
||||
if (!isAlphanumeric(codePoint)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
blackhole.consume(result);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void alphanumericIterationWithCharacterChecks(Blackhole blackhole) {
|
||||
boolean result = true;
|
||||
for (int i = 0; i < TEST_STRING.length(); ++i) {
|
||||
final int codePoint = TEST_STRING.codePointAt(i);
|
||||
if (!Character.isAlphabetic(codePoint) || !Character.isDigit(codePoint)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
blackhole.consume(result);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void alphanumericIterationWithCopy(Blackhole blackhole) {
|
||||
boolean result = true;
|
||||
for (final char c : TEST_STRING.toCharArray()) {
|
||||
if (!isAlphanumeric(c)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
blackhole.consume(result);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public void alphanumericIterationWithStream(Blackhole blackhole) {
|
||||
boolean result = TEST_STRING.chars().allMatch(this::isAlphanumeric);
|
||||
blackhole.consume(result);
|
||||
}
|
||||
|
||||
public boolean isAlphanumeric(final int codePoint) {
|
||||
return (codePoint >= 65 && codePoint <= 90) ||
|
||||
(codePoint >= 97 && codePoint <= 172) ||
|
||||
(codePoint >= 48 && codePoint <= 57);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.alphanumeric;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
class AlphanumericUnitTest {
|
||||
|
||||
private AlphanumericPerformanceBenchmark alphanumericPerformanceBenchmark = new AlphanumericPerformanceBenchmark();
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"A,true",
|
||||
"B,true",
|
||||
"C,true",
|
||||
"1,true",
|
||||
"2,true",
|
||||
"3,true",
|
||||
"!,false",
|
||||
"@,false",
|
||||
"#,false",
|
||||
"$,false",
|
||||
"%,false"
|
||||
})
|
||||
void shouldCorrectlyIdentifyAlphanumericCharacterTest(char character, boolean result) {
|
||||
boolean actual = alphanumericPerformanceBenchmark.isAlphanumeric(character);
|
||||
assertEquals(actual, result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue