Skip to content

Commit 49f4d68

Browse files
committed
implement more string util functions
1 parent 6f120fc commit 49f4d68

2 files changed

Lines changed: 133 additions & 1 deletion

File tree

src/main/java/org/htmlunit/util/StringUtils.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,77 @@ public static boolean isEmptyString(final CharSequence s) {
7070
return s != null && s.length() == 0;
7171
}
7272

73+
/**
74+
* Returns true if the param is null or empty.
75+
*
76+
* @param s the string to check
77+
* @return true if the param is null or empty
78+
*/
79+
public static boolean isEmptyOrNull(final CharSequence s) {
80+
return s == null || s.length() == 0;
81+
}
82+
83+
/**
84+
* Returns either the passed in CharSequence, or if the CharSequence is
85+
* empty or {@code null}, the default value.
86+
*
87+
* @param <T> the kind of CharSequence
88+
* @param s the CharSequence to check
89+
* @param defaultString the default to return if the input is empty or null
90+
* @return the passed in CharSequence, or the defaultString
91+
*/
92+
public static <T extends CharSequence> T defaultIfEmptyOrNull(final T s, final T defaultString) {
93+
return isEmptyOrNull(s) ? defaultString : s;
94+
}
95+
96+
/**
97+
* Tests if a CharSequence is null, empty, or contains only whitespace.
98+
*
99+
* @param s the CharSequence to check
100+
* @return true if a CharSequence is null, empty, or contains only whitespace
101+
*/
102+
public static boolean isBlank(final CharSequence s) {
103+
if (s == null) {
104+
return true;
105+
}
106+
107+
final int length = s.length();
108+
if (length == 0) {
109+
return true;
110+
}
111+
112+
for (int i = 0; i < length; i++) {
113+
if (!Character.isWhitespace(s.charAt(i))) {
114+
return false;
115+
}
116+
}
117+
return true;
118+
}
119+
120+
/**
121+
* Tests if a CharSequence is NOT null, empty, or contains only whitespace.
122+
*
123+
* @param s the CharSequence to check
124+
* @return false if a CharSequence is null, empty, or contains only whitespace
125+
*/
126+
public static boolean isNotBlank(final CharSequence s) {
127+
if (s == null) {
128+
return false;
129+
}
130+
131+
final int length = s.length();
132+
if (length == 0) {
133+
return false;
134+
}
135+
136+
for (int i = 0; i < length; i++) {
137+
if (!Character.isWhitespace(s.charAt(i))) {
138+
return true;
139+
}
140+
}
141+
return false;
142+
}
143+
73144
/**
74145
* @param expected the char that we expect
75146
* @param s the string to check
@@ -687,7 +758,7 @@ public static String[] splitAtComma(final String str) {
687758
* Splits the provided text into an array, using comma or blank as the
688759
* separator.
689760
*
690-
* @param str the String to parse, may be null
761+
* @param str the String to parse, may be null
691762
* @return an array of parsed Strings, an empty array if null String input
692763
*/
693764
public static String[] splitAtCommaOrBlank(final String str) {

src/test/java/org/htmlunit/util/StringUtilsTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,67 @@ public void isEmptyString() throws Exception {
4545
assertFalse(StringUtils.isEmptyString("string"));
4646
}
4747

48+
/**
49+
* @throws Exception if the test fails
50+
*/
51+
@Test
52+
public void isEmptyOrNull() throws Exception {
53+
assertTrue(StringUtils.isEmptyOrNull(null));
54+
assertTrue(StringUtils.isEmptyOrNull(""));
55+
assertFalse(StringUtils.isEmptyOrNull(" "));
56+
assertFalse(StringUtils.isEmptyOrNull("\t"));
57+
assertFalse(StringUtils.isEmptyOrNull("\r"));
58+
assertFalse(StringUtils.isEmptyOrNull("\n"));
59+
assertFalse(StringUtils.isEmptyOrNull("string"));
60+
}
61+
62+
63+
/**
64+
* @throws Exception if the test fails
65+
*/
66+
@Test
67+
public void defaultIfEmptyOrNull() throws Exception {
68+
assertEquals("X", StringUtils.defaultIfEmptyOrNull(null, "X"));
69+
assertEquals("X", StringUtils.defaultIfEmptyOrNull("", "X"));
70+
71+
assertEquals(" ", StringUtils.defaultIfEmptyOrNull(" ", "X"));
72+
assertEquals("a", StringUtils.defaultIfEmptyOrNull("a", "X"));
73+
}
74+
75+
/**
76+
* @throws Exception if the test fails
77+
*/
78+
@Test
79+
public void isBlank() throws Exception {
80+
assertTrue(StringUtils.isBlank(null));
81+
assertTrue(StringUtils.isBlank(""));
82+
assertTrue(StringUtils.isBlank(" "));
83+
assertTrue(StringUtils.isBlank("\t"));
84+
assertTrue(StringUtils.isBlank("\r"));
85+
assertTrue(StringUtils.isBlank("\n"));
86+
87+
assertFalse(StringUtils.isBlank("x"));
88+
assertFalse(StringUtils.isBlank("string"));
89+
assertFalse(StringUtils.isBlank(" x"));
90+
}
91+
92+
/**
93+
* @throws Exception if the test fails
94+
*/
95+
@Test
96+
public void isNotBlank() throws Exception {
97+
assertFalse(StringUtils.isNotBlank(null));
98+
assertFalse(StringUtils.isNotBlank(""));
99+
assertFalse(StringUtils.isNotBlank(" "));
100+
assertFalse(StringUtils.isNotBlank("\t"));
101+
assertFalse(StringUtils.isNotBlank("\r"));
102+
assertFalse(StringUtils.isNotBlank("\n"));
103+
104+
assertTrue(StringUtils.isNotBlank("x"));
105+
assertTrue(StringUtils.isNotBlank("string"));
106+
assertTrue(StringUtils.isNotBlank(" x"));
107+
}
108+
48109
/**
49110
* @throws Exception if the test fails
50111
*/

0 commit comments

Comments
 (0)