From: Yoshito Umaoka Date: Mon, 23 Jun 2014 21:36:55 +0000 (+0000) Subject: ICU-10836 Added missing Locale version APIs and test cases for various classes. X-Git-Tag: milestone-59-0-1~1842 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=30dc6d9bbba6e9fb8963b747afbfbd9336561704;p=icu ICU-10836 Added missing Locale version APIs and test cases for various classes. X-SVN-Rev: 35924 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/lang/UCharacter.java b/icu4j/main/classes/core/src/com/ibm/icu/lang/UCharacter.java index bde5a7747ee..ec8cd72304e 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/lang/UCharacter.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/lang/UCharacter.java @@ -5136,6 +5136,35 @@ public final class UCharacter implements ECharacterCategory, ECharacterDirection return result.toString(); } + /** + * {@icu}

Returns the titlecase version of the argument string.

+ *

Position for titlecasing is determined by the argument break + * iterator, hence the user can customize his break iterator for + * a specialized titlecasing. In this case only the forward iteration + * needs to be implemented. + * If the break iterator passed in is null, the default Unicode algorithm + * will be used to determine the titlecase positions. + *

+ *

Only positions returned by the break iterator will be title cased, + * character in between the positions will all be in lower case.

+ *

Casing is dependent on the argument locale and context-sensitive

+ * @param locale which string is to be converted in + * @param str source string to be performed on + * @param titleIter break iterator to determine the positions in which + * the character should be title cased. + * @param options bit set to modify the titlecasing operation + * @return lowercase version of the argument string + * @see #TITLECASE_NO_LOWERCASE + * @see #TITLECASE_NO_BREAK_ADJUSTMENT + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static String toTitleCase(Locale locale, String str, + BreakIterator titleIter, + int options) { + return toTitleCase(ULocale.forLocale(locale), str, titleIter, options); + } + /** * {@icu} The given character is mapped to its case folding equivalent according * to UnicodeData.txt and CaseFolding.txt; if the character has no case diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyDisplayNames.java b/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyDisplayNames.java index 390ffc9bc9d..86bb5f80d77 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyDisplayNames.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/CurrencyDisplayNames.java @@ -6,6 +6,7 @@ */ package com.ibm.icu.text; +import java.util.Locale; import java.util.Map; import com.ibm.icu.impl.CurrencyData; @@ -35,6 +36,23 @@ public abstract class CurrencyDisplayNames { return CurrencyData.provider.getInstance(locale, true); } + /** + * Return an instance of CurrencyDisplayNames that provides information + * localized for display in the provided locale. If there is no data for the + * provided locale, this falls back to the current default locale; if there + * is no data for that either, it falls back to the root locale. Substitute + * values are returned from APIs when there is no data for the requested ISO + * code. + * + * @param locale the locale into which to localize the names + * @return a CurrencyDisplayNames + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static CurrencyDisplayNames getInstance(Locale locale) { + return getInstance(locale, true); + } + /** * Return an instance of CurrencyDisplayNames that provides information * localized for display in the provided locale. If noSubstitute is false, @@ -53,6 +71,25 @@ public abstract class CurrencyDisplayNames { return CurrencyData.provider.getInstance(locale, !noSubstitute); } + /** + * Return an instance of CurrencyDisplayNames that provides information + * localized for display in the provided locale. If noSubstitute is false, + * this behaves like {@link #getInstance(Locale)}. Otherwise, 1) if there + * is no supporting data for the locale at all, there is no fallback through + * the default locale or root, and null is returned, and 2) if there is data + * for the locale, but not data for the requested ISO code, null is returned + * from those APIs instead of a substitute value. + * + * @param locale the JDK locale into which to localize the names + * @param noSubstitute if true, do not return substitute values. + * @return a CurrencyDisplayNames + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static CurrencyDisplayNames getInstance(Locale locale, boolean noSubstitute) { + return getInstance(ULocale.forLocale(locale), noSubstitute); + } + /** * Returns true if currency display name data is available. * @return true if currency display name data is available diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalInfo.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalInfo.java index 83573394852..8d15a5ed3c8 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalInfo.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DateIntervalInfo.java @@ -12,6 +12,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.MissingResourceException; @@ -326,6 +327,19 @@ public class DateIntervalInfo implements Cloneable, Freezable, } + /** + * Construct DateIntervalInfo for the given JDK locale, + * @param locale the interval patterns are loaded from the appropriate + * calendar data (specified calendar or default calendar) + * in this locale. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public DateIntervalInfo(Locale locale) + { + this(ULocale.forLocale(locale)); + } + /* * Initialize the DateIntervalInfo from locale * @param locale the given locale. diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java index 9602aeabf99..8bee2e64d73 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java @@ -16,6 +16,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; import java.util.Set; @@ -86,6 +87,16 @@ public class DateTimePatternGenerator implements FreezableFORMAT locale. diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/PluralFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/PluralFormat.java index 29781a86cd9..8d80a4d4c1e 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/PluralFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/PluralFormat.java @@ -11,6 +11,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.text.FieldPosition; import java.text.ParsePosition; +import java.util.Locale; import java.util.Map; import com.ibm.icu.impl.Utility; @@ -199,6 +200,18 @@ public class PluralFormat extends UFormat { init(null, PluralType.CARDINAL, ulocale); } + /** + * Creates a new cardinal-number PluralFormat for a given JDK locale. + * @param locale the PluralFormat will be configured with + * rules for this locale. This locale will also be used for standard + * number formatting. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public PluralFormat(Locale locale) { + this(ULocale.forLocale(locale)); + } + /** * Creates a new cardinal-number PluralFormat for a given set of rules. * The standard number formatting will be done using the default FORMAT locale. @@ -224,6 +237,20 @@ public class PluralFormat extends UFormat { init(rules, PluralType.CARDINAL, ulocale); } + /** + * Creates a new cardinal-number PluralFormat for a given set of rules. + * The standard number formatting will be done using the given locale. + * @param locale the default number formatting will be done using this + * locale. + * @param rules defines the behavior of the PluralFormat + * object. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public PluralFormat(Locale locale, PluralRules rules) { + this(ULocale.forLocale(locale), rules); + } + /** * Creates a new PluralFormat for the plural type. * The standard number formatting will be done using the given locale. @@ -236,6 +263,19 @@ public class PluralFormat extends UFormat { init(null, type, ulocale); } + /** + * Creates a new PluralFormat for the plural type. + * The standard number formatting will be done using the given JDK locale. + * @param locale the default number formatting will be done using this + * locale. + * @param type The plural type (e.g., cardinal or ordinal). + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public PluralFormat(Locale locale, PluralType type) { + this(ULocale.forLocale(locale), type); + } + /** * Creates a new cardinal-number PluralFormat for a given pattern string. * The default FORMAT locale will be used to get the set of plural rules and for diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java b/icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java index 2f6c4e2fea4..2540d746d1b 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java @@ -1871,6 +1871,29 @@ public class PluralRules implements Serializable { return Factory.getDefaultFactory().forLocale(locale, PluralType.CARDINAL); } + /** + * Provides access to the predefined cardinal-number PluralRules for a given + * JDK locale. + * Same as forLocale(locale, PluralType.CARDINAL). + * + *

ICU defines plural rules for many locales based on CLDR Language Plural Rules. + * For these predefined rules, see CLDR page at + * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html + * + * @param locale The JDK locale for which a PluralRules object is + * returned. + * @return The predefined PluralRules object for this locale. + * If there's no predefined rules for this locale, the rules + * for the closest parent in the locale hierarchy that has one will + * be returned. The final fallback always returns the default + * rules. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static PluralRules forLocale(Locale locale) { + return forLocale(ULocale.forLocale(locale)); + } + /** * Provides access to the predefined PluralRules for a given * locale and the plural type. @@ -1893,6 +1916,29 @@ public class PluralRules implements Serializable { return Factory.getDefaultFactory().forLocale(locale, type); } + /** + * Provides access to the predefined PluralRules for a given + * JDK locale and the plural type. + * + *

ICU defines plural rules for many locales based on CLDR Language Plural Rules. + * For these predefined rules, see CLDR page at + * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html + * + * @param locale The JDK locale for which a PluralRules object is + * returned. + * @param type The plural type (e.g., cardinal or ordinal). + * @return The predefined PluralRules object for this locale. + * If there's no predefined rules for this locale, the rules + * for the closest parent in the locale hierarchy that has one will + * be returned. The final fallback always returns the default + * rules. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static PluralRules forLocale(Locale locale, PluralType type) { + return forLocale(ULocale.forLocale(locale), type); + } + /* * Checks whether a token is a valid keyword. * diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/RelativeDateTimeFormatter.java b/icu4j/main/classes/core/src/com/ibm/icu/text/RelativeDateTimeFormatter.java index 239e34545c4..ced58a0a0a5 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/RelativeDateTimeFormatter.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/RelativeDateTimeFormatter.java @@ -7,6 +7,7 @@ package com.ibm.icu.text; import java.util.EnumMap; +import java.util.Locale; import com.ibm.icu.impl.CalendarData; import com.ibm.icu.impl.ICUCache; @@ -310,6 +311,7 @@ public final class RelativeDateTimeFormatter { * Returns a RelativeDateTimeFormatter for a particular locale. * * @param locale the locale. + * @return An instance of RelativeDateTimeFormatter. * @draft ICU 53 * @provisional This API might change or be removed in a future release. */ @@ -317,6 +319,18 @@ public final class RelativeDateTimeFormatter { return getInstance(locale, null, Style.LONG, DisplayContext.CAPITALIZATION_NONE); } + /** + * Returns a RelativeDateTimeFormatter for a particular JDK locale. + * + * @param locale the JDK locale. + * @return An instance of RelativeDateTimeFormatter. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static RelativeDateTimeFormatter getInstance(Locale locale) { + return getInstance(ULocale.forLocale(locale)); + } + /** * Returns a RelativeDateTimeFormatter for a particular locale that uses a particular * NumberFormat object. @@ -324,6 +338,7 @@ public final class RelativeDateTimeFormatter { * @param locale the locale * @param nf the number format object. It is defensively copied to ensure thread-safety * and immutability of this class. + * @return An instance of RelativeDateTimeFormatter. * @draft ICU 53 * @provisional This API might change or be removed in a future release. */ @@ -368,6 +383,21 @@ public final class RelativeDateTimeFormatter { } + /** + * Returns a RelativeDateTimeFormatter for a particular JDK locale that uses a particular + * NumberFormat object. + * + * @param locale the JDK locale + * @param nf the number format object. It is defensively copied to ensure thread-safety + * and immutability of this class. + * @return An instance of RelativeDateTimeFormatter. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static RelativeDateTimeFormatter getInstance(Locale locale, NumberFormat nf) { + return getInstance(ULocale.forLocale(locale), nf); + } + /** * Formats a relative date with a quantity such as "in 5 days" or * "3 months ago" @@ -436,7 +466,7 @@ public final class RelativeDateTimeFormatter { /** * Returns a copy of the NumberFormat this object is using. - * + * @return A copy of the NumberFormat. * @draft ICU 53 * @provisional This API might change or be removed in a future release. */ diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/SpoofChecker.java b/icu4j/main/classes/core/src/com/ibm/icu/text/SpoofChecker.java index 2fe4361ec81..c6819a2c97f 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/SpoofChecker.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/SpoofChecker.java @@ -23,6 +23,7 @@ import java.util.Comparator; import java.util.HashSet; import java.util.Hashtable; import java.util.LinkedHashSet; +import java.util.Locale; import java.util.Set; import java.util.Vector; import java.util.regex.Matcher; @@ -573,6 +574,26 @@ public class SpoofChecker { return this; } + /** + * Limit characters that are acceptable in identifiers being checked to those normally used with the languages + * associated with the specified locales. Any previously specified list of locales is replaced by the new + * settings. + * @param locales + * A Set of Locales, from which the language and associated script are extracted. If the locales Set + * is null, no restrictions will be placed on the allowed characters. + * + * @return self + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public Builder setAllowedJavaLocales(Set locales) { + HashSet ulocales = new HashSet(locales.size()); + for (Locale locale : locales) { + ulocales.add(ULocale.forLocale(locale)); + } + return setAllowedLocales(ulocales); + } + // Add (union) to the UnicodeSet all of the characters for the scripts // used for the specified locale. Part of the implementation of // setAllowedLocales. @@ -1450,6 +1471,22 @@ public class SpoofChecker { return fAllowedLocales; } + /** + * Get a list of JDK locales for the scripts that are acceptable in strings to be checked. If no limitations on scripts + * have been specified, an empty set will be returned. + * + * @return A set of locales corresponding to the acceptable scripts. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public Set getAllowedJavaLocales() { + HashSet locales = new HashSet(fAllowedLocales.size()); + for (ULocale uloc : fAllowedLocales) { + locales.add(uloc.toLocale()); + } + return locales; + } + /** * Get a UnicodeSet for the characters permitted in an identifier. This corresponds to the limits imposed by the Set * Allowed Characters functions. Limitations imposed by other checks will not be reflected in the set returned by diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneFormat.java index 82c6e563a64..2219f756a8c 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneFormat.java @@ -24,6 +24,7 @@ import java.util.Date; import java.util.EnumSet; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.MissingResourceException; import java.util.Set; @@ -477,6 +478,21 @@ public class TimeZoneFormat extends UFormat implements Freezable return _tzfCache.getInstance(locale, locale); } + /** + * Returns a frozen instance of TimeZoneFormat for the given JDK locale. + *

Note: The instance returned by this method is frozen. If you want to + * customize a TimeZoneFormat, you must use {@link #cloneAsThawed()} to get a + * thawed copy first. + * + * @param locale the JDK locale. + * @return a frozen instance of TimeZoneFormat for the given locale. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static TimeZoneFormat getInstance(Locale locale) { + return getInstance(ULocale.forLocale(locale)); + } + /** * Returns the time zone display name data used by this instance. * diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneNames.java b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneNames.java index e16448a135e..5aad466d7e3 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneNames.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/TimeZoneNames.java @@ -10,6 +10,7 @@ import java.io.Serializable; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; +import java.util.Locale; import java.util.Set; import com.ibm.icu.impl.ICUConfig; @@ -171,6 +172,19 @@ public abstract class TimeZoneNames implements Serializable { return TZNAMES_CACHE.getInstance(key, locale); } + /** + * Returns an instance of TimeZoneDisplayNames for the specified JDK locale. + * + * @param locale + * The JDK locale. + * @return An instance of TimeZoneDisplayNames + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static TimeZoneNames getInstance(Locale locale) { + return getInstance(ULocale.forLocale(locale)); + } + /** * Returns an immutable set of all available meta zone IDs. * @return An immutable set of all available meta zone IDs. diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java b/icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java index 2d66bcae9a1..c5a0bccd98f 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/Currency.java @@ -194,6 +194,21 @@ public class Currency extends MeasureUnit { return list.toArray(new String[list.size()]); } + /** + * Returns an array of Strings which contain the currency + * identifiers that are valid for the given JDK locale on the + * given date. If there are no such identifiers, returns null. + * Returned identifiers are in preference order. + * @param loc the JDK locale for which to retrieve currency codes. + * @param d the date for which to retrieve currency codes for the given locale. + * @return The array of ISO currency codes. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public static String[] getAvailableCurrencyCodes(Locale loc, Date d) { + return getAvailableCurrencyCodes(ULocale.forLocale(loc), d); + } + /** * Returns the set of available currencies. The returned set of currencies contains all of the * available currencies, including obsolete ones. The result set can be modified without diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java index 38d7c229a69..874d2e4c018 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateIntervalFormatTest.java @@ -1071,7 +1071,14 @@ public class DateIntervalFormatTest extends com.ibm.icu.dev.test.TestFmwk { errln("FAIL: Exception - " + e.getClass().getName()); } } - + + public void TestConstructor() { + DateIntervalInfo diiJapan = new DateIntervalInfo(ULocale.JAPAN); + DateIntervalInfo diiJapanJ = new DateIntervalInfo(Locale.JAPAN); + + assertEquals("DateIntervalInfo constructors", diiJapan, diiJapanJ); + } + /* Tests the method * public boolean equals(Object a) */ diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateTimeGeneratorTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateTimeGeneratorTest.java index c059d79e085..a93194932c2 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateTimeGeneratorTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateTimeGeneratorTest.java @@ -15,6 +15,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Random; import java.util.Set; @@ -1327,43 +1328,53 @@ public class DateTimeGeneratorTest extends TestFmwk { ULocale uloc = new ULocale(localeName); DateTimePatternGenerator dtpgen = DateTimePatternGenerator.getInstance(uloc); for (AllFieldsTestItem testItem: testItems) { - char[] skelBuf = new char[FIELD_LENGTH_MAX]; - for (int chrIndx = 0; chrIndx < FIELD_LENGTH_MAX; chrIndx++) { - skelBuf[chrIndx] = testItem.patternChar; - } - for (int lenIndx = 0; lenIndx < testItem.fieldLengths.length; lenIndx++) { - int skelLen = testItem.fieldLengths[lenIndx]; - if (skelLen > FIELD_LENGTH_MAX) { - continue; - }; - String skeleton = new String(skelBuf, 0, skelLen); - String pattern = dtpgen.getBestPattern(skeleton); - if (pattern.length() <= 0) { + char[] skelBuf = new char[FIELD_LENGTH_MAX]; + for (int chrIndx = 0; chrIndx < FIELD_LENGTH_MAX; chrIndx++) { + skelBuf[chrIndx] = testItem.patternChar; + } + for (int lenIndx = 0; lenIndx < testItem.fieldLengths.length; lenIndx++) { + int skelLen = testItem.fieldLengths[lenIndx]; + if (skelLen > FIELD_LENGTH_MAX) { + continue; + }; + String skeleton = new String(skelBuf, 0, skelLen); + String pattern = dtpgen.getBestPattern(skeleton); + if (pattern.length() <= 0) { errln("DateTimePatternGenerator getBestPattern for locale " + localeName + ", skeleton " + skeleton + ", produces 0-length pattern"); - } else { - // test that resulting pattern has at least one char in mustIncludeOneOf - boolean inQuoted = false; - int patIndx, patLen = pattern.length(); - for (patIndx = 0; patIndx < patLen; patIndx++) { - char c = pattern.charAt(patIndx); - if (c == '\'') { - inQuoted = !inQuoted; - } else if (!inQuoted && c <= 'z' && c >= 'A') { - if (testItem.mustIncludeOneOf.indexOf(c) >= 0) { - break; - } - } - } - if (patIndx >= patLen) { + } else { + // test that resulting pattern has at least one char in mustIncludeOneOf + boolean inQuoted = false; + int patIndx, patLen = pattern.length(); + for (patIndx = 0; patIndx < patLen; patIndx++) { + char c = pattern.charAt(patIndx); + if (c == '\'') { + inQuoted = !inQuoted; + } else if (!inQuoted && c <= 'z' && c >= 'A') { + if (testItem.mustIncludeOneOf.indexOf(c) >= 0) { + break; + } + } + } + if (patIndx >= patLen) { errln("DateTimePatternGenerator getBestPattern for locale " + localeName + ", skeleton " + skeleton + ", produces pattern without required chars: " + pattern); - } - } - } + } + } + } } } } + public void TestJavaLocale() { + DateTimePatternGenerator genUloc = DateTimePatternGenerator.getInstance(ULocale.GERMANY); + DateTimePatternGenerator genLoc = DateTimePatternGenerator.getInstance(Locale.GERMANY); + + final String pat = "yMdHms"; + String patUloc = genUloc.getBestPattern(pat); + String patLoc = genLoc.getBestPattern(pat); + + assertEquals("German pattern 'yMdHms' - getInstance with Java Locale", patUloc, patLoc); + } } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java index c4802c9d5f8..229a08828e3 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java @@ -335,12 +335,22 @@ public class MeasureUnitTest extends TestFmwk { verifyFormatPeriod("en NARROW", mf, narrowData); mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.NUMERIC, nf); verifyFormatPeriod("en NUMERIC", mf, numericData); + nf = NumberFormat.getNumberInstance(ULocale.GERMAN); nf.setMaximumFractionDigits(4); mf = MeasureFormat.getInstance(ULocale.GERMAN, FormatWidth.WIDE, nf); verifyFormatPeriod("de FULL", mf, fullDataDe); mf = MeasureFormat.getInstance(ULocale.GERMAN, FormatWidth.NUMERIC, nf); verifyFormatPeriod("de NUMERIC", mf, numericDataDe); + + // Same tests, with Java Locale + nf = NumberFormat.getNumberInstance(Locale.GERMAN); + nf.setMaximumFractionDigits(4); + mf = MeasureFormat.getInstance(Locale.GERMAN, FormatWidth.WIDE, nf); + verifyFormatPeriod("de FULL(Java Locale)", mf, fullDataDe); + mf = MeasureFormat.getInstance(Locale.GERMAN, FormatWidth.NUMERIC, nf); + verifyFormatPeriod("de NUMERIC(Java Locale)", mf, numericDataDe); + } private void verifyFormatPeriod(String desc, MeasureFormat mf, Object[][] testData) { @@ -718,7 +728,14 @@ public class MeasureUnitTest extends TestFmwk { assertEquals("getNumberFormat", ULocale.ENGLISH, mf.getNumberFormat().getLocale(ULocale.VALID_LOCALE)); assertEquals("getWidth", MeasureFormat.FormatWidth.WIDE, mf.getWidth()); } - + + public void testCurrencyFormatLocale() { + MeasureFormat mfu = MeasureFormat.getCurrencyFormat(ULocale.FRANCE); + MeasureFormat mfj = MeasureFormat.getCurrencyFormat(Locale.FRANCE); + + assertEquals("getCurrencyFormat ULocale/Locale", mfu, mfj); + } + public void testDoubleZero() { ULocale en = new ULocale("en"); NumberFormat nf = NumberFormat.getInstance(en); diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/PluralFormatUnitTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/PluralFormatUnitTest.java index 3f6e040e50b..261b78acce5 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/PluralFormatUnitTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/PluralFormatUnitTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2007-2013, International Business Machines Corporation and + * Copyright (C) 2007-2014, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* */ @@ -11,6 +11,7 @@ import java.text.ParsePosition; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -36,7 +37,7 @@ public class PluralFormatUnitTest extends TestFmwk { public void TestConstructor() { // Test correct formatting of numbers. - PluralFormat plFmts[] = new PluralFormat[8]; + PluralFormat plFmts[] = new PluralFormat[10]; plFmts[0] = new PluralFormat(); plFmts[0].applyPattern("other{#}"); plFmts[1] = new PluralFormat(PluralRules.DEFAULT); @@ -52,6 +53,12 @@ public class PluralFormatUnitTest extends TestFmwk { "other{#}"); plFmts[7] = new PluralFormat(ULocale.getDefault(), "other{#}"); + // Constructors with Java Locale + plFmts[8] = new PluralFormat(Locale.getDefault()); + plFmts[8].applyPattern("other{#}"); + plFmts[9] = new PluralFormat(Locale.getDefault(), PluralRules.DEFAULT); + plFmts[9].applyPattern("other{#}"); + // These plural formats should produce the same output as a // NumberFormat for the default locale. NumberFormat numberFmt = NumberFormat.getInstance(ULocale.getDefault()); diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/PluralRulesTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/PluralRulesTest.java index 4335a56071a..8ba8dbcc947 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/PluralRulesTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/PluralRulesTest.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -1091,4 +1092,14 @@ public class PluralRulesTest extends TestFmwk { } return b.append(']').toString(); } + + public void testJavaLocaleFactory() { + PluralRules rulesU0 = PluralRules.forLocale(ULocale.FRANCE); + PluralRules rulesJ0 = PluralRules.forLocale(Locale.FRANCE); + assertEquals("forLocale()", rulesU0, rulesJ0); + + PluralRules rulesU1 = PluralRules.forLocale(ULocale.FRANCE, PluralType.ORDINAL); + PluralRules rulesJ1 = PluralRules.forLocale(Locale.FRANCE, PluralType.ORDINAL); + assertEquals("forLocale() with type", rulesU1, rulesJ1); + } } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RelativeDateTimeFormatterTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RelativeDateTimeFormatterTest.java index e8d3d9ed09c..e73fcb97c24 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RelativeDateTimeFormatterTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/RelativeDateTimeFormatterTest.java @@ -6,6 +6,8 @@ */ package com.ibm.icu.dev.test.format; +import java.util.Locale; + import com.ibm.icu.dev.test.TestFmwk; import com.ibm.icu.text.DisplayContext; import com.ibm.icu.text.NumberFormat; @@ -713,7 +715,23 @@ public class RelativeDateTimeFormatterTest extends TestFmwk { RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(new ULocale("en_US")); assertEquals("TestcombineDateAndTime", "yesterday, 3:50", fmt.combineDateAndTime("yesterday", "3:50")); } - + + public void TestJavaLocale() { + Locale loc = Locale.US; + double amount = 12.3456d; + + RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance(loc); + String s = fmt.format(amount, Direction.LAST, RelativeUnit.SECONDS); + assertEquals("Java Locale.US", "12.346 seconds ago", s); + + // Modified instance + NumberFormat nf = fmt.getNumberFormat(); + nf.setMaximumFractionDigits(1); + fmt = RelativeDateTimeFormatter.getInstance(loc, nf); + + s = fmt.format(amount, Direction.LAST, RelativeUnit.SECONDS); + assertEquals("Java Locale.US", "12.3 seconds ago", s); + } public void TestGetters() { RelativeDateTimeFormatter fmt = RelativeDateTimeFormatter.getInstance( new ULocale("en_US"), diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeZoneFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeZoneFormatTest.java index cfbb81a2d55..e11a6207f9c 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeZoneFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/TimeZoneFormatTest.java @@ -13,6 +13,7 @@ import java.util.Arrays; import java.util.Date; import java.util.EnumSet; import java.util.List; +import java.util.Locale; import java.util.Set; import java.util.TreeSet; import java.util.regex.Pattern; @@ -772,9 +773,11 @@ public class TimeZoneFormatTest extends com.ibm.icu.dev.test.TestFmwk { }; for (Object[] testCase : TESTDATA) { - TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(new ULocale((String)testCase[0])); TimeZone tz = TimeZone.getTimeZone((String)testCase[1]); Output timeType = new Output(); + + ULocale uloc = new ULocale((String)testCase[0]); + TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(uloc); String out = tzfmt.format((Style)testCase[3], tz, ((Date)testCase[2]).getTime(), timeType); if (!out.equals((String)testCase[4]) || timeType.value != testCase[5]) { @@ -782,6 +785,17 @@ public class TimeZoneFormatTest extends com.ibm.icu.dev.test.TestFmwk { + ",style=" + testCase[3] + "]: expected [output=" + testCase[4] + ",type=" + testCase[5] + "]; actual [output=" + out + ",type=" + timeType.value + "]"); } + + // with equivalent Java Locale + Locale loc = uloc.toLocale(); + tzfmt = TimeZoneFormat.getInstance(loc); + out = tzfmt.format((Style)testCase[3], tz, ((Date)testCase[2]).getTime(), timeType); + + if (!out.equals((String)testCase[4]) || timeType.value != testCase[5]) { + errln("Format result for [locale(Java)=" + testCase[0] + ",tzid=" + testCase[1] + ",date=" + testCase[2] + + ",style=" + testCase[3] + "]: expected [output=" + testCase[4] + ",type=" + testCase[5] + + "]; actual [output=" + out + ",type=" + timeType.value + "]"); + } } } } \ No newline at end of file diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterCaseTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterCaseTest.java index ecbb280df9a..c0d0ad2a19a 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterCaseTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterCaseTest.java @@ -1,6 +1,6 @@ /** ******************************************************************************* -* Copyright (C) 1996-2010, International Business Machines Corporation and +* Copyright (C) 1996-2014, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* */ @@ -358,6 +358,16 @@ public final class UCharacterCaseTest extends TestFmwk "IJssel Igloo IJmuiden", UCharacter.toTitleCase(LOC_DUTCH, "ijssel igloo IJMUIDEN", null)); + // Also check the behavior using Java Locale + Locale JAVALOC_DUTCH = new Locale("nl"); + assertEquals("Dutch titlecase check in English (Java Locale)", + "Ijssel Igloo Ijmuiden", + UCharacter.toTitleCase(Locale.ENGLISH, "ijssel igloo IJMUIDEN", null)); + + assertEquals("Dutch titlecase check in Dutch (Java Locale)", + "IJssel Igloo IJmuiden", + UCharacter.toTitleCase(JAVALOC_DUTCH, "ijssel igloo IJMUIDEN", null)); + iter.setText("ijssel igloo IjMUIdEN iPoD ijenough"); assertEquals("Dutch titlecase check in Dutch with nolowercase option", "IJssel Igloo IJMUIdEN IPoD IJenough", diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterTest.java index 488e50fc1f7..18169b95175 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/lang/UCharacterTest.java @@ -2913,7 +2913,7 @@ public final class UCharacterTest extends TestFmwk // Calls the function "String toTitleCase(ULocale locale, String str, BreakIterator titleIter, int options)" // Tests when "if (locale == null)" is true - UCharacter.toTitleCase(null, "", null, 0); + UCharacter.toTitleCase((ULocale)null, "", null, 0); // TODO: Tests when "if(index==BreakIterator.DONE || index>srcLength)" is true // TODO: Tests when "while((c=iter.nextCaseMapCP())>=0 && UCaseProps.NONE==gCsp.getType(c))" is false diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/text/SpoofCheckerTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/text/SpoofCheckerTest.java index 2454ecf8640..7a8fb0bea5e 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/text/SpoofCheckerTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/text/SpoofCheckerTest.java @@ -16,6 +16,7 @@ import java.util.BitSet; import java.util.Comparator; import java.util.HashSet; import java.util.LinkedHashSet; +import java.util.Locale; import java.util.Random; import java.util.Set; import java.util.regex.Matcher; @@ -218,22 +219,34 @@ public class SpoofCheckerTest extends TestFmwk { */ public void TestAllowedLocales() { SpoofChecker sc = new SpoofChecker.Builder().build(); - Set allowedLocales = new LinkedHashSet(); + Set allowedLocales = null; + Set allowedJavaLocales = null; boolean checkResults; /* Default allowed locales list should be empty */ allowedLocales = sc.getAllowedLocales(); - assertTrue("", allowedLocales.isEmpty()); + assertTrue("Empty allowed locales", allowedLocales.isEmpty()); + + allowedJavaLocales = sc.getAllowedJavaLocales(); + assertTrue("Empty allowed Java locales", allowedJavaLocales.isEmpty()); /* Allow en and ru, which should enable Latin and Cyrillic only to pass */ ULocale enloc = new ULocale("en"); ULocale ruloc = new ULocale("ru_RU"); + allowedLocales = new HashSet(); allowedLocales.add(enloc); allowedLocales.add(ruloc); sc = new SpoofChecker.Builder().setAllowedLocales(allowedLocales).build(); allowedLocales = sc.getAllowedLocales(); - assertTrue("", allowedLocales.contains(enloc)); - assertTrue("", allowedLocales.contains(ruloc)); + assertTrue("en in allowed locales", allowedLocales.contains(enloc)); + assertTrue("ru_RU in allowed locales", allowedLocales.contains(ruloc)); + + Locale frlocJ = new Locale("fr"); + allowedJavaLocales = new HashSet(); + allowedJavaLocales.add(frlocJ); + sc = new SpoofChecker.Builder().setAllowedJavaLocales(allowedJavaLocales).build(); + assertFalse("no en in allowed Java locales", allowedJavaLocales.contains(new Locale("en"))); + assertTrue("fr in allowed Java locales", allowedJavaLocales.contains(frlocJ)); /* * Limit checks to SpoofChecker.CHAR_LIMIT. Some of the test data has whole script confusables also, which we diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java index 6bfd090b00e..45a861039a1 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java @@ -1,6 +1,6 @@ /* ********************************************************************** - * Copyright (c) 2002-2013, International Business Machines + * Copyright (c) 2002-2014, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu @@ -19,6 +19,7 @@ import java.util.Locale; import java.util.Set; import com.ibm.icu.dev.test.TestFmwk; +import com.ibm.icu.dev.test.TestUtil; import com.ibm.icu.impl.CurrencyData; import com.ibm.icu.text.CurrencyDisplayNames; import com.ibm.icu.text.CurrencyMetaInfo; @@ -239,7 +240,7 @@ public class CurrencyTest extends TestFmwk { // with no substitute cdn = CurrencyDisplayNames.getInstance(ULocale.GERMANY, true); assertNotNull("have currency data for Germany", cdn); - + // known currency, behavior unchanged assertEquals("de_USD_name", "US-Dollar", cdn.getName("USD")); assertEquals("de_USD_symbol", "$", cdn.getSymbol("USD")); @@ -261,6 +262,12 @@ public class CurrencyTest extends TestFmwk { ln = " (" + cdn.getULocale().toString() + ")"; } assertNull("no fallback from unknown locale" + ln , cdn); + + // Locale version + cdn = CurrencyDisplayNames.getInstance(Locale.GERMANY, true); + assertNotNull("have currency data for Germany (Java Locale)", cdn); + assertEquals("de_USD_name (Locale)", "US-Dollar", cdn.getName("USD")); + assertNull("de_FOO_name (Locale)", cdn.getName("FOO")); } // Coverage-only test of CurrencyData @@ -565,6 +572,33 @@ public class CurrencyTest extends TestFmwk { actualSet.addAll(Arrays.asList(actual)); } assertEquals(locale + " on " + timeString, expectedSet, actualSet); + + // With Java Locale + // Note: skip this test on Java 6 or older when keywords are available + if (locale.getKeywords() == null || TestUtil.getJavaVersion() >= 7) { + Locale javaloc = locale.toLocale(); + String[] actualWithJavaLocale = Currency.getAvailableCurrencyCodes(javaloc, date); + // should be exactly same with the ULocale version + boolean same = true; + if (actual == null) { + if (actualWithJavaLocale != null) { + same = false; + } + } else { + if (actualWithJavaLocale == null || actual.length != actualWithJavaLocale.length) { + same = false; + } else { + same = true; + for (int i = 0; i < actual.length; i++) { + if (!actual[i].equals(actualWithJavaLocale[i])) { + same = false; + break; + } + } + } + } + assertTrue("getAvailableCurrencyCodes with ULocale vs Locale", same); + } } } diff --git a/icu4j/main/tests/packaging/src/com/ibm/icu/dev/test/TestLocaleNamePackaging.java b/icu4j/main/tests/packaging/src/com/ibm/icu/dev/test/TestLocaleNamePackaging.java index 7e27802a6d9..42ad4d14228 100644 --- a/icu4j/main/tests/packaging/src/com/ibm/icu/dev/test/TestLocaleNamePackaging.java +++ b/icu4j/main/tests/packaging/src/com/ibm/icu/dev/test/TestLocaleNamePackaging.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2009-2012, International Business Machines Corporation and * + * Copyright (C) 2009-2014, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -9,6 +9,8 @@ package com.ibm.icu.dev.test; import static com.ibm.icu.impl.LocaleDisplayNamesImpl.DataTableType.LANG; import static com.ibm.icu.impl.LocaleDisplayNamesImpl.DataTableType.REGION; +import java.util.Locale; + import com.ibm.icu.impl.LocaleDisplayNamesImpl; import com.ibm.icu.text.LocaleDisplayNames; import com.ibm.icu.text.LocaleDisplayNames.DialectHandling; @@ -24,8 +26,8 @@ public class TestLocaleNamePackaging extends TestFmwk { } public boolean validate() { - warnln("language data: " + LocaleDisplayNamesImpl.haveData(LANG)); - warnln(" region data: " + LocaleDisplayNamesImpl.haveData(REGION)); + logln("language data: " + LocaleDisplayNamesImpl.haveData(LANG)); + logln(" region data: " + LocaleDisplayNamesImpl.haveData(REGION)); return true; } @@ -34,6 +36,12 @@ public class TestLocaleNamePackaging extends TestFmwk { new ULocale("und_TH") }; + // Java Locales equivalent to above + private static Locale[] javaLocales = { + new Locale(""), Locale.US, new Locale("es", "ES"), Locale.GERMANY, + new Locale("und", "TH") + }; + public void testRegionDisplayNames() { String[] expectedWithRegionData = { "", @@ -83,6 +91,20 @@ public class TestLocaleNamePackaging extends TestFmwk { } } } + + // Same test with Java Locale + n = 0; + for (Locale displayJavaLocale : javaLocales) { + LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayJavaLocale); + for (Locale targetLocale : javaLocales) { + String result = dn.regionDisplayName(targetLocale.getCountry()); + assertEquals(targetLocale + " in " + displayJavaLocale, expected[n++], result); + if (n == expected.length) { + n = 0; + } + } + } + } public void testLanguageDisplayNames() { @@ -96,12 +118,12 @@ public class TestLocaleNamePackaging extends TestFmwk { "English", "Spanish", "German", - "Unknown or Invalid Language", + "Unknown Language", "", "ingl\u00E9s", "espa\u00F1ol", "alem\u00E1n", - "indeterminada", + "lengua desconocida", "", "Englisch", "Spanisch", @@ -111,7 +133,7 @@ public class TestLocaleNamePackaging extends TestFmwk { "English", "Spanish", "German", - "Unknown or Invalid Language", + "Unknown Language", }; String[] expectedWithoutLanguageData = { "", @@ -134,16 +156,30 @@ public class TestLocaleNamePackaging extends TestFmwk { } } } + + // Same test with Java Locale + n = 0; + for (Locale displayJavaLocale : javaLocales) { + LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayJavaLocale); + for (Locale targetLocale : javaLocales) { + String result = dn.languageDisplayName(targetLocale.getLanguage()); + assertEquals(targetLocale + " in " + displayJavaLocale, expected[n++], result); + if (n == expected.length) { + n = 0; + } + } + } + } // test a 'root' locale, with keywords public void testLocaleDisplayNameWithKeywords() { String[] expectedWithLanguageData = { "root (collation=phonebook)", - "Root (collation=Phonebook Sort Order)", - "ra\u00EDz (intercalaci\u00F3n=orden de list\u00EDn telef\u00F3nico)", - "Root (Sortierung=Telefonbuch-Sortierregeln)", - "Root (collation=Phonebook Sort Order)", + "Root (Phonebook Sort Order)", + "ra\u00EDz (orden de list\u00EDn telef\u00F3nico)", + "Root (Telefonbuch-Sortierung)", + "Root (Phonebook Sort Order)", }; String[] expectedWithoutLanguageData = { "root (collation=phonebook)",