ICU-12616 Update DateIntervalInfo to new version of ResourceSink.

X-SVN-Rev: 38945
This commit is contained in:
Felipe Balbontín 2016-07-06 15:53:03 +00:00
parent f0765287d7
commit e486897ced

View File

@ -413,10 +413,8 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
/**
* Sink for enumerating all of the date interval skeletons.
* Contains inner sink classes, each one corresponding to a type of resource table.
* The outer class finds the dateInterval table or an alias.
*/
private static final class DateIntervalSink extends UResource.TableSink {
private static final class DateIntervalSink extends UResource.Sink {
/**
* Accepted pattern letters:
@ -431,26 +429,57 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
*/
private static final String ACCEPTED_PATTERN_LETTERS = "yMdahHms";
// Output data
DateIntervalInfo dateIntervalInfo;
/**
* Sink to handle each skeleton table.
*/
class SkeletonSink extends UResource.TableSink {
@Override
public TableSink getOrCreateTableSink(Key key) {
currentSkeleton = key.toString();
return patternSink;
}
}
SkeletonSink skeletonSink = new SkeletonSink();
// Alias handling
String nextCalendarType;
/**
* Sink to store the date interval pattern for each skeleton pattern character.
*/
class PatternSink extends UResource.TableSink {
// Constructor
public DateIntervalSink(DateIntervalInfo dateIntervalInfo) {
this.dateIntervalInfo = dateIntervalInfo;
}
@Override
public void put(Key key, Value value) {
public void put(Key key, Value value, boolean noFallback) {
// Iterate over all the calendar entries and only pick the 'intervalFormats' table.
UResource.Table dateIntervalData = value.getTable();
for (int i = 0; dateIntervalData.getKeyAndValue(i, key, value); i++) {
if (!key.contentEquals(INTERVAL_FORMATS_KEY)) {
continue;
}
// Handle aliases and tables. Ignore the rest.
if (value.getType() == ICUResourceBundle.ALIAS) {
// Get the calendar type from the alias path.
nextCalendarType = getCalendarTypeFromPath(value.getAliasString());
break;
} else if (value.getType() == ICUResourceBundle.TABLE) {
// Iterate over all the skeletons in the 'intervalFormat' table.
UResource.Table skeletonData = value.getTable();
for (int j = 0; skeletonData.getKeyAndValue(j, key, value); j++) {
if (value.getType() == ICUResourceBundle.TABLE) {
// Process the skeleton
processSkeletonTable(key, value);
}
}
break;
}
}
}
/**
* Processes the patterns for a skeleton table.
* @param key
* @param value
*/
public void processSkeletonTable(Key key, Value value) {
// Iterate over all the patterns in the current skeleton table
String currentSkeleton = key.toString();
UResource.Table patternData = value.getTable();
for (int k = 0; patternData.getKeyAndValue(k, key, value); k++) {
if (value.getType() == ICUResourceBundle.STRING) {
// Process the key
CharSequence patternLetter = validateAndProcessPatternLetter(key);
@ -460,9 +489,41 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
String lrgDiffCalUnit = patternLetter.toString();
// Set the interval pattern
setIntervalPatternIfAbsent(lrgDiffCalUnit, value);
setIntervalPatternIfAbsent(currentSkeleton, lrgDiffCalUnit, value);
}
}
}
}
/**
* Returns and resets the next calendar type.
* @return Next calendar type
*/
public String getAndResetNextCalendarType() {
String tmpCalendarType = nextCalendarType;
nextCalendarType = null;
return tmpCalendarType;
}
// Alias' path prefix and suffix.
private static final String DATE_INTERVAL_PATH_PREFIX =
"/LOCALE/" + CALENDAR_KEY + "/";
private static final String DATE_INTERVAL_PATH_SUFFIX =
"/" + INTERVAL_FORMATS_KEY;
/**
* Extracts the calendar type from the path
* @param path
* @return Calendar Type
*/
private String getCalendarTypeFromPath(String path) {
if (path.startsWith(DATE_INTERVAL_PATH_PREFIX) &&
path.endsWith(DATE_INTERVAL_PATH_SUFFIX)) {
return path.substring(DATE_INTERVAL_PATH_PREFIX.length(),
path.length() - DATE_INTERVAL_PATH_SUFFIX.length());
}
throw new ICUException("Malformed 'intervalFormat' alias path: " + path);
}
/**
* Processes the pattern letter
@ -493,7 +554,7 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
* @param lrgDiffCalUnit
* @param intervalPattern
*/
private void setIntervalPatternIfAbsent(String lrgDiffCalUnit, Value intervalPattern) {
private void setIntervalPatternIfAbsent(String currentSkeleton, String lrgDiffCalUnit, Value intervalPattern) {
// Check if the pattern has already been stored on the data structure.
Map<String, PatternInfo> patternsOfOneSkeleton =
dateIntervalInfo.fIntervalPatterns.get(currentSkeleton);
@ -503,76 +564,6 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
intervalPattern.toString());
}
}
}
PatternSink patternSink = new PatternSink();
// Output data
DateIntervalInfo dateIntervalInfo;
// Alias handling
String nextCalendarType;
// Current skeleton table being enumerated
String currentSkeleton;
// Constructor
public DateIntervalSink(DateIntervalInfo dateIntervalInfo) {
this.dateIntervalInfo = dateIntervalInfo;
}
@Override
public void put(Key key, Value value) {
// Check if it's an alias of intervalFormats
if (value.getType() != ICUResourceBundle.ALIAS
|| !key.contentEquals(INTERVAL_FORMATS_KEY)) {
return;
}
// Get the calendar type from the alias path.
nextCalendarType = getCalendarTypeFromPath(value.getAliasString());
}
@Override
public TableSink getOrCreateTableSink(Key key) {
// Check if it's the intervalFormats table
if (key.contentEquals(INTERVAL_FORMATS_KEY)) {
return skeletonSink;
}
return null;
}
/**
* Returns and resets the next calendar type.
* @return Next calendar type
*/
public String getAndResetNextCalendarType() {
String tmpCalendarType = nextCalendarType;
nextCalendarType = null;
return tmpCalendarType;
}
// Alias' path prefix and sufix.
private static final String DATE_INTERVAL_PATH_PREFIX =
"/LOCALE/" + CALENDAR_KEY + "/";
private static final String DATE_INTERVAL_PATH_SUFIX =
"/" + INTERVAL_FORMATS_KEY;
/**
* Extracts the calendar type from the path
* @param path
* @return Calendar Type
*/
private String getCalendarTypeFromPath(String path) {
if (path.startsWith(DATE_INTERVAL_PATH_PREFIX) &&
path.endsWith(DATE_INTERVAL_PATH_SUFIX)) {
return path.substring(DATE_INTERVAL_PATH_PREFIX.length(),
path.length() - DATE_INTERVAL_PATH_SUFIX.length());
}
throw new ICUException("Malformed 'intervalFormat' alias path: " + path);
}
}
@ -623,7 +614,7 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
// Get all resources for this calendar type
String pathToIntervalFormats = CALENDAR_KEY + "/" + calendarTypeToUse;
resource.getAllTableItemsWithFallback(pathToIntervalFormats, sink);
resource.getAllItemsWithFallback(pathToIntervalFormats, sink);
// Get next calendar type to load if there was an alias pointing at it
calendarTypeToUse = sink.getAndResetNextCalendarType();