mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-07 22:44:49 +00:00
ICU-12413 Check in revised data loading
X-SVN-Rev: 38877
This commit is contained in:
parent
79bb830dde
commit
d597a30abe
1 changed files with 47 additions and 40 deletions
|
@ -10,12 +10,11 @@ package com.ibm.icu.impl;
|
|||
|
||||
import java.text.FieldPosition;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.Comparator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import com.ibm.icu.impl.UResource;
|
||||
import com.ibm.icu.lang.UCharacter;
|
||||
import com.ibm.icu.text.BreakIterator;
|
||||
import com.ibm.icu.text.DateFormat;
|
||||
|
@ -26,7 +25,6 @@ import com.ibm.icu.util.Calendar;
|
|||
import com.ibm.icu.util.TimeZone;
|
||||
import com.ibm.icu.util.ULocale;
|
||||
import com.ibm.icu.util.UResourceBundle;
|
||||
import com.ibm.icu.util.UResourceBundleIterator;
|
||||
|
||||
/**
|
||||
* @author srl
|
||||
|
@ -217,64 +215,73 @@ public class RelativeDateFormat extends DateFormat {
|
|||
int fDateStyle;
|
||||
int fTimeStyle;
|
||||
ULocale fLocale;
|
||||
|
||||
private transient URelativeString fDates[] = null; // array of strings
|
||||
|
||||
private transient List<URelativeString> fDates = null;
|
||||
|
||||
private boolean combinedFormatHasDateAtStart = false;
|
||||
private boolean capitalizationInfoIsSet = false;
|
||||
private boolean capitalizationOfRelativeUnitsForListOrMenu = false;
|
||||
private boolean capitalizationOfRelativeUnitsForStandAlone = false;
|
||||
private transient BreakIterator capitalizationBrkIter = null;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the string at a specific offset.
|
||||
* @param day day offset ( -1, 0, 1, etc.. )
|
||||
* @param day day offset ( -1, 0, 1, etc.. ). Does not require sorting by offset.
|
||||
* @return the string, or NULL if none at that location.
|
||||
*/
|
||||
private String getStringForDay(int day) {
|
||||
if(fDates == null) {
|
||||
loadDates();
|
||||
}
|
||||
for(int i=0;i<fDates.length;i++) {
|
||||
if(fDates[i].offset == day) {
|
||||
return fDates[i].string;
|
||||
for(URelativeString dayItem : fDates) {
|
||||
if(dayItem.offset == day) {
|
||||
return dayItem.string;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// Sink to get "fields/day/relative".
|
||||
private final class RelDateFmtDataSink extends UResource.Sink {
|
||||
|
||||
@Override
|
||||
public void put(UResource.Key key, UResource.Value value, boolean noFallback) {
|
||||
if (value.getType() == ICUResourceBundle.ALIAS) {
|
||||
return;
|
||||
}
|
||||
|
||||
UResource.Table table = value.getTable();
|
||||
for (int i = 0; table.getKeyAndValue(i, key, value); ++i) {
|
||||
|
||||
int keyOffset;
|
||||
try {
|
||||
keyOffset = Integer.parseInt(key.toString());
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
// Flag the error?
|
||||
return;
|
||||
}
|
||||
// Check if already set.
|
||||
if (getStringForDay(keyOffset) == null) {
|
||||
URelativeString newDayInfo = new URelativeString(keyOffset, value.getString());
|
||||
fDates.add(newDayInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the Date string array
|
||||
*/
|
||||
private synchronized void loadDates() {
|
||||
ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, fLocale);
|
||||
ICUResourceBundle rdb = rb.getWithFallback("fields/day/relative");
|
||||
|
||||
Set<URelativeString> datesSet = new TreeSet<URelativeString>(new Comparator<URelativeString>() {
|
||||
public int compare(URelativeString r1, URelativeString r2) {
|
||||
|
||||
if(r1.offset == r2.offset) {
|
||||
return 0;
|
||||
} else if(r1.offset < r2.offset) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}) ;
|
||||
|
||||
for(UResourceBundleIterator i = rdb.getIterator();i.hasNext();) {
|
||||
UResourceBundle line = i.next();
|
||||
|
||||
String k = line.getKey();
|
||||
String v = line.getString();
|
||||
URelativeString rs = new URelativeString(k,v);
|
||||
datesSet.add(rs);
|
||||
}
|
||||
fDates = datesSet.toArray(new URelativeString[0]);
|
||||
|
||||
// Use sink mechanism to traverse data structure.
|
||||
fDates = new ArrayList<URelativeString>();
|
||||
RelDateFmtDataSink sink = new RelDateFmtDataSink();
|
||||
rb.getAllItemsWithFallback("fields/day/relative", sink);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set capitalizationOfRelativeUnitsForListOrMenu, capitalizationOfRelativeUnitsForStandAlone
|
||||
*/
|
||||
|
@ -362,4 +369,4 @@ public class RelativeDateFormat extends DateFormat {
|
|||
fCombinedFormat = new MessageFormat(pattern, locale);
|
||||
return fCombinedFormat;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue