use of android.icu.util.TimeUnitAmount 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.TimeUnitAmount 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;
}
}
}
}
}
use of android.icu.util.TimeUnitAmount in project j2objc by google.
the class TimeUnitTest method TestClone.
@Test
public void TestClone() {
TimeUnitFormat tuf = new TimeUnitFormat(ULocale.ENGLISH, TimeUnitFormat.ABBREVIATED_NAME);
NumberFormat nf = NumberFormat.getInstance();
tuf.setNumberFormat(nf);
TimeUnitFormat tufClone = (TimeUnitFormat) tuf.clone();
tuf.setLocale(Locale.GERMAN);
assertEquals("", "1 hr", tufClone.format(new TimeUnitAmount(1, TimeUnit.HOUR)));
}
Aggregations