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

View File

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

View File

@ -12,10 +12,12 @@ import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Set;
@ -38,10 +40,21 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
private static final Map<String, Map<String,MeasureUnit>> cache
= 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) {
this.type = type;
@ -50,31 +63,40 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/**
* 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 code the code, such as "meter"
* @return the unit.
* @draft ICU 52
* @provisional This API might change or be removed in a future release.
* @internal
* @deprecated This API is ICU internal only.
*/
public static MeasureUnit getInstance(String type, String code) {
Map<String, MeasureUnit> tmp = cache.get(type);
if (tmp != null) {
MeasureUnit result = tmp.get(code);
if (result != null) {
return result;
synchronized (MeasureUnit.class){
Map<String, MeasureUnit> tmp = cache.get(type);
if (tmp != null) {
MeasureUnit result = tmp.get(code);
if (result != null) {
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.");
}
synchronized (MeasureUnit.class) {
return (Currency) MeasureUnit.addUnit(type, code, UNIT_FACTORY);
}
return MeasureUnit.addUnit(type, code, UNIT_FACTORY);
}
static final UnicodeSet ASCII = 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 {
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.
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);
if (tmp == null) {
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.
* @return available units
* @internal
* @deprecated This API is ICU internal only.
*/
public static Set<String> getAvailableTypes() {
return Collections.unmodifiableSet(cache.keySet());
public synchronized static Set<String> getAvailableTypes() {
return new HashSet<String>(cache.keySet());
}
/**
* Get all of the available general units for a given type.
* @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);
return units == null ? null : Collections.unmodifiableCollection(units.values());
return units == null ? null : new ArrayList<MeasureUnit>(units.values());
}
/**
* Get all of the available general 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>();
for (String type : new TreeSet<String>(MeasureUnit.getAvailableTypes())) {
for (MeasureUnit unit : MeasureUnit.getAvailable(type)) {
@ -199,8 +231,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
}
/**
* Return a hashcode for this currency.
* @stable ICU 2.2
* @internal
* @deprecated This API is ICU internal only.
*/
@Override
public int hashCode() {
@ -208,9 +240,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
}
/**
* Return true if rhs is a Currency instance,
* is non-null, and has the same currency code.
* @stable ICU 2.2
* @internal
* @deprecated This API is ICU internal only.
*/
@Override
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) {
int diff;
@ -233,6 +266,10 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
: code.compareTo(other.code);
}
/**
* @internal
* @deprecated This API is ICU internal only.
*/
@Override
public String toString() {
return type + "-" + code;
@ -240,6 +277,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/**
* @return the type for this unit
* @internal
* @deprecated This API is ICU internal only.
*/
public String getType() {
return type;
@ -247,6 +286,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/**
* @return the code for this unit.
* @internal
* @deprecated This API is ICU internal only.
*/
public String getCode() {
return code;
@ -254,8 +295,8 @@ public class MeasureUnit implements Comparable<MeasureUnit>, Serializable {
/**
* Useful constants. Not necessarily complete: see {@link #getAvailable()}.
* @draft ICU 52
* @provisional This API might change or be removed in a future release.
* @internal
* @deprecated This API is ICU internal only.
*/
public static final MeasureUnit
/** Constant for unit of acceleration: g-force */