use of android.icu.util.TimeUnit in project j2objc by google.
the class TimeUnitFormat method setup.
private void setup(String resourceKey, Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns, int style, Set<String> pluralKeywords) {
// fill timeUnitToCountToPatterns from resource file
try {
ICUResourceBundle resource = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_UNIT_BASE_NAME, locale);
TimeUnitFormatSetupSink sink = new TimeUnitFormatSetupSink(timeUnitToCountToPatterns, style, pluralKeywords, locale);
resource.getAllItemsWithFallback(resourceKey, sink);
} catch (MissingResourceException e) {
}
// there should be patterns for each plural rule in each time unit.
// For each time unit,
// for each plural rule, following is unit pattern fall-back rule:
// ( for example: "one" hour )
// look for its unit pattern in its locale tree.
// if pattern is not found in its own locale, such as de_DE,
// look for the pattern in its parent, such as de,
// keep looking till found or till root.
// if the pattern is not found in root either,
// fallback to plural count "other",
// look for the pattern of "other" in the locale tree:
// "de_DE" to "de" to "root".
// If not found, fall back to value of
// static variable DEFAULT_PATTERN_FOR_xxx, such as "{0} h".
//
// Following is consistency check to create pattern for each
// plural rule in each time unit using above fall-back rule.
//
final TimeUnit[] timeUnits = TimeUnit.values();
Set<String> keywords = pluralRules.getKeywords();
for (int i = 0; i < timeUnits.length; ++i) {
// for each time unit,
// get all the patterns for each plural rule in this locale.
final TimeUnit timeUnit = timeUnits[i];
Map<String, Object[]> countToPatterns = timeUnitToCountToPatterns.get(timeUnit);
if (countToPatterns == null) {
countToPatterns = new TreeMap<String, Object[]>();
timeUnitToCountToPatterns.put(timeUnit, countToPatterns);
}
for (String pluralCount : keywords) {
if (countToPatterns.get(pluralCount) == null || countToPatterns.get(pluralCount)[style] == null) {
// look through parents
searchInTree(resourceKey, style, timeUnit, pluralCount, pluralCount, countToPatterns);
}
}
}
}
use of android.icu.util.TimeUnit in project j2objc by google.
the class TimeUnitTest method formatParsing.
private void formatParsing(TimeUnitFormat format) {
final TimeUnit[] values = TimeUnit.values();
for (int j = 0; j < values.length; ++j) {
final TimeUnit timeUnit = values[j];
double[] tests = { 0, 0.5, 1, 2, 3, 5 };
for (int i = 0; i < tests.length; ++i) {
TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
String formatted = format.format(source);
// System.out.println(formatted);
logln(tests[i] + " => " + formatted);
try {
TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
}
} catch (ParseException e) {
errln(e.getMessage());
}
}
}
}
use of android.icu.util.TimeUnit in project j2objc by google.
the class TimeUnitTest method TestBasic.
@Test
public void TestBasic() {
String[] locales = { "en", "sl", "fr", "zh", "ar", "ru", "zh_Hant" };
for (int locIndex = 0; locIndex < locales.length; ++locIndex) {
// System.out.println("locale: " + locales[locIndex]);
TimeUnitFormat[] formats = new TimeUnitFormat[] { new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.FULL_NAME), new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.ABBREVIATED_NAME) };
for (int style = TimeUnitFormat.FULL_NAME; style <= TimeUnitFormat.ABBREVIATED_NAME; ++style) {
final TimeUnit[] values = TimeUnit.values();
for (int j = 0; j < values.length; ++j) {
final TimeUnit timeUnit = values[j];
double[] tests = { 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35 };
for (int i = 0; i < tests.length; ++i) {
TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
String formatted = formats[style].format(source);
// System.out.println(formatted);
logln(tests[i] + " => " + formatted);
try {
// Style should not matter when parsing.
for (int parseStyle = TimeUnitFormat.FULL_NAME; parseStyle <= TimeUnitFormat.ABBREVIATED_NAME; parseStyle++) {
TimeUnitAmount result = (TimeUnitAmount) formats[parseStyle].parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
}
}
} catch (ParseException e) {
errln(e.getMessage());
}
}
}
}
}
}
use of android.icu.util.TimeUnit in project j2objc by google.
the class TimeUnitFormat method parseObject.
/**
* Parse a TimeUnitAmount.
* @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
* @deprecated ICU 53 see {@link MeasureFormat}.
*/
@Deprecated
@Override
public TimeUnitAmount parseObject(String source, ParsePosition pos) {
if (!isReady) {
setup();
}
Number resultNumber = null;
TimeUnit resultTimeUnit = null;
int oldPos = pos.getIndex();
int newPos = -1;
int longestParseDistance = 0;
String countOfLongestMatch = null;
// and looking for the longest match.
for (TimeUnit timeUnit : timeUnitToCountToPatterns.keySet()) {
Map<String, Object[]> countToPattern = timeUnitToCountToPatterns.get(timeUnit);
for (Entry<String, Object[]> patternEntry : countToPattern.entrySet()) {
String count = patternEntry.getKey();
for (int styl = FULL_NAME; styl < TOTAL_STYLES; ++styl) {
MessageFormat pattern = (MessageFormat) (patternEntry.getValue())[styl];
pos.setErrorIndex(-1);
pos.setIndex(oldPos);
// see if we can parse
Object parsed = pattern.parseObject(source, pos);
if (pos.getErrorIndex() != -1 || pos.getIndex() == oldPos) {
// nothing parsed
continue;
}
Number temp = null;
if (((Object[]) parsed).length != 0) {
// pattern with Number as beginning,
// such as "{0} d".
// check to make sure that the timeUnit is consistent
Object tempObj = ((Object[]) parsed)[0];
if (tempObj instanceof Number) {
temp = (Number) tempObj;
} else {
// the number. When this happens we must parse the formatted number ourselves.
try {
temp = format.parse(tempObj.toString());
} catch (ParseException e) {
continue;
}
}
}
int parseDistance = pos.getIndex() - oldPos;
if (parseDistance > longestParseDistance) {
resultNumber = temp;
resultTimeUnit = timeUnit;
newPos = pos.getIndex();
longestParseDistance = parseDistance;
countOfLongestMatch = count;
}
}
}
}
/*
* After find the longest match, parse the number. Result number could be null for the pattern without number
* pattern. such as unit pattern in Arabic. When result number is null, use plural rule to set the number.
*/
if (resultNumber == null && longestParseDistance != 0) {
// set the number using plurrual count
if (countOfLongestMatch.equals("zero")) {
resultNumber = Integer.valueOf(0);
} else if (countOfLongestMatch.equals("one")) {
resultNumber = Integer.valueOf(1);
} else if (countOfLongestMatch.equals("two")) {
resultNumber = Integer.valueOf(2);
} else {
// should not happen.
// TODO: how to handle?
resultNumber = Integer.valueOf(3);
}
}
if (longestParseDistance == 0) {
pos.setIndex(oldPos);
pos.setErrorIndex(0);
return null;
} else {
pos.setIndex(newPos);
pos.setErrorIndex(-1);
return new TimeUnitAmount(resultNumber, resultTimeUnit);
}
}
use of android.icu.util.TimeUnit in project j2objc by google.
the class TimeUnitTest method TestGreek.
/*
* @bug 7902
* This tests that requests for short unit names correctly fall back
* to long unit names for a locale where the locale data does not
* provide short unit names. As of CLDR 1.9, Greek is one such language.
*/
@Test
public void TestGreek() {
String[] locales = { "el_GR", "el" };
final TimeUnit[] units = new TimeUnit[] { TimeUnit.SECOND, TimeUnit.MINUTE, TimeUnit.HOUR, TimeUnit.DAY, TimeUnit.WEEK, TimeUnit.MONTH, TimeUnit.YEAR };
int[] styles = new int[] { TimeUnitFormat.FULL_NAME, TimeUnitFormat.ABBREVIATED_NAME };
int[] numbers = new int[] { 1, 7 };
String[] expected = { // "el_GR" 1 wide
"1 δευτερόλεπτο", "1 λεπτό", "1 ώρα", "1 ημέρα", "1 εβδομάδα", "1 μήνας", "1 έτος", // "el_GR" 1 short
"1 δευτ.", "1 λεπ.", "1 ώρα", "1 ημέρα", "1 εβδ.", "1 μήν.", // year (one)
"1 έτ.", // "el_GR" 7 wide
"7 δευτερόλεπτα", "7 λεπτά", "7 ώρες", "7 ημέρες", "7 εβδομάδες", "7 μήνες", "7 έτη", // "el_GR" 7 short
"7 δευτ.", "7 λεπ.", // hour (other)
"7 ώρ.", "7 ημέρες", "7 εβδ.", "7 μήν.", // year (other)
"7 έτ.", // "el" 1 wide
"1 δευτερόλεπτο", "1 λεπτό", "1 ώρα", "1 ημέρα", "1 εβδομάδα", "1 μήνας", "1 έτος", // "el" 1 short
"1 δευτ.", "1 λεπ.", "1 ώρα", "1 ημέρα", "1 εβδ.", "1 μήν.", // year (one)
"1 έτ.", // "el" 7 wide
"7 δευτερόλεπτα", "7 λεπτά", "7 ώρες", "7 ημέρες", "7 εβδομάδες", "7 μήνες", "7 έτη", // "el" 7 short
"7 δευτ.", "7 λεπ.", // hour (other)
"7 ώρ.", "7 ημέρες", "7 εβδ.", "7 μήν.", // year (other
"7 έτ." };
int counter = 0;
TimeUnitFormat timeUnitFormat;
TimeUnitAmount timeUnitAmount;
String formatted;
for (int locIndex = 0; locIndex < locales.length; ++locIndex) {
for (int numIndex = 0; numIndex < numbers.length; ++numIndex) {
for (int styleIndex = 0; styleIndex < styles.length; ++styleIndex) {
for (int unitIndex = 0; unitIndex < units.length; ++unitIndex) {
timeUnitAmount = new TimeUnitAmount(numbers[numIndex], units[unitIndex]);
timeUnitFormat = new TimeUnitFormat(new ULocale(locales[locIndex]), styles[styleIndex]);
formatted = timeUnitFormat.format(timeUnitAmount);
assertEquals("locale: " + locales[locIndex] + ", style: " + styles[styleIndex] + ", units: " + units[unitIndex] + ", value: " + numbers[numIndex], expected[counter], formatted);
++counter;
}
}
}
}
}
Aggregations