am b136b7dd: Merge "Add sanity check to KeybaordTextsTable for debugging"

* commit 'b136b7dd6f0757e3988639b0794c04d50b4087da':
  Add sanity check to KeybaordTextsTable for debugging
main
Tadashi G. Takaoka 2014-02-14 01:54:48 -08:00 committed by Android Git Automerger
commit 56120831a6
3 changed files with 46 additions and 16 deletions

View File

@ -85,13 +85,13 @@ public final class KeyboardTextsSet {
do { do {
level++; level++;
if (level >= MAX_STRING_REFERENCE_INDIRECTION) { if (level >= MAX_STRING_REFERENCE_INDIRECTION) {
throw new RuntimeException("too many @string/resource indirection: " + text); throw new RuntimeException("Too many " + PREFIX_TEXT + "name indirection: " + text);
} }
final int prefixLen = PREFIX_TEXT.length(); final int prefixLen = PREFIX_TEXT.length();
final int size = text.length(); final int size = text.length();
if (size < prefixLen) { if (size < prefixLen) {
return TextUtils.isEmpty(text) ? null : text; break;
} }
sb = null; sb = null;

View File

@ -43,22 +43,36 @@ import java.util.HashMap;
public final class KeyboardTextsTable { public final class KeyboardTextsTable {
// Name to index map. // Name to index map.
private static final HashMap<String, Integer> sNameToIndexesMap = CollectionUtils.newHashMap(); private static final HashMap<String, Integer> sNameToIndexesMap = CollectionUtils.newHashMap();
// Language to texts map. // Language to texts table map.
private static final HashMap<String, String[]> sLanguageToTextsMap = private static final HashMap<String, String[]> sLanguageToTextsTableMap =
CollectionUtils.newHashMap();
// TODO: Remove this variable after debugging.
// Texts table to language maps.
private static final HashMap<String[], String> sTextsTableToLanguageMap =
CollectionUtils.newHashMap(); CollectionUtils.newHashMap();
public static String getText(final String name, final String[] textsTable) { public static String getText(final String name, final String[] textsTable) {
final Integer indexObj = sNameToIndexesMap.get(name); final Integer indexObj = sNameToIndexesMap.get(name);
if (indexObj == null) { if (indexObj == null) {
throw new RuntimeException("Unknown text name: " + name); throw new RuntimeException("Unknown text name=" + name + " language="
+ sTextsTableToLanguageMap.get(textsTable));
} }
final int index = indexObj; final int index = indexObj;
final String text = (index < textsTable.length) ? textsTable[index] : null; final String text = (index < textsTable.length) ? textsTable[index] : null;
return (text != null) ? text : LANGUAGE_DEFAULT[index]; if (text != null) {
return text;
}
// Sanity check.
if (index >= 0 && index < LANGUAGE_DEFAULT.length) {
return LANGUAGE_DEFAULT[index];
}
// Throw exception for debugging purpose.
throw new RuntimeException("Illegal index=" + index + " for name=" + name
+ " language=" + sTextsTableToLanguageMap.get(textsTable));
} }
public static String[] getTextsTable(final String language) { public static String[] getTextsTable(final String language) {
final String[] textsTable = sLanguageToTextsMap.get(language); final String[] textsTable = sLanguageToTextsTableMap.get(language);
return textsTable != null ? textsTable : LANGUAGE_DEFAULT; return textsTable != null ? textsTable : LANGUAGE_DEFAULT;
} }
@ -3632,8 +3646,9 @@ public final class KeyboardTextsTable {
for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) { for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) {
final String language = (String)LANGUAGES_AND_TEXTS[i]; final String language = (String)LANGUAGES_AND_TEXTS[i];
final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1]; final String[] textsTable = (String[])LANGUAGES_AND_TEXTS[i + 1];
sLanguageToTextsMap.put(language, texts); sLanguageToTextsTableMap.put(language, textsTable);
sTextsTableToLanguageMap.put(textsTable, language);
} }
} }
} }

View File

@ -43,22 +43,36 @@ import java.util.HashMap;
public final class KeyboardTextsTable { public final class KeyboardTextsTable {
// Name to index map. // Name to index map.
private static final HashMap<String, Integer> sNameToIndexesMap = CollectionUtils.newHashMap(); private static final HashMap<String, Integer> sNameToIndexesMap = CollectionUtils.newHashMap();
// Language to texts map. // Language to texts table map.
private static final HashMap<String, String[]> sLanguageToTextsMap = private static final HashMap<String, String[]> sLanguageToTextsTableMap =
CollectionUtils.newHashMap();
// TODO: Remove this variable after debugging.
// Texts table to language maps.
private static final HashMap<String[], String> sTextsTableToLanguageMap =
CollectionUtils.newHashMap(); CollectionUtils.newHashMap();
public static String getText(final String name, final String[] textsTable) { public static String getText(final String name, final String[] textsTable) {
final Integer indexObj = sNameToIndexesMap.get(name); final Integer indexObj = sNameToIndexesMap.get(name);
if (indexObj == null) { if (indexObj == null) {
throw new RuntimeException("Unknown text name: " + name); throw new RuntimeException("Unknown text name=" + name + " language="
+ sTextsTableToLanguageMap.get(textsTable));
} }
final int index = indexObj; final int index = indexObj;
final String text = (index < textsTable.length) ? textsTable[index] : null; final String text = (index < textsTable.length) ? textsTable[index] : null;
return (text != null) ? text : LANGUAGE_DEFAULT[index]; if (text != null) {
return text;
}
// Sanity check.
if (index >= 0 && index < LANGUAGE_DEFAULT.length) {
return LANGUAGE_DEFAULT[index];
}
// Throw exception for debugging purpose.
throw new RuntimeException("Illegal index=" + index + " for name=" + name
+ " language=" + sTextsTableToLanguageMap.get(textsTable));
} }
public static String[] getTextsTable(final String language) { public static String[] getTextsTable(final String language) {
final String[] textsTable = sLanguageToTextsMap.get(language); final String[] textsTable = sLanguageToTextsTableMap.get(language);
return textsTable != null ? textsTable : LANGUAGE_DEFAULT; return textsTable != null ? textsTable : LANGUAGE_DEFAULT;
} }
@ -89,8 +103,9 @@ public final class KeyboardTextsTable {
for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) { for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) {
final String language = (String)LANGUAGES_AND_TEXTS[i]; final String language = (String)LANGUAGES_AND_TEXTS[i];
final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1]; final String[] textsTable = (String[])LANGUAGES_AND_TEXTS[i + 1];
sLanguageToTextsMap.put(language, texts); sLanguageToTextsTableMap.put(language, textsTable);
sTextsTableToLanguageMap.put(textsTable, language);
} }
} }
} }