/*
**************************************************************************
- * Copyright (C) 2008-2011, Google, International Business Machines
- * Corporationand others. All Rights Reserved.
+ * Copyright (C) 2008-2013, Google, International Business Machines
+ * Corporation and others. All Rights Reserved.
**************************************************************************
*/
package com.ibm.icu.text;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.MissingResourceException;
import java.util.Set;
import java.util.TreeMap;
* @stable ICU 4.2
*/
public static final int ABBREVIATED_NAME = 1;
-
+
private static final int TOTAL_STYLES = 2;
private static final long serialVersionUID = -3707773153184971529L;
}
/**
- * Create TimeUnitFormat given a ULocale and a formatting style: full or
- * abbreviated.
+ * Create TimeUnitFormat given a ULocale and a formatting style.
* @param locale locale of this time unit formatter.
* @param style format style, either FULL_NAME or ABBREVIATED_NAME style.
* @throws IllegalArgumentException if the style is not FULL_NAME or
}
/**
- * Create TimeUnitFormat given a Locale and a formatting style: full or
- * abbreviated.
+ * Create TimeUnitFormat given a Locale and a formatting style.
* @stable ICU 4.2
*/
public TimeUnitFormat(Locale locale, int style) {
}
/**
- * Set the format used for formatting or parsing. If null or not available, use the getNumberInstance(locale).
+ * Set the format used for formatting or parsing. Passing null is equivalent to passing
+ * {@link NumberFormat#getNumberInstance(ULocale)}.
* @param format the number formatter.
* @return this, for chaining.
* @stable ICU 4.0
if (isReady == false) {
return this;
}
- for (TimeUnit timeUnit : timeUnitToCountToPatterns.keySet()) {
- Map<String, Object[]> countToPattern = timeUnitToCountToPatterns.get(timeUnit);
- for (String count : countToPattern.keySet()) {
- Object[] pair = countToPattern.get(count);
+ for (Map<String, Object[]> countToPattern : timeUnitToCountToPatterns.values()) {
+ for (Object[] pair : countToPattern.values()) {
MessageFormat pattern = (MessageFormat)pair[FULL_NAME];
pattern.setFormatByArgumentIndex(0, format);
pattern = (MessageFormat)pair[ABBREVIATED_NAME];
public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
if ( !(obj instanceof TimeUnitAmount) ) {
- throw new IllegalArgumentException("can not format non TimeUnitAmount object");
+ throw new IllegalArgumentException(
+ "cannot format a non TimeUnitAmount object");
}
if (!isReady) {
setup();
MessageFormat pattern = (MessageFormat)(countToPattern.get(count))[style];
return pattern.format(new Object[]{amount.getNumber()}, toAppendTo, pos);
}
-
-
+
/**
* Parse a TimeUnitAmount.
* @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
// and looking for the longest match.
for (TimeUnit timeUnit : timeUnitToCountToPatterns.keySet()) {
Map<String, Object[]> countToPattern = timeUnitToCountToPatterns.get(timeUnit);
- for (String count : countToPattern.keySet()) {
+ for (Entry<String, Object[]> patternEntry : countToPattern.entrySet()) {
+ String count = patternEntry.getKey();
for (int styl = FULL_NAME; styl < TOTAL_STYLES; ++styl) {
- MessageFormat pattern = (MessageFormat)(countToPattern.get(count))[styl];
+ MessageFormat pattern = (MessageFormat)(patternEntry.getValue())[styl];
pos.setErrorIndex(-1);
pos.setIndex(oldPos);
// see if we can parse
if (resultNumber == null && longestParseDistance != 0) {
// set the number using plurrual count
if ( countOfLongestMatch.equals("zero") ) {
- resultNumber = new Integer(0);
+ resultNumber = Integer.valueOf(0);
} else if ( countOfLongestMatch.equals("one") ) {
- resultNumber = new Integer(1);
+ resultNumber = Integer.valueOf(1);
} else if ( countOfLongestMatch.equals("two") ) {
- resultNumber = new Integer(2);
+ resultNumber = Integer.valueOf(2);
} else {
// should not happen.
// TODO: how to handle?
- resultNumber = new Integer(3);
+ resultNumber = Integer.valueOf(3);
}
}
if (longestParseDistance == 0) {
}
pluralRules = PluralRules.forLocale(locale);
timeUnitToCountToPatterns = new HashMap<TimeUnit, Map<String, Object[]>>();
-
- setup("units", timeUnitToCountToPatterns, FULL_NAME);
- setup("unitsShort", timeUnitToCountToPatterns, ABBREVIATED_NAME);
+ Set<String> pluralKeywords = pluralRules.getKeywords();
+ setup("units/duration", timeUnitToCountToPatterns, FULL_NAME, pluralKeywords);
+ setup("unitsShort/duration", timeUnitToCountToPatterns, ABBREVIATED_NAME, pluralKeywords);
isReady = true;
}
-
-
+
private void setup(String resourceKey, Map<TimeUnit, Map<String, Object[]>> timeUnitToCountToPatterns,
- int style) {
+ int style, Set<String> pluralKeywords) {
// fill timeUnitToCountToPatterns from resource file
try {
ICUResourceBundle resource = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME, locale);
}
for ( int pluralIndex = 0; pluralIndex < count; ++pluralIndex) {
String pluralCount = oneUnitRes.get(pluralIndex).getKey();
+ if (!pluralKeywords.contains(pluralCount))
+ continue;
String pattern = oneUnitRes.get(pluralIndex).getString();
final MessageFormat messageFormat = new MessageFormat(pattern, locale);
if (format != null) {