From f4376e1e6f53aeb53cd082d31c92de653e8cb3af Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Wed, 6 Apr 2016 23:40:59 +0000 Subject: [PATCH] ICU-12446 UResource sink size: move into new enter(size) method X-SVN-Rev: 38604 --- .../src/com/ibm/icu/impl/DayPeriodRules.java | 10 ++-- .../ibm/icu/impl/ICUResourceBundleReader.java | 55 +++---------------- .../com/ibm/icu/impl/TimeZoneNamesImpl.java | 4 +- .../core/src/com/ibm/icu/impl/UResource.java | 34 +++++++++--- .../com/ibm/icu/text/DateIntervalInfo.java | 4 +- .../icu/text/DateTimePatternGenerator.java | 10 +++- .../src/com/ibm/icu/text/MeasureFormat.java | 6 +- .../icu/text/RelativeDateTimeFormatter.java | 6 +- 8 files changed, 54 insertions(+), 75 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/DayPeriodRules.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/DayPeriodRules.java index 0209d3293db..6a3cad4d515 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/DayPeriodRules.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/DayPeriodRules.java @@ -76,7 +76,7 @@ public final class DayPeriodRules { // Entry point. @Override - public UResource.TableSink getOrCreateTableSink(UResource.Key key, int initialSize) { + public UResource.TableSink getOrCreateTableSink(UResource.Key key) { if (key.contentEquals("locales")) { return localesSink; } else if (key.contentEquals("rules")) { @@ -99,7 +99,7 @@ public final class DayPeriodRules { // Rules. private class RulesSink extends UResource.TableSink { @Override - public UResource.TableSink getOrCreateTableSink(UResource.Key key, int initialSize) { + public UResource.TableSink getOrCreateTableSink(UResource.Key key) { ruleSetNum = parseSetNum(key.toString()); data.rules[ruleSetNum] = new DayPeriodRules(); return ruleSetSink; @@ -110,7 +110,7 @@ public final class DayPeriodRules { // Rules -> "set10", e.g. private class RuleSetSink extends UResource.TableSink { @Override - public UResource.TableSink getOrCreateTableSink(UResource.Key key, int initialSize) { + public UResource.TableSink getOrCreateTableSink(UResource.Key key) { period = DayPeriod.fromStringOrNull(key); if (period == null) { throw new ICUException("Unknown day period in data."); } return periodSink; @@ -138,7 +138,7 @@ public final class DayPeriodRules { } @Override - public UResource.ArraySink getOrCreateArraySink(UResource.Key key, int initialSize) { + public UResource.ArraySink getOrCreateArraySink(UResource.Key key) { cutoffType = CutoffType.fromStringOrNull(key); return cutoffSink; } @@ -241,7 +241,7 @@ public final class DayPeriodRules { } @Override - public UResource.TableSink getOrCreateTableSink(UResource.Key key, int initialSize) { + public UResource.TableSink getOrCreateTableSink(UResource.Key key) { int setNum = parseSetNum(key.toString()); if (setNum > data.maxRuleSetNum) { data.maxRuleSetNum = setNum; diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundleReader.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundleReader.java index 0083150bd1f..7427cb57664 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundleReader.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundleReader.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2004-2015, International Business Machines Corporation and + * Copyright (C) 2004-2016, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* */ @@ -723,22 +723,6 @@ public final class ICUResourceBundleReader { } } - private int getArrayLength(int res) { - int offset = RES_GET_OFFSET(res); - if(offset == 0) { - return 0; - } - int type = RES_GET_TYPE(res); - if(type == UResourceBundle.ARRAY) { - offset = getResourceByteOffset(offset); - return getInt(offset); - } else if(type == ICUResourceBundle.ARRAY16) { - return b16BitUnits.charAt(offset); - } else { - return 0; - } - } - Array getArray(int res) { int type=RES_GET_TYPE(res); if(!URES_IS_ARRAY(type)) { @@ -757,25 +741,6 @@ public final class ICUResourceBundleReader { return (Array)resourceCache.putIfAbsent(res, array, 0); } - private int getTableLength(int res) { - int offset = RES_GET_OFFSET(res); - if(offset == 0) { - return 0; - } - int type = RES_GET_TYPE(res); - if(type == UResourceBundle.TABLE) { - offset = getResourceByteOffset(offset); - return bytes.getChar(offset); - } else if(type == ICUResourceBundle.TABLE16) { - return b16BitUnits.charAt(offset); - } else if(type == ICUResourceBundle.TABLE32) { - offset = getResourceByteOffset(offset); - return getInt(offset); - } else { - return 0; - } - } - Table getTable(int res) { int type = RES_GET_TYPE(res); if(!URES_IS_TABLE(type)) { @@ -935,23 +900,20 @@ public final class ICUResourceBundleReader { Array() {} void getAllItems(ICUResourceBundleReader reader, UResource.Key key, ReaderValue value, ArraySink sink) { + sink.enter(size); for (int i = 0; i < size; ++i) { int res = getContainerResource(reader, i); int type = RES_GET_TYPE(res); if (URES_IS_ARRAY(type)) { - int numItems = reader.getArrayLength(res); - ArraySink subSink = sink.getOrCreateArraySink(i, numItems); + ArraySink subSink = sink.getOrCreateArraySink(i); if (subSink != null) { Array array = reader.getArray(res); - assert(array.size == numItems); array.getAllItems(reader, key, value, subSink); } } else if (URES_IS_TABLE(type)) { - int numItems = reader.getTableLength(res); - TableSink subSink = sink.getOrCreateTableSink(i, numItems); + TableSink subSink = sink.getOrCreateTableSink(i); if (subSink != null) { Table table = reader.getTable(res); - assert(table.size == numItems); table.getAllItems(reader, key, value, subSink); } /* TODO: settle on how to deal with aliases, port to C++ @@ -1031,6 +993,7 @@ public final class ICUResourceBundleReader { } void getAllItems(ICUResourceBundleReader reader, UResource.Key key, ReaderValue value, TableSink sink) { + sink.enter(size); for (int i = 0; i < size; ++i) { if (keyOffsets != null) { reader.setKeyFromKey16(keyOffsets[i], key); @@ -1040,19 +1003,15 @@ public final class ICUResourceBundleReader { int res = getContainerResource(reader, i); int type = RES_GET_TYPE(res); if (URES_IS_ARRAY(type)) { - int numItems = reader.getArrayLength(res); - ArraySink subSink = sink.getOrCreateArraySink(key, numItems); + ArraySink subSink = sink.getOrCreateArraySink(key); if (subSink != null) { Array array = reader.getArray(res); - assert(array.size == numItems); array.getAllItems(reader, key, value, subSink); } } else if (URES_IS_TABLE(type)) { - int numItems = reader.getTableLength(res); - TableSink subSink = sink.getOrCreateTableSink(key, numItems); + TableSink subSink = sink.getOrCreateTableSink(key); if (subSink != null) { Table table = reader.getTable(res); - assert(table.size == numItems); table.getAllItems(reader, key, value, subSink); } /* TODO: settle on how to deal with aliases, port to C++ diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/TimeZoneNamesImpl.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/TimeZoneNamesImpl.java index 1e8d1cf980a..78c8583cd6b 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/TimeZoneNamesImpl.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/TimeZoneNamesImpl.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2011-2015, International Business Machines Corporation and + * Copyright (C) 2011-2016, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* */ @@ -325,7 +325,7 @@ public class TimeZoneNamesImpl extends TimeZoneNames { } @Override - public TableSink getOrCreateTableSink(UResource.Key key, int initialSize) { + public TableSink getOrCreateTableSink(UResource.Key key) { ZNamesLoader loader = keyToLoader.get(key); if (loader != null) { if (loader == ZNamesLoader.DUMMY_LOADER) { diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/UResource.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/UResource.java index c8f1d7935b7..30d1185a489 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/UResource.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/UResource.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2015, International Business Machines Corporation and + * Copyright (C) 2015-2016, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* */ @@ -320,6 +320,16 @@ public final class UResource { * never put() as {@link Value} items. */ public static class ArraySink { + /** + * "Enters" the array. + * Called just before enumerating the array's resource items. + * The size can be used to allocate storage for the items. + * It may differ between child and parent bundles. + * + * @param size number of table items + */ + public void enter(int size) {} + /** * Adds a value from a resource array. * @@ -335,10 +345,9 @@ public final class UResource { * The default implementation always returns null. * * @param index of the resource array item - * @param size number of array items * @return nested-array sink, or null */ - public ArraySink getOrCreateArraySink(int index, int size) { + public ArraySink getOrCreateArraySink(int index) { return null; } @@ -349,10 +358,9 @@ public final class UResource { * The default implementation always returns null. * * @param index of the resource array item - * @param initialSize size hint for creating the sink if necessary * @return nested-table sink, or null */ - public TableSink getOrCreateTableSink(int index, int initialSize) { + public TableSink getOrCreateTableSink(int index) { return null; } @@ -372,6 +380,16 @@ public final class UResource { * never put() as {@link Value} items. */ public static class TableSink { + /** + * "Enters" the table. + * Called just before enumerating the table's resource items. + * The size can be used to allocate storage for the items. + * It usually differs between child and parent bundles. + * + * @param size number of table items + */ + public void enter(int size) {} + /** * Adds a key-value pair from a resource table. * @@ -398,10 +416,9 @@ public final class UResource { * The default implementation always returns null. * * @param key resource key string - * @param size number of array items * @return nested-array sink, or null */ - public ArraySink getOrCreateArraySink(Key key, int size) { + public ArraySink getOrCreateArraySink(Key key) { return null; } @@ -412,10 +429,9 @@ public final class UResource { * The default implementation always returns null. * * @param key resource key string - * @param initialSize size hint for creating the sink if necessary * @return nested-table sink, or null */ - public TableSink getOrCreateTableSink(Key key, int initialSize) { + public TableSink getOrCreateTableSink(Key key) { return null; } 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 5105bec58fb..6e5fc0e6e4f 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 @@ -435,7 +435,7 @@ public class DateIntervalInfo implements Cloneable, Freezable, */ class SkeletonSink extends UResource.TableSink { @Override - public TableSink getOrCreateTableSink(Key key, int initialSize) { + public TableSink getOrCreateTableSink(Key key) { currentSkeleton = key.toString(); return patternSink; } @@ -534,7 +534,7 @@ public class DateIntervalInfo implements Cloneable, Freezable, } @Override - public TableSink getOrCreateTableSink(Key key, int initialSize) { + public TableSink getOrCreateTableSink(Key key) { // Check if it's the intervalFormats table if (key.contentEquals(INTERVAL_FORMATS_KEY)) { return skeletonSink; 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 f504a09fd11..a0fd49fffd1 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 @@ -447,7 +447,7 @@ public class DateTimePatternGenerator implements Freezable