Rename "csv" in StringUtils to "commaConcatenatedText"

Change-Id: I5a32882a088e4ff5bfe973a86465d6ff839113d9
main
Tadashi G. Takaoka 2013-05-28 13:19:16 +09:00
parent a2d8d30f8e
commit 1b46738796
4 changed files with 73 additions and 48 deletions

View File

@ -49,13 +49,14 @@ public final class AdditionalSubtype {
&& SubtypeLocale.isExceptionalLocale(localeString)) {
final String layoutDisplayName = SubtypeLocale.getKeyboardLayoutSetDisplayName(
keyboardLayoutSetName);
layoutDisplayNameExtraValue = StringUtils.appendToCsvIfNotExists(
layoutDisplayNameExtraValue = StringUtils.appendToCommaConcatenatedTextIfNotExists(
UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME + "=" + layoutDisplayName, extraValue);
} else {
layoutDisplayNameExtraValue = extraValue;
}
final String additionalSubtypeExtraValue = StringUtils.appendToCsvIfNotExists(
IS_ADDITIONAL_SUBTYPE, layoutDisplayNameExtraValue);
final String additionalSubtypeExtraValue =
StringUtils.appendToCommaConcatenatedTextIfNotExists(
IS_ADDITIONAL_SUBTYPE, layoutDisplayNameExtraValue);
final int nameId = SubtypeLocale.getSubtypeNameId(localeString, keyboardLayoutSetName);
return new InputMethodSubtype(nameId, R.drawable.ic_subtype_keyboard,
localeString, KEYBOARD_MODE,
@ -66,8 +67,9 @@ public final class AdditionalSubtype {
final String localeString = subtype.getLocale();
final String keyboardLayoutSetName = SubtypeLocale.getKeyboardLayoutSetName(subtype);
final String layoutExtraValue = KEYBOARD_LAYOUT_SET + "=" + keyboardLayoutSetName;
final String extraValue = StringUtils.removeFromCsvIfExists(layoutExtraValue,
StringUtils.removeFromCsvIfExists(IS_ADDITIONAL_SUBTYPE, subtype.getExtraValue()));
final String extraValue = StringUtils.removeFromCommaConcatenatedTextIfExists(
layoutExtraValue, StringUtils.removeFromCommaConcatenatedTextIfExists(
IS_ADDITIONAL_SUBTYPE, subtype.getExtraValue()));
final String basePrefSubtype = localeString + LOCALE_AND_LAYOUT_SEPARATOR
+ keyboardLayoutSetName;
return extraValue.isEmpty() ? basePrefSubtype

View File

@ -199,6 +199,7 @@ public final class InputAttributes {
if (editorInfo == null) return false;
final String findingKey = (packageName != null) ? packageName + "." + key
: key;
return StringUtils.containsInCsv(findingKey, editorInfo.privateImeOptions);
return StringUtils.containsInCommaConcatenatedText(
findingKey, editorInfo.privateImeOptions);
}
}

View File

@ -35,33 +35,50 @@ public final class StringUtils {
return text.codePointCount(0, text.length());
}
public static boolean containsInArray(final String key, final String[] array) {
public static boolean containsInArray(final String text, final String[] array) {
for (final String element : array) {
if (key.equals(element)) return true;
if (text.equals(element)) return true;
}
return false;
}
public static boolean containsInCsv(final String key, final String csv) {
if (TextUtils.isEmpty(csv)) return false;
return containsInArray(key, csv.split(","));
private static final String SEPARATOR_FOR_COMMA_CONCATENATED_TEXT = ",";
public static boolean containsInCommaConcatenatedText(final String text,
final String extraValues) {
if (TextUtils.isEmpty(extraValues)) {
return false;
}
return containsInArray(text, extraValues.split(SEPARATOR_FOR_COMMA_CONCATENATED_TEXT));
}
public static String appendToCsvIfNotExists(final String key, final String csv) {
if (TextUtils.isEmpty(csv)) return key;
if (containsInCsv(key, csv)) return csv;
return csv + "," + key;
public static String appendToCommaConcatenatedTextIfNotExists(final String text,
final String extraValues) {
if (TextUtils.isEmpty(extraValues)) {
return text;
}
if (containsInCommaConcatenatedText(text, extraValues)) {
return extraValues;
}
return extraValues + SEPARATOR_FOR_COMMA_CONCATENATED_TEXT + text;
}
public static String removeFromCsvIfExists(final String key, final String csv) {
if (TextUtils.isEmpty(csv)) return "";
final String[] elements = csv.split(",");
if (!containsInArray(key, elements)) return csv;
public static String removeFromCommaConcatenatedTextIfExists(final String text,
final String extraValues) {
if (TextUtils.isEmpty(extraValues)) {
return "";
}
final String[] elements = extraValues.split(SEPARATOR_FOR_COMMA_CONCATENATED_TEXT);
if (!containsInArray(text, elements)) {
return extraValues;
}
final ArrayList<String> result = CollectionUtils.newArrayList(elements.length - 1);
for (final String element : elements) {
if (!key.equals(element)) result.add(element);
if (!text.equals(element)) {
result.add(element);
}
}
return TextUtils.join(",", result);
return TextUtils.join(SEPARATOR_FOR_COMMA_CONCATENATED_TEXT, result);
}
/**

View File

@ -40,57 +40,62 @@ public class StringUtilsTests extends AndroidTestCase {
}));
}
public void testContainsInCsv() {
assertFalse("null", StringUtils.containsInCsv("key", null));
assertFalse("empty", StringUtils.containsInCsv("key", ""));
assertFalse("not in 1 element", StringUtils.containsInCsv("key", "key1"));
assertFalse("not in 2 elements", StringUtils.containsInCsv("key", "key1,key2"));
public void testContainsInExtraValues() {
assertFalse("null", StringUtils.containsInCommaConcatenatedText("key", null));
assertFalse("empty", StringUtils.containsInCommaConcatenatedText("key", ""));
assertFalse("not in 1 element",
StringUtils.containsInCommaConcatenatedText("key", "key1"));
assertFalse("not in 2 elements",
StringUtils.containsInCommaConcatenatedText("key", "key1,key2"));
assertTrue("in 1 element", StringUtils.containsInCsv("key", "key"));
assertTrue("in 2 elements", StringUtils.containsInCsv("key", "key1,key"));
assertTrue("in 1 element", StringUtils.containsInCommaConcatenatedText("key", "key"));
assertTrue("in 2 elements", StringUtils.containsInCommaConcatenatedText("key", "key1,key"));
}
public void testAppendToCsvIfNotExists() {
assertEquals("null", "key", StringUtils.appendToCsvIfNotExists("key", null));
assertEquals("empty", "key", StringUtils.appendToCsvIfNotExists("key", ""));
public void testAppendToExtraValuesIfNotExists() {
assertEquals("null", "key",
StringUtils.appendToCommaConcatenatedTextIfNotExists("key", null));
assertEquals("empty", "key",
StringUtils.appendToCommaConcatenatedTextIfNotExists("key", ""));
assertEquals("not in 1 element", "key1,key",
StringUtils.appendToCsvIfNotExists("key", "key1"));
StringUtils.appendToCommaConcatenatedTextIfNotExists("key", "key1"));
assertEquals("not in 2 elements", "key1,key2,key",
StringUtils.appendToCsvIfNotExists("key", "key1,key2"));
StringUtils.appendToCommaConcatenatedTextIfNotExists("key", "key1,key2"));
assertEquals("in 1 element", "key",
StringUtils.appendToCsvIfNotExists("key", "key"));
StringUtils.appendToCommaConcatenatedTextIfNotExists("key", "key"));
assertEquals("in 2 elements at position 1", "key,key2",
StringUtils.appendToCsvIfNotExists("key", "key,key2"));
StringUtils.appendToCommaConcatenatedTextIfNotExists("key", "key,key2"));
assertEquals("in 2 elements at position 2", "key1,key",
StringUtils.appendToCsvIfNotExists("key", "key1,key"));
StringUtils.appendToCommaConcatenatedTextIfNotExists("key", "key1,key"));
assertEquals("in 3 elements at position 2", "key1,key,key3",
StringUtils.appendToCsvIfNotExists("key", "key1,key,key3"));
StringUtils.appendToCommaConcatenatedTextIfNotExists("key", "key1,key,key3"));
}
public void testRemoveFromCsvIfExists() {
assertEquals("null", "", StringUtils.removeFromCsvIfExists("key", null));
assertEquals("empty", "", StringUtils.removeFromCsvIfExists("key", ""));
public void testRemoveFromExtraValuesIfExists() {
assertEquals("null", "", StringUtils.removeFromCommaConcatenatedTextIfExists("key", null));
assertEquals("empty", "", StringUtils.removeFromCommaConcatenatedTextIfExists("key", ""));
assertEquals("not in 1 element", "key1",
StringUtils.removeFromCsvIfExists("key", "key1"));
StringUtils.removeFromCommaConcatenatedTextIfExists("key", "key1"));
assertEquals("not in 2 elements", "key1,key2",
StringUtils.removeFromCsvIfExists("key", "key1,key2"));
StringUtils.removeFromCommaConcatenatedTextIfExists("key", "key1,key2"));
assertEquals("in 1 element", "",
StringUtils.removeFromCsvIfExists("key", "key"));
StringUtils.removeFromCommaConcatenatedTextIfExists("key", "key"));
assertEquals("in 2 elements at position 1", "key2",
StringUtils.removeFromCsvIfExists("key", "key,key2"));
StringUtils.removeFromCommaConcatenatedTextIfExists("key", "key,key2"));
assertEquals("in 2 elements at position 2", "key1",
StringUtils.removeFromCsvIfExists("key", "key1,key"));
StringUtils.removeFromCommaConcatenatedTextIfExists("key", "key1,key"));
assertEquals("in 3 elements at position 2", "key1,key3",
StringUtils.removeFromCsvIfExists("key", "key1,key,key3"));
StringUtils.removeFromCommaConcatenatedTextIfExists("key", "key1,key,key3"));
assertEquals("in 3 elements at position 1,2,3", "",
StringUtils.removeFromCsvIfExists("key", "key,key,key"));
StringUtils.removeFromCommaConcatenatedTextIfExists("key", "key,key,key"));
assertEquals("in 5 elements at position 2,4", "key1,key3,key5",
StringUtils.removeFromCsvIfExists("key", "key1,key,key3,key,key5"));
StringUtils.removeFromCommaConcatenatedTextIfExists(
"key", "key1,key,key3,key,key5"));
}