ICU-10268 Mark Units API as internal deprecated and fix some minor bugs.

X-SVN-Rev: 34127
This commit is contained in:
Travis Keep 2013-08-29 18:31:11 +00:00
parent 62cb5a9ca6
commit 3a4fa13ef8
3 changed files with 146 additions and 64 deletions

View File

@ -37,6 +37,8 @@ import com.ibm.icu.util.ULocale.Category;
/** /**
* Mutable class for formatting GeneralMeasures, or sequences of them. * Mutable class for formatting GeneralMeasures, or sequences of them.
* @author markdavis * @author markdavis
* @internal
* @deprecated This API is ICU internal only.
*/ */
public class GeneralMeasureFormat extends MeasureFormat { public class GeneralMeasureFormat extends MeasureFormat {
@ -78,9 +80,8 @@ public class GeneralMeasureFormat extends MeasureFormat {
private static final long serialVersionUID = 7922671801770278517L; private static final long serialVersionUID = 7922671801770278517L;
/** /**
* @param styleToCountToFormat2 * @internal
* @param rules2 * @deprecated This API is ICU internal only.
* @param rules2
*/ */
protected GeneralMeasureFormat(ULocale locale, FormatWidth style, protected GeneralMeasureFormat(ULocale locale, FormatWidth style,
Map<MeasureUnit, EnumMap<FormatWidth, Map<String, PatternData>>> unitToStyleToCountToFormat, Map<MeasureUnit, EnumMap<FormatWidth, Map<String, PatternData>>> unitToStyleToCountToFormat,
@ -97,8 +98,8 @@ public class GeneralMeasureFormat extends MeasureFormat {
* Create a format from the locale and length * Create a format from the locale and length
* @param locale locale of this time unit formatter. * @param locale locale of this time unit formatter.
* @param length the desired length * @param length the desired length
* @draft ICU 52 * @internal
* @provisional This API might change or be removed in a future release. * @deprecated This API is ICU internal only.
*/ */
public static GeneralMeasureFormat getInstance(ULocale locale, FormatWidth length) { public static GeneralMeasureFormat getInstance(ULocale locale, FormatWidth length) {
return getInstance(locale, length, NumberFormat.getInstance(locale)); return getInstance(locale, length, NumberFormat.getInstance(locale));
@ -108,8 +109,8 @@ public class GeneralMeasureFormat extends MeasureFormat {
* Create a format from the locale and length * Create a format from the locale and length
* @param locale locale of this time unit formatter. * @param locale locale of this time unit formatter.
* @param length the desired length * @param length the desired length
* @draft ICU 52 * @internal
* @provisional This API might change or be removed in a future release. * @deprecated This API is ICU internal only.
*/ */
public static GeneralMeasureFormat getInstance(ULocale locale, FormatWidth length, public static GeneralMeasureFormat getInstance(ULocale locale, FormatWidth length,
NumberFormat decimalFormat) { NumberFormat decimalFormat) {
@ -128,8 +129,8 @@ public class GeneralMeasureFormat extends MeasureFormat {
* Return a formatter for CurrencyAmount objects in the given * Return a formatter for CurrencyAmount objects in the given
* locale. * locale.
* @param locale desired locale * @param locale desired locale
* @return a formatter object * @internal
* @stable ICU 3.0 * @deprecated This API is ICU internal only.
*/ */
public static MeasureFormat getCurrencyFormat(ULocale locale) { public static MeasureFormat getCurrencyFormat(ULocale locale) {
return new CurrencyFormat(locale); return new CurrencyFormat(locale);
@ -139,8 +140,8 @@ public class GeneralMeasureFormat extends MeasureFormat {
* Return a formatter for CurrencyAmount objects in the default * Return a formatter for CurrencyAmount objects in the default
* <code>FORMAT</code> locale. * <code>FORMAT</code> locale.
* @return a formatter object * @return a formatter object
* @see Category#FORMAT * @internal
* @stable ICU 3.0 * @deprecated This API is ICU internal only.
*/ */
public static MeasureFormat getCurrencyFormat() { public static MeasureFormat getCurrencyFormat() {
return getCurrencyFormat(ULocale.getDefault(Category.FORMAT)); return getCurrencyFormat(ULocale.getDefault(Category.FORMAT));
@ -148,6 +149,8 @@ public class GeneralMeasureFormat extends MeasureFormat {
/** /**
* @return the locale of the format. * @return the locale of the format.
* @internal
* @deprecated This API is ICU internal only.
*/ */
public ULocale getLocale() { public ULocale getLocale() {
return locale; return locale;
@ -155,6 +158,8 @@ public class GeneralMeasureFormat extends MeasureFormat {
/** /**
* @return the desired length for the format * @return the desired length for the format
* @internal
* @deprecated This API is ICU internal only.
*/ */
public FormatWidth getLength() { public FormatWidth getLength() {
return length; return length;
@ -232,73 +237,80 @@ public class GeneralMeasureFormat extends MeasureFormat {
return unitToStyleToCountToFormat; return unitToStyleToCountToFormat;
} }
/* (non-Javadoc) /**
* @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) * @internal
* @deprecated This API is ICU internal only.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public StringBuffer format(Object arg0, StringBuffer arg1, FieldPosition arg2) { public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
if (arg0 instanceof Collection) { if (obj instanceof Collection) {
Collection<Measure> coll = (Collection<Measure>) arg0; Collection<Measure> coll = (Collection<Measure>) obj;
return format(arg1, arg2, coll.toArray(new Measure[coll.size()])); return format(toAppendTo, pos, coll.toArray(new Measure[coll.size()]));
} else if (arg0 instanceof Measure[]) { } else if (obj instanceof Measure[]) {
return format(arg1, arg2, (Measure[]) arg0); return format(toAppendTo, pos, (Measure[]) obj);
} else { } else {
return format((Measure) arg0, arg1, arg2); return format((Measure) obj, toAppendTo, pos);
} }
} }
/** /**
* Format a general measure (type-safe). * Format a general measure (type-safe).
* @param measure the measure to format * @param measure the measure to format
* @param stringBuffer as in {@link #format(Object, StringBuffer, FieldPosition)} * @param toAppendTo as in {@link #format(Object, StringBuffer, FieldPosition)}
* @param fieldPosition as in {@link #format(Object, StringBuffer, FieldPosition)} * @param pos as in {@link #format(Object, StringBuffer, FieldPosition)}
* @return passed-in buffer with appended text. * @return passed-in buffer with appended text.
* @internal
* @deprecated This API is ICU internal only.
*/ */
public StringBuffer format(Measure measure, StringBuffer stringBuffer, FieldPosition fieldPosition) { public StringBuffer format(Measure measure, StringBuffer toAppendTo, FieldPosition pos) {
Number n = measure.getNumber(); Number n = measure.getNumber();
MeasureUnit unit = measure.getUnit(); MeasureUnit unit = measure.getUnit();
UFieldPosition pos = new UFieldPosition(fieldPosition.getFieldAttribute(), fieldPosition.getField()); UFieldPosition fpos = new UFieldPosition(pos.getFieldAttribute(), pos.getField());
StringBuffer formattedNumber = numberFormat.format(n, new StringBuffer(), pos); StringBuffer formattedNumber = numberFormat.format(n, new StringBuffer(), fpos);
String keyword = rules.select(new PluralRules.FixedDecimal(n.doubleValue(), pos.getCountVisibleFractionDigits(), pos.getFractionDigits())); String keyword = rules.select(new PluralRules.FixedDecimal(n.doubleValue(), fpos.getCountVisibleFractionDigits(), fpos.getFractionDigits()));
Map<FormatWidth, Map<String, PatternData>> styleToCountToFormat = unitToStyleToCountToFormat.get(unit); Map<FormatWidth, Map<String, PatternData>> styleToCountToFormat = unitToStyleToCountToFormat.get(unit);
Map<String, PatternData> countToFormat = styleToCountToFormat.get(length); Map<String, PatternData> countToFormat = styleToCountToFormat.get(length);
PatternData messagePatternData = countToFormat.get(keyword); PatternData messagePatternData = countToFormat.get(keyword);
stringBuffer.append(messagePatternData.prefix); toAppendTo.append(messagePatternData.prefix);
if (messagePatternData.suffix != null) { // there is a number (may not happen with, say, Arabic dual) if (messagePatternData.suffix != null) { // there is a number (may not happen with, say, Arabic dual)
// Fix field position // Fix field position
fieldPosition.setBeginIndex(pos.getBeginIndex() + messagePatternData.prefix.length()); pos.setBeginIndex(fpos.getBeginIndex() + messagePatternData.prefix.length());
fieldPosition.setEndIndex(pos.getEndIndex() + messagePatternData.prefix.length()); pos.setEndIndex(fpos.getEndIndex() + messagePatternData.prefix.length());
stringBuffer.append(formattedNumber); toAppendTo.append(formattedNumber);
stringBuffer.append(messagePatternData.suffix); toAppendTo.append(messagePatternData.suffix);
} }
return stringBuffer; return toAppendTo;
} }
/** /**
* Format a sequence of measures. * Format a sequence of measures.
* @param stringBuffer as in {@link #format(Object, StringBuffer, FieldPosition)} * @param toAppendto as in {@link #format(Object, StringBuffer, FieldPosition)}
* @param fieldPosition as in {@link #format(Object, StringBuffer, FieldPosition)} * @param pos as in {@link #format(Object, StringBuffer, FieldPosition)}
* @param measures a sequence of one or more measures. * @param measures a sequence of one or more measures.
* @return passed-in buffer with appended text. * @return passed-in buffer with appended text.
* @internal
* @deprecated This API is ICU internal only.
*/ */
public StringBuffer format(StringBuffer stringBuffer, FieldPosition fieldPosition, Measure... measures) { public StringBuffer format(StringBuffer toAppendto, FieldPosition pos, Measure... measures) {
StringBuffer[] results = new StringBuffer[measures.length]; StringBuffer[] results = new StringBuffer[measures.length];
for (int i = 0; i < measures.length; ++i) { for (int i = 0; i < measures.length; ++i) {
results[i] = format(measures[i], new StringBuffer(), fieldPosition); results[i] = format(measures[i], new StringBuffer(), pos);
} }
ListFormatter listFormatter = ListFormatter.getInstance(locale, ListFormatter listFormatter = ListFormatter.getInstance(locale,
length == FormatWidth.WIDE ? ListFormatter.Style.DURATION : ListFormatter.Style.DURATION_SHORT); length == FormatWidth.WIDE ? ListFormatter.Style.DURATION : ListFormatter.Style.DURATION_SHORT);
return stringBuffer.append(listFormatter.format(results)); return toAppendto.append(listFormatter.format((Object[]) results));
} }
/** /**
* Format a sequence of measures. * Format a sequence of measures.
* @param measures a sequence of one or more measures. * @param measures a sequence of one or more measures.
* @return passed-in buffer with appended text. * @return passed-in buffer with appended text.
* @internal
* @deprecated This API is ICU internal only.
*/ */
public String format(Measure... measures) { public String format(Measure... measures) {
StringBuffer result = format(new StringBuffer(), new FieldPosition(0), measures); StringBuffer result = format(new StringBuffer(), new FieldPosition(0), measures);
@ -428,6 +440,11 @@ public class GeneralMeasureFormat extends MeasureFormat {
return size; return size;
} }
} }
/**
* @internal
* @deprecated This API is ICU internal only.
*/
@Override @Override
public Measure parseObject(String toParse, ParsePosition parsePosition) { public Measure parseObject(String toParse, ParsePosition parsePosition) {
if (parseData == null) { if (parseData == null) {
@ -489,6 +506,10 @@ public class GeneralMeasureFormat extends MeasureFormat {
} }
}; };
/**
* @internal
* @deprecated This API is ICU internal only.
*/
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null || obj.getClass() != GeneralMeasureFormat.class) { if (obj == null || obj.getClass() != GeneralMeasureFormat.class) {
@ -500,6 +521,10 @@ public class GeneralMeasureFormat extends MeasureFormat {
&& numberFormat.equals(other.numberFormat); && numberFormat.equals(other.numberFormat);
} }
/**
* @internal
* @deprecated This API is ICU internal only.
*/
@Override @Override
public int hashCode() { public int hashCode() {
// TODO Auto-generated method stub // TODO Auto-generated method stub

View File

@ -8,10 +8,26 @@ package com.ibm.icu.util;
/** /**
* General purpose formatting width enum. * General purpose formatting width enum.
* @internal
* @deprecated This API is ICU internal only.
*/ */
public enum FormatWidth { public enum FormatWidth {
/**
* @internal
* @deprecated This API is ICU internal only.
*/
WIDE("units"), WIDE("units"),
/**
* @internal
* @deprecated This API is ICU internal only.
*/
SHORT("unitsShort"), SHORT("unitsShort"),
/**
* @internal
* @deprecated This API is ICU internal only.
*/
NARROW("unitsNarrow"); NARROW("unitsNarrow");
/** /**

View File

@ -12,10 +12,12 @@ import java.io.ObjectInput;
import java.io.ObjectOutput; import java.io.ObjectOutput;
import java.io.ObjectStreamException; import java.io.ObjectStreamException;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.Set; import java.util.Set;
@ -38,10 +40,21 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
private static final Map<String, Map<String,MeasureUnit>> cache private static final Map<String, Map<String,MeasureUnit>> cache
= new HashMap<String, Map<String,MeasureUnit>>(); = new HashMap<String, Map<String,MeasureUnit>>();
protected final String type;
protected final String code;
/** /**
* @param code * @internal
* @deprecated This API is ICU internal only.
*/
protected final String type;
/**
* @internal
* @deprecated This API is ICU internal only.
*/
protected final String code;
/**
* @internal
* @deprecated This API is ICU internal only.
*/ */
protected MeasureUnit(String type, String code) { protected MeasureUnit(String type, String code) {
this.type = type; this.type = type;
@ -50,13 +63,19 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/** /**
* Create an instance of a measurement unit. * Create an instance of a measurement unit.
* <p>
* Warning: Currently, the values of the parameters depend on the structure of
* ICU resource bundles. Do not use this function unless you know what you are
* doing.
*
* @param type the type, such as "length" * @param type the type, such as "length"
* @param code the code, such as "meter" * @param code the code, such as "meter"
* @return the unit. * @return the unit.
* @draft ICU 52 * @internal
* @provisional This API might change or be removed in a future release. * @deprecated This API is ICU internal only.
*/ */
public static MeasureUnit getInstance(String type, String code) { public static MeasureUnit getInstance(String type, String code) {
synchronized (MeasureUnit.class){
Map<String, MeasureUnit> tmp = cache.get(type); Map<String, MeasureUnit> tmp = cache.get(type);
if (tmp != null) { if (tmp != null) {
MeasureUnit result = tmp.get(code); MeasureUnit result = tmp.get(code);
@ -64,17 +83,20 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
return result; return result;
} }
} }
if (type == null || !ASCII.containsAll(type) || code == null || ASCII_HYPHEN.containsAll(code)) { }
if (type == null || !ASCII.containsAll(type) || code == null || !ASCII_HYPHEN.containsAll(code)) {
throw new NullPointerException("The type or code are invalid."); throw new NullPointerException("The type or code are invalid.");
} }
synchronized (MeasureUnit.class) { return MeasureUnit.addUnit(type, code, UNIT_FACTORY);
return (Currency) MeasureUnit.addUnit(type, code, UNIT_FACTORY);
}
} }
static final UnicodeSet ASCII = new UnicodeSet('a', 'z').freeze(); static final UnicodeSet ASCII = new UnicodeSet('a', 'z').freeze();
static final UnicodeSet ASCII_HYPHEN = new UnicodeSet('-', '-', 'a', 'z').freeze(); static final UnicodeSet ASCII_HYPHEN = new UnicodeSet('-', '-', 'a', 'z').freeze();
/**
* @internal
* @deprecated This API is ICU internal only.
*/
protected interface Factory { protected interface Factory {
MeasureUnit create(String type, String code); MeasureUnit create(String type, String code);
} }
@ -152,7 +174,11 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
} }
// Must only be called at static initialization, or inside synchronized block. // Must only be called at static initialization, or inside synchronized block.
protected static MeasureUnit addUnit(String type, String unitName, Factory factory) { /**
* @internal
* @deprecated This API is ICU internal only.
*/
protected synchronized static MeasureUnit addUnit(String type, String unitName, Factory factory) {
Map<String, MeasureUnit> tmp = cache.get(type); Map<String, MeasureUnit> tmp = cache.get(type);
if (tmp == null) { if (tmp == null) {
cache.put(type, tmp = new HashMap<String, MeasureUnit>()); cache.put(type, tmp = new HashMap<String, MeasureUnit>());
@ -170,25 +196,31 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/** /**
* Get all of the available general units' types. * Get all of the available general units' types.
* @return available units * @return available units
* @internal
* @deprecated This API is ICU internal only.
*/ */
public static Set<String> getAvailableTypes() { public synchronized static Set<String> getAvailableTypes() {
return Collections.unmodifiableSet(cache.keySet()); return new HashSet<String>(cache.keySet());
} }
/** /**
* Get all of the available general units for a given type. * Get all of the available general units for a given type.
* @return available units * @return available units
* @internal
* @deprecated This API is ICU internal only.
*/ */
public static Collection<MeasureUnit> getAvailable(String type) { public synchronized static Collection<MeasureUnit> getAvailable(String type) {
Map<String, MeasureUnit> units = cache.get(type); Map<String, MeasureUnit> units = cache.get(type);
return units == null ? null : Collections.unmodifiableCollection(units.values()); return units == null ? null : new ArrayList<MeasureUnit>(units.values());
} }
/** /**
* Get all of the available general units. * Get all of the available general units.
* @return available units * @return available units
* @internal
* @deprecated This API is ICU internal only.
*/ */
public static Set<MeasureUnit> getAvailable() { public synchronized static Set<MeasureUnit> getAvailable() {
Set<MeasureUnit> result = new TreeSet<MeasureUnit>(); Set<MeasureUnit> result = new TreeSet<MeasureUnit>();
for (String type : new TreeSet<String>(MeasureUnit.getAvailableTypes())) { for (String type : new TreeSet<String>(MeasureUnit.getAvailableTypes())) {
for (MeasureUnit unit : MeasureUnit.getAvailable(type)) { for (MeasureUnit unit : MeasureUnit.getAvailable(type)) {
@ -199,8 +231,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
} }
/** /**
* Return a hashcode for this currency. * @internal
* @stable ICU 2.2 * @deprecated This API is ICU internal only.
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
@ -208,9 +240,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
} }
/** /**
* Return true if rhs is a Currency instance, * @internal
* is non-null, and has the same currency code. * @deprecated This API is ICU internal only.
* @stable ICU 2.2
*/ */
@Override @Override
public boolean equals(Object rhs) { public boolean equals(Object rhs) {
@ -225,6 +256,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
} }
} }
/** /**
* @internal
* @deprecated This API is ICU internal only.
*/ */
public int compareTo(MeasureUnit other) { public int compareTo(MeasureUnit other) {
int diff; int diff;
@ -233,6 +266,10 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
: code.compareTo(other.code); : code.compareTo(other.code);
} }
/**
* @internal
* @deprecated This API is ICU internal only.
*/
@Override @Override
public String toString() { public String toString() {
return type + "-" + code; return type + "-" + code;
@ -240,6 +277,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/** /**
* @return the type for this unit * @return the type for this unit
* @internal
* @deprecated This API is ICU internal only.
*/ */
public String getType() { public String getType() {
return type; return type;
@ -247,6 +286,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/** /**
* @return the code for this unit. * @return the code for this unit.
* @internal
* @deprecated This API is ICU internal only.
*/ */
public String getCode() { public String getCode() {
return code; return code;
@ -254,8 +295,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/** /**
* Useful constants. Not necessarily complete: see {@link #getAvailable()}. * Useful constants. Not necessarily complete: see {@link #getAvailable()}.
* @draft ICU 52 * @internal
* @provisional This API might change or be removed in a future release. * @deprecated This API is ICU internal only.
*/ */
public static final MeasureUnit public static final MeasureUnit
/** Constant for unit of acceleration: g-force */ /** Constant for unit of acceleration: g-force */