am 053424e5: Merge "Add StringUtils.joinCommaSplittableText"
* commit '053424e56e7986e573ae4a43e79d52feb1513d34': Add StringUtils.joinCommaSplittableTextmain
commit
7e860d7d7a
|
@ -39,6 +39,8 @@ public final class StringUtils {
|
|||
public static final int CAPITALIZE_FIRST = 1; // First only
|
||||
public static final int CAPITALIZE_ALL = 2; // All caps
|
||||
|
||||
private static final String EMPTY_STRING = "";
|
||||
|
||||
private StringUtils() {
|
||||
// This utility class is not publicly instantiable.
|
||||
}
|
||||
|
@ -80,6 +82,22 @@ public final class StringUtils {
|
|||
return containsInArray(text, extraValues.split(SEPARATOR_FOR_COMMA_SPLITTABLE_TEXT));
|
||||
}
|
||||
|
||||
// Remove @UsedForTesting annotation once this method is used in the production code.
|
||||
@UsedForTesting
|
||||
public static String joinCommaSplittableText(final String head, final String tail) {
|
||||
if (TextUtils.isEmpty(head) && TextUtils.isEmpty(tail)) {
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
// Here either head or tail is not null.
|
||||
if (TextUtils.isEmpty(head)) {
|
||||
return tail;
|
||||
}
|
||||
if (TextUtils.isEmpty(tail)) {
|
||||
return head;
|
||||
}
|
||||
return head + SEPARATOR_FOR_COMMA_SPLITTABLE_TEXT + tail;
|
||||
}
|
||||
|
||||
public static String appendToCommaSplittableTextIfNotExists(final String text,
|
||||
final String extraValues) {
|
||||
if (TextUtils.isEmpty(extraValues)) {
|
||||
|
@ -94,7 +112,7 @@ public final class StringUtils {
|
|||
public static String removeFromCommaSplittableTextIfExists(final String text,
|
||||
final String extraValues) {
|
||||
if (TextUtils.isEmpty(extraValues)) {
|
||||
return "";
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
final String[] elements = extraValues.split(SEPARATOR_FOR_COMMA_SPLITTABLE_TEXT);
|
||||
if (!containsInArray(text, elements)) {
|
||||
|
@ -380,7 +398,7 @@ public final class StringUtils {
|
|||
@UsedForTesting
|
||||
public static String byteArrayToHexString(byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
return "";
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
for (byte b : bytes) {
|
||||
|
@ -444,7 +462,7 @@ public final class StringUtils {
|
|||
|
||||
public static String listToJsonStr(List<Object> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return "";
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
final StringWriter sw = new StringWriter();
|
||||
final JsonWriter writer = new JsonWriter(sw);
|
||||
|
@ -470,6 +488,6 @@ public final class StringUtils {
|
|||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ public class StringUtilsTests extends AndroidTestCase {
|
|||
}));
|
||||
}
|
||||
|
||||
public void testContainsInExtraValues() {
|
||||
public void testContainsInCommaSplittableText() {
|
||||
assertFalse("null", StringUtils.containsInCommaSplittableText("key", null));
|
||||
assertFalse("empty", StringUtils.containsInCommaSplittableText("key", ""));
|
||||
assertFalse("not in 1 element",
|
||||
|
@ -56,7 +56,28 @@ public class StringUtilsTests extends AndroidTestCase {
|
|||
assertTrue("in 2 elements", StringUtils.containsInCommaSplittableText("key", "key1,key"));
|
||||
}
|
||||
|
||||
public void testAppendToExtraValuesIfNotExists() {
|
||||
public void testJoinCommaSplittableText() {
|
||||
assertEquals("2 nulls", "",
|
||||
StringUtils.joinCommaSplittableText(null, null));
|
||||
assertEquals("null and empty", "",
|
||||
StringUtils.joinCommaSplittableText(null, ""));
|
||||
assertEquals("empty and null", "",
|
||||
StringUtils.joinCommaSplittableText("", null));
|
||||
assertEquals("2 empties", "",
|
||||
StringUtils.joinCommaSplittableText("", ""));
|
||||
assertEquals("text and null", "text",
|
||||
StringUtils.joinCommaSplittableText("text", null));
|
||||
assertEquals("text and empty", "text",
|
||||
StringUtils.joinCommaSplittableText("text", ""));
|
||||
assertEquals("null and text", "text",
|
||||
StringUtils.joinCommaSplittableText(null, "text"));
|
||||
assertEquals("empty and text", "text",
|
||||
StringUtils.joinCommaSplittableText("", "text"));
|
||||
assertEquals("2 texts", "text1,text2",
|
||||
StringUtils.joinCommaSplittableText("text1", "text2"));
|
||||
}
|
||||
|
||||
public void testAppendToCommaSplittableTextIfNotExists() {
|
||||
assertEquals("null", "key",
|
||||
StringUtils.appendToCommaSplittableTextIfNotExists("key", null));
|
||||
assertEquals("empty", "key",
|
||||
|
@ -77,7 +98,7 @@ public class StringUtilsTests extends AndroidTestCase {
|
|||
StringUtils.appendToCommaSplittableTextIfNotExists("key", "key1,key,key3"));
|
||||
}
|
||||
|
||||
public void testRemoveFromExtraValuesIfExists() {
|
||||
public void testRemoveFromCommaSplittableTextIfExists() {
|
||||
assertEquals("null", "", StringUtils.removeFromCommaSplittableTextIfExists("key", null));
|
||||
assertEquals("empty", "", StringUtils.removeFromCommaSplittableTextIfExists("key", ""));
|
||||
|
||||
|
|
Loading…
Reference in New Issue