diff options
author | Shane Carr <sffc@google.com> | 2017-05-12 21:50:31 +0000 |
---|---|---|
committer | Fredrik Roubert <roubert@google.com> | 2017-07-06 14:36:53 +0200 |
commit | 6ecdcbc7386b1554409c5957c5252983e0e61bbd (patch) | |
tree | 9d1eabdb9152c94e3249c86770bd20eee2c82db5 /icu4j/main/classes | |
parent | 55082b7c43083c4c0234fff07f36364f935895d8 (diff) | |
download | icu-6ecdcbc7386b1554409c5957c5252983e0e61bbd.tar.gz |
Cherry-pick: ticket:13073 Adding API for setting custom compact data, for CLDR Survey Tool.
http://bugs.icu-project.org/trac/changeset/40117
Change-Id: I54be3079eea1fb9f3972392742c0b835a4cf5aac
Diffstat (limited to 'icu4j/main/classes')
-rw-r--r-- | icu4j/main/classes/core/src/com/ibm/icu/impl/number/Properties.java | 21 | ||||
-rw-r--r-- | icu4j/main/classes/core/src/com/ibm/icu/impl/number/formatters/CompactDecimalFormat.java | 70 |
2 files changed, 88 insertions, 3 deletions
diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/number/Properties.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/number/Properties.java index d7e0c04c8..948fc8eb5 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/number/Properties.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/number/Properties.java @@ -12,6 +12,7 @@ import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.ArrayList; +import java.util.Map; import com.ibm.icu.impl.number.Parse.GroupingMode; import com.ibm.icu.impl.number.Parse.ParseMode; @@ -73,6 +74,7 @@ public class Properties /| or #equals(), but it will NOT catch if you forget to add it to #hashCode(). |/ /+--------------------------------------------------------------------------------------------*/ + private transient Map<String, Map<String, String>> compactCustomData; private transient CompactStyle compactStyle; private transient Currency currency; private transient CurrencyPluralInfo currencyPluralInfo; @@ -132,6 +134,7 @@ public class Properties } private Properties _clear() { + compactCustomData = DEFAULT_COMPACT_CUSTOM_DATA; compactStyle = DEFAULT_COMPACT_STYLE; currency = DEFAULT_CURRENCY; currencyPluralInfo = DEFAULT_CURRENCY_PLURAL_INFO; @@ -180,6 +183,7 @@ public class Properties } private Properties _copyFrom(Properties other) { + compactCustomData = other.compactCustomData; compactStyle = other.compactStyle; currency = other.currency; currencyPluralInfo = other.currencyPluralInfo; @@ -229,6 +233,7 @@ public class Properties private boolean _equals(Properties other) { boolean eq = true; + eq = eq && _equalsHelper(compactCustomData, other.compactCustomData); eq = eq && _equalsHelper(compactStyle, other.compactStyle); eq = eq && _equalsHelper(currency, other.currency); eq = eq && _equalsHelper(currencyPluralInfo, other.currencyPluralInfo); @@ -292,6 +297,7 @@ public class Properties private int _hashCode() { int hashCode = 0; + hashCode ^= _hashCodeHelper(compactCustomData); hashCode ^= _hashCodeHelper(compactStyle); hashCode ^= _hashCodeHelper(currency); hashCode ^= _hashCodeHelper(currencyPluralInfo); @@ -386,6 +392,13 @@ public class Properties return _equals((Properties) other); } + /// BEGIN GETTERS/SETTERS /// + + @Override + public Map<String, Map<String, String>> getCompactCustomData() { + return compactCustomData; + } + @Override public CompactStyle getCompactStyle() { return compactStyle; @@ -396,8 +409,6 @@ public class Properties return currency; } - /// BEGIN GETTERS/SETTERS /// - @Override @Deprecated public CurrencyPluralInfo getCurrencyPluralInfo() { @@ -661,6 +672,12 @@ public class Properties } @Override + public Properties setCompactCustomData(Map<String, Map<String, String>> compactCustomData) { + this.compactCustomData = compactCustomData; + return this; + } + + @Override public Properties setCompactStyle(CompactStyle compactStyle) { this.compactStyle = compactStyle; return this; diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/number/formatters/CompactDecimalFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/number/formatters/CompactDecimalFormat.java index ca88b970b..84899c50f 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/number/formatters/CompactDecimalFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/number/formatters/CompactDecimalFormat.java @@ -49,6 +49,39 @@ public class CompactDecimalFormat extends Format.BeforeFormat { * @return The property bag, for chaining. */ public IProperties setCompactStyle(CompactStyle compactStyle); + + static Map<String, Map<String, String>> DEFAULT_COMPACT_CUSTOM_DATA = null; + + /** @see #setCompactCustomData */ + public Map<String, Map<String, String>> getCompactCustomData(); + + /** + * Specifies custom data to be used instead of CLDR data when constructing a + * CompactDecimalFormat. The argument should be a map with the following structure: + * + * <pre> + * { + * "1000": { + * "one": "0 thousand", + * "other": "0 thousand" + * }, + * "10000": { + * "one": "00 thousand", + * "other": "00 thousand" + * }, + * // ... + * } + * </pre> + * + * This API endpoint is used by the CLDR Survey Tool. + * + * @param compactCustomData A map with the above structure. + * @return The property bag, for chaining. + * @internal + * @deprecated This API is ICU internal only. + */ + @Deprecated + public IProperties setCompactCustomData(Map<String, Map<String, String>> compactCustomData); } public static boolean useCompactDecimalFormat(IProperties properties) { @@ -179,7 +212,12 @@ public class CompactDecimalFormat extends Format.BeforeFormat { private CompactDecimalFormat(DecimalFormatSymbols symbols, IProperties properties) { CompactDecimalFingerprint fingerprint = new CompactDecimalFingerprint(symbols, properties); this.rounder = getRounder(properties); - this.data = getData(symbols, fingerprint); + // Short-circuit and use custom data if provided + if (properties.getCompactCustomData() != null) { + this.data = createDataFromCustom(symbols, fingerprint, properties.getCompactCustomData()); + } else { + this.data = getData(symbols, fingerprint); + } this.defaultMod = getDefaultMod(symbols, fingerprint); this.style = properties.getCompactStyle(); // for exporting only } @@ -490,4 +528,34 @@ public class CompactDecimalFormat extends Format.BeforeFormat { } } } + + /** + * Uses data from the custom powersToPluralsToPatterns map instead of an ICUResourceBundle to + * populate an instance of CompactDecimalData. + */ + static CompactDecimalData createDataFromCustom( + DecimalFormatSymbols symbols, + CompactDecimalFingerprint fingerprint, + Map<String, Map<String, String>> powersToPluralsToPatterns) { + CompactDecimalData data = new CompactDecimalData(); + PNAffixGenerator pnag = PNAffixGenerator.getThreadLocalInstance(); + for (Map.Entry<String, Map<String, String>> magnitudeEntry : + powersToPluralsToPatterns.entrySet()) { + byte magnitude = (byte) (magnitudeEntry.getKey().length() - 1); + for (Map.Entry<String, String> pluralEntry : magnitudeEntry.getValue().entrySet()) { + StandardPlural plural = StandardPlural.fromString(pluralEntry.getKey().toString()); + String patternString = pluralEntry.getValue().toString(); + Properties properties = PatternString.parseToProperties(patternString); + byte _multiplier = (byte) -(magnitude - properties.getMinimumIntegerDigits() + 1); + if (_multiplier != data.setOrGetMultiplier(magnitude, _multiplier)) { + throw new IllegalArgumentException( + "Different number of zeros for same power of ten in custom compact decimal format data"); + } + PNAffixGenerator.Result result = + pnag.getModifiers(symbols, fingerprint.currencySymbol, properties); + data.setModifiers(result.positive, result.negative, magnitude, plural); + } + } + return data; + } } |