Remove FQCN for StringUtils and let private functions when possible
This commit is contained in:
parent
5badedf7e3
commit
63d9335b09
@ -3,6 +3,7 @@ package com.baeldung.trim;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.openjdk.jmh.annotations.Benchmark;
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
import org.openjdk.jmh.annotations.Fork;
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
@ -34,7 +35,7 @@ public class LTrimRTrim {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Setup
|
@Setup
|
||||||
public void setup() {
|
private void setup() {
|
||||||
src = " White spaces left and right ";
|
src = " White spaces left and right ";
|
||||||
ltrimResult = "White spaces left and right ";
|
ltrimResult = "White spaces left and right ";
|
||||||
rtrimResult = " White spaces left and right";
|
rtrimResult = " White spaces left and right";
|
||||||
@ -67,7 +68,7 @@ public class LTrimRTrim {
|
|||||||
|
|
||||||
// Going through the String detecting Whitespaces
|
// Going through the String detecting Whitespaces
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public boolean whileCharacters() {
|
private boolean whileCharacters() {
|
||||||
String ltrim = whileLtrim(src);
|
String ltrim = whileLtrim(src);
|
||||||
String rtrim = whileRtrim(src);
|
String rtrim = whileRtrim(src);
|
||||||
|
|
||||||
@ -76,7 +77,7 @@ public class LTrimRTrim {
|
|||||||
|
|
||||||
// replaceAll() and Regular Expressions
|
// replaceAll() and Regular Expressions
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public boolean replaceAllRegularExpression() {
|
private boolean replaceAllRegularExpression() {
|
||||||
String ltrim = src.replaceAll("^\\s+", "");
|
String ltrim = src.replaceAll("^\\s+", "");
|
||||||
String rtrim = src.replaceAll("\\s+$", "");
|
String rtrim = src.replaceAll("\\s+$", "");
|
||||||
|
|
||||||
@ -95,7 +96,7 @@ public class LTrimRTrim {
|
|||||||
|
|
||||||
// Pattern matches() with replaceAll
|
// Pattern matches() with replaceAll
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public boolean patternMatchesLTtrimRTrim() {
|
private boolean patternMatchesLTtrimRTrim() {
|
||||||
String ltrim = patternLtrim(src);
|
String ltrim = patternLtrim(src);
|
||||||
String rtrim = patternRtrim(src);
|
String rtrim = patternRtrim(src);
|
||||||
|
|
||||||
@ -104,7 +105,7 @@ public class LTrimRTrim {
|
|||||||
|
|
||||||
// Guava CharMatcher trimLeadingFrom / trimTrailingFrom
|
// Guava CharMatcher trimLeadingFrom / trimTrailingFrom
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public boolean guavaCharMatcher() {
|
private boolean guavaCharMatcher() {
|
||||||
String ltrim = CharMatcher.whitespace().trimLeadingFrom(src);
|
String ltrim = CharMatcher.whitespace().trimLeadingFrom(src);
|
||||||
String rtrim = CharMatcher.whitespace().trimTrailingFrom(src);
|
String rtrim = CharMatcher.whitespace().trimTrailingFrom(src);
|
||||||
|
|
||||||
@ -113,9 +114,9 @@ public class LTrimRTrim {
|
|||||||
|
|
||||||
// Apache Commons StringUtils containsIgnoreCase
|
// Apache Commons StringUtils containsIgnoreCase
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public boolean apacheCommonsStringUtils() {
|
private boolean apacheCommonsStringUtils() {
|
||||||
String ltrim = org.apache.commons.lang3.StringUtils.stripStart(src, null);
|
String ltrim = StringUtils.stripStart(src, null);
|
||||||
String rtrim = org.apache.commons.lang3.StringUtils.stripEnd(src, null);
|
String rtrim = StringUtils.stripEnd(src, null);
|
||||||
|
|
||||||
return checkStrings(ltrim, rtrim);
|
return checkStrings(ltrim, rtrim);
|
||||||
}
|
}
|
||||||
|
@ -67,8 +67,8 @@ public class LTrimRTrimUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenString_whenCallingStringUtilsStripStartEnd_thenReturnsTrue() {
|
public void givenString_whenCallingStringUtilsStripStartEnd_thenReturnsTrue() {
|
||||||
// Use StringUtils containsIgnoreCase to avoid case insensitive issues
|
// Use StringUtils containsIgnoreCase to avoid case insensitive issues
|
||||||
String ltrim = org.apache.commons.lang3.StringUtils.stripStart(src, null);
|
String ltrim = StringUtils.stripStart(src, null);
|
||||||
String rtrim = org.apache.commons.lang3.StringUtils.stripEnd(src, null);
|
String rtrim = StringUtils.stripEnd(src, null);
|
||||||
|
|
||||||
// Compare the Strings obtained and the expected
|
// Compare the Strings obtained and the expected
|
||||||
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
|
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user