ICU-12616 Update DateIntervalInfo to new version of ResourceSink.
X-SVN-Rev: 38945
This commit is contained in:
parent
f0765287d7
commit
e486897ced
@ -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,117 +429,70 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
|
||||
*/
|
||||
private static final String ACCEPTED_PATTERN_LETTERS = "yMdahHms";
|
||||
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* Sink to store the date interval pattern for each skeleton pattern character.
|
||||
*/
|
||||
class PatternSink extends UResource.TableSink {
|
||||
|
||||
@Override
|
||||
public void put(Key key, Value value) {
|
||||
// Process the key
|
||||
CharSequence patternLetter = validateAndProcessPatternLetter(key);
|
||||
|
||||
// If the calendar field has a valid value
|
||||
if (patternLetter != null) {
|
||||
// Get the largest different calendar unit
|
||||
String lrgDiffCalUnit = patternLetter.toString();
|
||||
|
||||
// Set the interval pattern
|
||||
setIntervalPatternIfAbsent(lrgDiffCalUnit, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the pattern letter
|
||||
* @param patternLetter
|
||||
* @return Pattern letter
|
||||
*/
|
||||
private CharSequence validateAndProcessPatternLetter(CharSequence patternLetter) {
|
||||
// Check that patternLetter is just one letter
|
||||
if (patternLetter.length() != 1) { return null; }
|
||||
|
||||
// Check that the pattern letter is accepted
|
||||
char letter = patternLetter.charAt(0);
|
||||
if (ACCEPTED_PATTERN_LETTERS.indexOf(letter) < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Replace 'h' for 'H'
|
||||
if (letter == CALENDAR_FIELD_TO_PATTERN_LETTER[Calendar.HOUR_OF_DAY].charAt(0)) {
|
||||
patternLetter = CALENDAR_FIELD_TO_PATTERN_LETTER[Calendar.HOUR];
|
||||
}
|
||||
|
||||
return patternLetter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the interval pattern for the current skeleton in the internal data structure
|
||||
* if it's not present.
|
||||
* @param lrgDiffCalUnit
|
||||
* @param intervalPattern
|
||||
*/
|
||||
private void setIntervalPatternIfAbsent(String lrgDiffCalUnit, Value intervalPattern) {
|
||||
// Check if the pattern has already been stored on the data structure.
|
||||
Map<String, PatternInfo> patternsOfOneSkeleton =
|
||||
dateIntervalInfo.fIntervalPatterns.get(currentSkeleton);
|
||||
if (patternsOfOneSkeleton == null || !patternsOfOneSkeleton.containsKey(lrgDiffCalUnit)) {
|
||||
// Store the pattern
|
||||
dateIntervalInfo.setIntervalPatternInternally(currentSkeleton, lrgDiffCalUnit,
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
// Get the calendar type from the alias path.
|
||||
nextCalendarType = getCalendarTypeFromPath(value.getAliasString());
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableSink getOrCreateTableSink(Key key) {
|
||||
// Check if it's the intervalFormats table
|
||||
if (key.contentEquals(INTERVAL_FORMATS_KEY)) {
|
||||
return skeletonSink;
|
||||
/**
|
||||
* 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);
|
||||
|
||||
// If the calendar field has a valid value
|
||||
if (patternLetter != null) {
|
||||
// Get the largest different calendar unit
|
||||
String lrgDiffCalUnit = patternLetter.toString();
|
||||
|
||||
// Set the interval pattern
|
||||
setIntervalPatternIfAbsent(currentSkeleton, lrgDiffCalUnit, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -554,10 +505,10 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
|
||||
return tmpCalendarType;
|
||||
}
|
||||
|
||||
// Alias' path prefix and sufix.
|
||||
// Alias' path prefix and suffix.
|
||||
private static final String DATE_INTERVAL_PATH_PREFIX =
|
||||
"/LOCALE/" + CALENDAR_KEY + "/";
|
||||
private static final String DATE_INTERVAL_PATH_SUFIX =
|
||||
private static final String DATE_INTERVAL_PATH_SUFFIX =
|
||||
"/" + INTERVAL_FORMATS_KEY;
|
||||
|
||||
/**
|
||||
@ -567,12 +518,52 @@ public class DateIntervalInfo implements Cloneable, Freezable<DateIntervalInfo>,
|
||||
*/
|
||||
private String getCalendarTypeFromPath(String path) {
|
||||
if (path.startsWith(DATE_INTERVAL_PATH_PREFIX) &&
|
||||
path.endsWith(DATE_INTERVAL_PATH_SUFIX)) {
|
||||
path.endsWith(DATE_INTERVAL_PATH_SUFFIX)) {
|
||||
return path.substring(DATE_INTERVAL_PATH_PREFIX.length(),
|
||||
path.length() - DATE_INTERVAL_PATH_SUFIX.length());
|
||||
path.length() - DATE_INTERVAL_PATH_SUFFIX.length());
|
||||
}
|
||||
throw new ICUException("Malformed 'intervalFormat' alias path: " + path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the pattern letter
|
||||
* @param patternLetter
|
||||
* @return Pattern letter
|
||||
*/
|
||||
private CharSequence validateAndProcessPatternLetter(CharSequence patternLetter) {
|
||||
// Check that patternLetter is just one letter
|
||||
if (patternLetter.length() != 1) { return null; }
|
||||
|
||||
// Check that the pattern letter is accepted
|
||||
char letter = patternLetter.charAt(0);
|
||||
if (ACCEPTED_PATTERN_LETTERS.indexOf(letter) < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Replace 'h' for 'H'
|
||||
if (letter == CALENDAR_FIELD_TO_PATTERN_LETTER[Calendar.HOUR_OF_DAY].charAt(0)) {
|
||||
patternLetter = CALENDAR_FIELD_TO_PATTERN_LETTER[Calendar.HOUR];
|
||||
}
|
||||
|
||||
return patternLetter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the interval pattern for the current skeleton in the internal data structure
|
||||
* if it's not present.
|
||||
* @param lrgDiffCalUnit
|
||||
* @param 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);
|
||||
if (patternsOfOneSkeleton == null || !patternsOfOneSkeleton.containsKey(lrgDiffCalUnit)) {
|
||||
// Store the pattern
|
||||
dateIntervalInfo.setIntervalPatternInternally(currentSkeleton, lrgDiffCalUnit,
|
||||
intervalPattern.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user