Clean Apache Commons and Guava functions and tests

This commit is contained in:
Cavero Barca 2020-03-06 21:58:00 +01:00
parent f5a18c6dca
commit 020c3bd8c9
2 changed files with 10 additions and 26 deletions

View File

@ -102,38 +102,20 @@ public class LTrimRTrim {
return checkStrings(ltrim, rtrim); return checkStrings(ltrim, rtrim);
} }
public static String guavaLtrim(String s) {
return CharMatcher.whitespace()
.trimLeadingFrom(s);
}
public static String guavaRtrim(String s) {
return CharMatcher.whitespace()
.trimTrailingFrom(s);
}
// Guava CharMatcher trimLeadingFrom / trimTrailingFrom // Guava CharMatcher trimLeadingFrom / trimTrailingFrom
@Benchmark @Benchmark
public boolean guavaCharMatcher() { public boolean guavaCharMatcher() {
String ltrim = guavaLtrim(src); String ltrim = CharMatcher.whitespace().trimLeadingFrom(src);
String rtrim = guavaRtrim(src); String rtrim = CharMatcher.whitespace().trimTrailingFrom(src);
return checkStrings(ltrim, rtrim); return checkStrings(ltrim, rtrim);
} }
public static String stringUtilsRTrim(String str) {
return org.apache.commons.lang3.StringUtils.stripEnd(str, " ");
}
public static String stringUtilsLTrim(String str) {
return org.apache.commons.lang3.StringUtils.stripStart(str, " ");
}
// Apache Commons StringUtils containsIgnoreCase // Apache Commons StringUtils containsIgnoreCase
@Benchmark @Benchmark
public boolean apacheCommonsStringUtils() { public boolean apacheCommonsStringUtils() {
String ltrim = stringUtilsLTrim(src); String ltrim = org.apache.commons.lang3.StringUtils.stripStart(src, " ");
String rtrim = stringUtilsRTrim(src); String rtrim = org.apache.commons.lang3.StringUtils.stripEnd(src, " ");
return checkStrings(ltrim, rtrim); return checkStrings(ltrim, rtrim);
} }

View File

@ -4,6 +4,8 @@ import org.apache.commons.lang3.StringUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import com.google.common.base.CharMatcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -53,8 +55,8 @@ public class LTrimRTrimUnitTest {
@Test @Test
public void givenString_whenCallingGuavaCharMatcher_thenReturnsTrue() { public void givenString_whenCallingGuavaCharMatcher_thenReturnsTrue() {
// Use StringUtils containsIgnoreCase to avoid case insensitive issues // Use StringUtils containsIgnoreCase to avoid case insensitive issues
String ltrim = LTrimRTrim.guavaLtrim(src); String ltrim = CharMatcher.whitespace().trimLeadingFrom(src);;
String rtrim = LTrimRTrim.guavaRtrim(src); String rtrim = CharMatcher.whitespace().trimTrailingFrom(src);
// Compare the Strings obtained and the expected // Compare the Strings obtained and the expected
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim)); Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
@ -65,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 = LTrimRTrim.stringUtilsLTrim(src); String ltrim = org.apache.commons.lang3.StringUtils.stripStart(src, " ");
String rtrim = LTrimRTrim.stringUtilsRTrim(src); String rtrim = org.apache.commons.lang3.StringUtils.stripEnd(src, " ");
// Compare the Strings obtained and the expected // Compare the Strings obtained and the expected
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim)); Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));