From: Shane Carr Date: Tue, 31 May 2016 16:34:58 +0000 (+0000) Subject: ICU-12534 Optimizing currency spacing data loading in ICUCurrencyDisplayInfoProvider... X-Git-Tag: milestone-59-0-1~417 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d03ad991d96c899a9a48a67b0313064d7adfa182;p=icu ICU-12534 Optimizing currency spacing data loading in ICUCurrencyDisplayInfoProvider using data sink. X-SVN-Rev: 38772 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/CurrencyData.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/CurrencyData.java index 343a45c4f20..e3d8bc82913 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/CurrencyData.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/CurrencyData.java @@ -1,7 +1,7 @@ /* ******************************************************************************* - * Copyright (C) 2009-2012, International Business Machines Corporation and * - * others. All Rights Reserved. * + * Copyright (C) 2009-2016, International Business Machines Corporation and + * others. All Rights Reserved. ******************************************************************************* */ package com.ibm.icu.impl; @@ -40,24 +40,40 @@ public class CurrencyData { } public static final class CurrencySpacingInfo { - public final String beforeCurrencyMatch; - public final String beforeContextMatch; - public final String beforeInsert; - public final String afterCurrencyMatch; - public final String afterContextMatch; - public final String afterInsert; - - public CurrencySpacingInfo( - String beforeCurrencyMatch, String beforeContextMatch, String beforeInsert, - String afterCurrencyMatch, String afterContextMatch, String afterInsert) { - this.beforeCurrencyMatch = beforeCurrencyMatch; - this.beforeContextMatch = beforeContextMatch; - this.beforeInsert = beforeInsert; - this.afterCurrencyMatch = afterCurrencyMatch; - this.afterContextMatch = afterContextMatch; - this.afterInsert = afterInsert; + private final String[][] symbols = new String[SpacingType.COUNT.ordinal()][SpacingPattern.COUNT.ordinal()]; + + public static enum SpacingType { BEFORE, AFTER, COUNT }; + public static enum SpacingPattern { CURRENCY_MATCH, SURROUNDING_MATCH, INSERT_BETWEEN, COUNT }; + + public CurrencySpacingInfo() {} + + public CurrencySpacingInfo(String... strings) { + assert strings.length == 6; + + int k = 0; + for (int i=0; i getUnitPatterns() { Map result = new HashMap(); @@ -183,24 +183,72 @@ public class ICUCurrencyDisplayInfoProvider implements CurrencyDisplayInfoProvid @Override public CurrencySpacingInfo getSpacingInfo() { - ICUResourceBundle srb = rb.findWithFallback("currencySpacing"); - if (srb != null) { - ICUResourceBundle brb = srb.findWithFallback("beforeCurrency"); - ICUResourceBundle arb = srb.findWithFallback("afterCurrency"); - if (arb != null && brb != null) { - String beforeCurrencyMatch = brb.findStringWithFallback("currencyMatch"); - String beforeContextMatch = brb.findStringWithFallback("surroundingMatch"); - String beforeInsert = brb.findStringWithFallback("insertBetween"); - String afterCurrencyMatch = arb.findStringWithFallback("currencyMatch"); - String afterContextMatch = arb.findStringWithFallback("surroundingMatch"); - String afterInsert = arb.findStringWithFallback("insertBetween"); - - return new CurrencySpacingInfo( - beforeCurrencyMatch, beforeContextMatch, beforeInsert, - afterCurrencyMatch, afterContextMatch, afterInsert); + SpacingInfoSink sink = new SpacingInfoSink(); + rb.getAllItemsWithFallback("currencySpacing", sink); + return sink.getSpacingInfo(fallback); + } + + private final class SpacingInfoSink extends UResource.Sink { + CurrencySpacingInfo spacingInfo = new CurrencySpacingInfo(); + boolean hasBeforeCurrency = false; + boolean hasAfterCurrency = false; + + /* + * currencySpacing{ + * afterCurrency{ + * currencyMatch{"[:^S:]"} + * insertBetween{" "} + * surroundingMatch{"[:digit:]"} + * } + * beforeCurrency{ + * currencyMatch{"[:^S:]"} + * insertBetween{" "} + * surroundingMatch{"[:digit:]"} + * } + * } + */ + @Override + public void put(UResource.Key key, UResource.Value value, boolean noFallback) { + UResource.Table spacingTypesTable = value.getTable(); + for (int i = 0; spacingTypesTable.getKeyAndValue(i, key, value); ++i) { + CurrencySpacingInfo.SpacingType type; + if (key.contentEquals("beforeCurrency")) { + type = CurrencySpacingInfo.SpacingType.BEFORE; + hasBeforeCurrency = true; + } else if (key.contentEquals("afterCurrency")) { + type = CurrencySpacingInfo.SpacingType.AFTER; + hasAfterCurrency = true; + } else { + continue; + } + + UResource.Table patternsTable = value.getTable(); + for (int j = 0; patternsTable.getKeyAndValue(j, key, value); ++j) { + CurrencySpacingInfo.SpacingPattern pattern; + if (key.contentEquals("currencyMatch")) { + pattern = CurrencySpacingInfo.SpacingPattern.CURRENCY_MATCH; + } else if (key.contentEquals("surroundingMatch")) { + pattern = CurrencySpacingInfo.SpacingPattern.SURROUNDING_MATCH; + } else if (key.contentEquals("insertBetween")) { + pattern = CurrencySpacingInfo.SpacingPattern.INSERT_BETWEEN; + } else { + continue; + } + + spacingInfo.setSymbolIfNull(type, pattern, value.getString()); + } + } + } + + CurrencySpacingInfo getSpacingInfo(boolean fallback) { + if (hasBeforeCurrency && hasAfterCurrency) { + return spacingInfo; + } else if (fallback) { + return CurrencySpacingInfo.DEFAULT; + } else { + return null; } } - return fallback ? CurrencySpacingInfo.DEFAULT : null; } private Map _createSymbolMap() {