Add StringUtils.joinCommaSplittableText
Change-Id: I80ad9d8b5aee53f1403486a56ffae8d09cbd4e28
This commit is contained in:
parent
c3a4c075c9
commit
6589751ace
2 changed files with 46 additions and 7 deletions
|
@ -39,6 +39,8 @@ public final class StringUtils {
|
||||||
public static final int CAPITALIZE_FIRST = 1; // First only
|
public static final int CAPITALIZE_FIRST = 1; // First only
|
||||||
public static final int CAPITALIZE_ALL = 2; // All caps
|
public static final int CAPITALIZE_ALL = 2; // All caps
|
||||||
|
|
||||||
|
private static final String EMPTY_STRING = "";
|
||||||
|
|
||||||
private StringUtils() {
|
private StringUtils() {
|
||||||
// This utility class is not publicly instantiable.
|
// 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));
|
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,
|
public static String appendToCommaSplittableTextIfNotExists(final String text,
|
||||||
final String extraValues) {
|
final String extraValues) {
|
||||||
if (TextUtils.isEmpty(extraValues)) {
|
if (TextUtils.isEmpty(extraValues)) {
|
||||||
|
@ -94,7 +112,7 @@ public final class StringUtils {
|
||||||
public static String removeFromCommaSplittableTextIfExists(final String text,
|
public static String removeFromCommaSplittableTextIfExists(final String text,
|
||||||
final String extraValues) {
|
final String extraValues) {
|
||||||
if (TextUtils.isEmpty(extraValues)) {
|
if (TextUtils.isEmpty(extraValues)) {
|
||||||
return "";
|
return EMPTY_STRING;
|
||||||
}
|
}
|
||||||
final String[] elements = extraValues.split(SEPARATOR_FOR_COMMA_SPLITTABLE_TEXT);
|
final String[] elements = extraValues.split(SEPARATOR_FOR_COMMA_SPLITTABLE_TEXT);
|
||||||
if (!containsInArray(text, elements)) {
|
if (!containsInArray(text, elements)) {
|
||||||
|
@ -380,7 +398,7 @@ public final class StringUtils {
|
||||||
@UsedForTesting
|
@UsedForTesting
|
||||||
public static String byteArrayToHexString(byte[] bytes) {
|
public static String byteArrayToHexString(byte[] bytes) {
|
||||||
if (bytes == null || bytes.length == 0) {
|
if (bytes == null || bytes.length == 0) {
|
||||||
return "";
|
return EMPTY_STRING;
|
||||||
}
|
}
|
||||||
final StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
for (byte b : bytes) {
|
for (byte b : bytes) {
|
||||||
|
@ -444,7 +462,7 @@ public final class StringUtils {
|
||||||
|
|
||||||
public static String listToJsonStr(List<Object> list) {
|
public static String listToJsonStr(List<Object> list) {
|
||||||
if (list == null || list.isEmpty()) {
|
if (list == null || list.isEmpty()) {
|
||||||
return "";
|
return EMPTY_STRING;
|
||||||
}
|
}
|
||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final JsonWriter writer = new JsonWriter(sw);
|
final JsonWriter writer = new JsonWriter(sw);
|
||||||
|
@ -470,6 +488,6 @@ public final class StringUtils {
|
||||||
} catch (IOException e) {
|
} 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("null", StringUtils.containsInCommaSplittableText("key", null));
|
||||||
assertFalse("empty", StringUtils.containsInCommaSplittableText("key", ""));
|
assertFalse("empty", StringUtils.containsInCommaSplittableText("key", ""));
|
||||||
assertFalse("not in 1 element",
|
assertFalse("not in 1 element",
|
||||||
|
@ -56,7 +56,28 @@ public class StringUtilsTests extends AndroidTestCase {
|
||||||
assertTrue("in 2 elements", StringUtils.containsInCommaSplittableText("key", "key1,key"));
|
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",
|
assertEquals("null", "key",
|
||||||
StringUtils.appendToCommaSplittableTextIfNotExists("key", null));
|
StringUtils.appendToCommaSplittableTextIfNotExists("key", null));
|
||||||
assertEquals("empty", "key",
|
assertEquals("empty", "key",
|
||||||
|
@ -77,7 +98,7 @@ public class StringUtilsTests extends AndroidTestCase {
|
||||||
StringUtils.appendToCommaSplittableTextIfNotExists("key", "key1,key,key3"));
|
StringUtils.appendToCommaSplittableTextIfNotExists("key", "key1,key,key3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRemoveFromExtraValuesIfExists() {
|
public void testRemoveFromCommaSplittableTextIfExists() {
|
||||||
assertEquals("null", "", StringUtils.removeFromCommaSplittableTextIfExists("key", null));
|
assertEquals("null", "", StringUtils.removeFromCommaSplittableTextIfExists("key", null));
|
||||||
assertEquals("empty", "", StringUtils.removeFromCommaSplittableTextIfExists("key", ""));
|
assertEquals("empty", "", StringUtils.removeFromCommaSplittableTextIfExists("key", ""));
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue