diff options
author | Markus Scherer <markus.icu@gmail.com> | 2015-12-07 23:17:26 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2015-12-07 23:17:26 +0000 |
commit | 0516ae25160adb56944ae37c271ed1096d03c096 (patch) | |
tree | f875cda8b44b525d72187be79016e67024ec8293 | |
parent | 59900c99a37d2338b4794e8282b204727ed3f2cc (diff) | |
parent | 1f9cd7c572376201ae180817f98b8a674b134274 (diff) | |
download | icu-0516ae25160adb56944ae37c271ed1096d03c096.tar.gz |
Cherry-pick: ticket:11986: slightly nicer QuantityFormatter.getPluralIndex(), avoid key.toString() for it
am: 1f9cd7c572
* commit '1f9cd7c572376201ae180817f98b8a674b134274':
Cherry-pick: ticket:11986: slightly nicer QuantityFormatter.getPluralIndex(), avoid key.toString() for it
-rw-r--r-- | icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java | 2 | ||||
-rw-r--r-- | icu4j/main/classes/core/src/com/ibm/icu/text/QuantityFormatter.java | 39 |
2 files changed, 22 insertions, 19 deletions
diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java index 7899bceda..7e9f0d8d6 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/MeasureFormat.java @@ -760,7 +760,7 @@ public class MeasureFormat extends UFormat { // one{"{0} hr"} // other{"{0} hrs"} if (!hasPatterns) { - builder.add(key.toString(), value.getString()); + builder.add(key, value.getString()); } } } diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/QuantityFormatter.java b/icu4j/main/classes/core/src/com/ibm/icu/text/QuantityFormatter.java index e72f19967..17b9a8288 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/QuantityFormatter.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/QuantityFormatter.java @@ -21,28 +21,31 @@ class QuantityFormatter { * Plural forms in index order: "other", "zero", "one", "two", "few", "many" * "other" must be first. */ - private static final int getPluralIndex(String pluralForm) { - if (pluralForm.equals("other")) { - return 0; - } - if (pluralForm.length() == 3) { - if (pluralForm.equals("one")) { + private static final int getPluralIndex(CharSequence pluralForm) { + switch (pluralForm.length()) { + case 3: + if ("one".contentEquals(pluralForm)) { return 2; - } - if (pluralForm.equals("two")) { + } else if ("two".contentEquals(pluralForm)) { return 3; - } - if (pluralForm.equals("few")) { + } else if ("few".contentEquals(pluralForm)) { return 4; } - } - if (pluralForm.length() == 4) { - if (pluralForm.equals("many")) { + break; + case 4: + if ("many".contentEquals(pluralForm)) { return 5; - } - if (pluralForm.equals("zero")) { + } else if ("zero".contentEquals(pluralForm)) { return 1; } + break; + case 5: + if ("other".contentEquals(pluralForm)) { + return 0; + } + break; + default: + break; } return -1; } @@ -70,10 +73,10 @@ class QuantityFormatter { * @throws IllegalArgumentException if variant is not recognized or * if template has more than just the {0} placeholder. */ - public Builder add(String variant, String template) { + public Builder add(CharSequence variant, String template) { int idx = getPluralIndex(variant); if (idx < 0) { - throw new IllegalArgumentException(variant); + throw new IllegalArgumentException(variant.toString()); } SimplePatternFormatter newT = SimplePatternFormatter.compile(template); if (newT.getPlaceholderCount() > 1) { @@ -142,7 +145,7 @@ class QuantityFormatter { * @param variant "zero", "one", "two", "few", "many", "other" * @return the SimplePatternFormatter */ - public SimplePatternFormatter getByVariant(String variant) { + public SimplePatternFormatter getByVariant(CharSequence variant) { int idx = getPluralIndex(variant); SimplePatternFormatter template = templates[idx < 0 ? 0 : idx]; return template == null ? templates[0] : template; |