FEAT StringUtils + tests
parent
ecefdbb1e1
commit
547bbfb970
|
@ -0,0 +1,78 @@
|
|||
package de.nclazz.commons;
|
||||
|
||||
public abstract class StringUtils {
|
||||
|
||||
private StringUtils() { /* no-op */ }
|
||||
|
||||
/**
|
||||
* Checks if a String is null or does not contain a character
|
||||
* other than whitespace.
|
||||
*
|
||||
* @param s String to check
|
||||
* @return {@code true} if String is empty
|
||||
*/
|
||||
public static boolean isEmpty(String s) {
|
||||
if(s == null) {
|
||||
return true;
|
||||
}
|
||||
if(s.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
for(int i = 0; i < s.length(); i++) {
|
||||
if(!Character.isWhitespace(s.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes a String, meaning only the first letter becomes an
|
||||
* upper case letter if not already.
|
||||
* <p>i.e. {@code fooBar} -> {@code FooBar}</p>
|
||||
* @param s String to capitalize
|
||||
* @return capitalized String
|
||||
* @see #decapitalize(String)
|
||||
*/
|
||||
public static String capitalize(String s) {
|
||||
if(s != null) {
|
||||
s = s.substring(0, 1).toUpperCase() + s.substring(1);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decapitalizes a String, meaning only the first letter becomes a
|
||||
*lower case letter if not already.
|
||||
* <p>i.e. {@code FooBar} -> {@code fooBar}</p>
|
||||
* @param s String to decapitalize
|
||||
* @return decapitalized String
|
||||
* @see #capitalize(String)
|
||||
*/
|
||||
public static String decapitalize(String s) {
|
||||
if(s != null) {
|
||||
s = s.substring(0, 1).toLowerCase() + s.substring(1);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String center(String s, int size) {
|
||||
return center(s, size, ' ');
|
||||
}
|
||||
|
||||
public static String center(String s, int size, char pad) {
|
||||
if (s == null || size <= s.length())
|
||||
return s;
|
||||
|
||||
StringBuilder sb = new StringBuilder(size);
|
||||
for (int i = 0; i < (size - s.length()) / 2; i++) {
|
||||
sb.append(pad);
|
||||
}
|
||||
sb.append(s);
|
||||
while (sb.length() < size) {
|
||||
sb.append(pad);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package de.nclazz.commons;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class StringUtilsTests {
|
||||
|
||||
@Test
|
||||
void testStringIsEmpty_Success() {
|
||||
String[] testStrings = {
|
||||
"",
|
||||
null,
|
||||
" ",
|
||||
"\n",
|
||||
"\r",
|
||||
"\n\r",
|
||||
"\n \r "
|
||||
};
|
||||
|
||||
for(String str : testStrings) {
|
||||
assertTrue(StringUtils.isEmpty(str));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStringIsEmpty_Fails() {
|
||||
String[] testStrings = {
|
||||
"a",
|
||||
" aa ",
|
||||
"\na",
|
||||
"\ra",
|
||||
"\na\r",
|
||||
"\n a \r "
|
||||
};
|
||||
|
||||
for(String str : testStrings) {
|
||||
assertFalse(StringUtils.isEmpty(str));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStringCapitalize() {
|
||||
String[][] testStrings = {
|
||||
{ "foobar", "Foobar" },
|
||||
{ "Foobar", "Foobar" },
|
||||
{ "FOOBAR", "FOOBAR" },
|
||||
{ "fOOBAR", "FOOBAR" },
|
||||
{ "1foobar", "1foobar" },
|
||||
{ "_foobar", "_foobar" },
|
||||
{ "?foobar", "?foobar" },
|
||||
{ "f?oobar", "F?oobar" },
|
||||
};
|
||||
|
||||
for(String[] arr : testStrings) {
|
||||
assertEquals(arr[1], StringUtils.capitalize(arr[0]));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStringDecapitalize() {
|
||||
String[][] testStrings = {
|
||||
{ "foobar", "Foobar" },
|
||||
{ "foobar", "Foobar" },
|
||||
{ "fOOBAR", "FOOBAR" },
|
||||
{ "fOOBAR", "FOOBAR" },
|
||||
{ "1foobar", "1foobar" },
|
||||
{ "_foobar", "_foobar" },
|
||||
{ "?foobar", "?foobar" },
|
||||
{ "f?oobar", "F?oobar" },
|
||||
};
|
||||
|
||||
for(String[] arr : testStrings) {
|
||||
assertEquals(arr[0], StringUtils.decapitalize(arr[1]));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue