Class MeasureFormat
- java.lang.Object
-
- java.text.Format
-
- com.ibm.icu.text.UFormat
-
- com.ibm.icu.text.MeasureFormat
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
- Direct Known Subclasses:
CurrencyFormat
,TimeUnitFormat
public class MeasureFormat extends UFormat
A formatter for Measure objects.IMPORTANT: New users are strongly encouraged to see if
NumberFormatter
fits their use case. Although not deprecated, this class, MeasureFormat, is provided for backwards compatibility only, and has much more limited capabilities.
To format a Measure object, first create a formatter object using a MeasureFormat factory method. Then use that object's format or formatMeasures methods. Here is sample code:
MeasureFormat fmtFr = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.SHORT); Measure measure = new Measure(23, MeasureUnit.CELSIUS); // Output: 23 °C System.out.println(fmtFr.format(measure)); Measure measureF = new Measure(70, MeasureUnit.FAHRENHEIT); // Output: 70 °F System.out.println(fmtFr.format(measureF)); MeasureFormat fmtFrFull = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.WIDE); // Output: 70 pieds et 5,3 pouces System.out.println(fmtFrFull.formatMeasures(new Measure(70, MeasureUnit.FOOT), new Measure(5.3, MeasureUnit.INCH))); // Output: 1 pied et 1 pouce System.out.println( fmtFrFull.formatMeasures(new Measure(1, MeasureUnit.FOOT), new Measure(1, MeasureUnit.INCH))); MeasureFormat fmtFrNarrow = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.NARROW); // Output: 1′ 1″ System.out.println(fmtFrNarrow.formatMeasures(new Measure(1, MeasureUnit.FOOT), new Measure(1, MeasureUnit.INCH))); MeasureFormat fmtEn = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE); // Output: 1 inch, 2 feet fmtEn.formatMeasures(new Measure(1, MeasureUnit.INCH), new Measure(2, MeasureUnit.FOOT));
This class does not do conversions from one unit to another. It simply formats whatever units it is given
This class is immutable and thread-safe so long as its deprecated subclass, TimeUnitFormat, is never used. TimeUnitFormat is not thread-safe, and is mutable. Although this class has existing subclasses, this class does not support new sub-classes.
- See Also:
UFormat
, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MeasureFormat.FormatWidth
Formatting width enum.(package private) static class
MeasureFormat.MeasureProxy
(package private) static class
MeasureFormat.NumberFormatterCacheEntry
(package private) static class
MeasureFormat.NumericFormatters
-
Nested classes/interfaces inherited from class com.ibm.icu.text.UFormat
UFormat.SpanField
-
-
Field Summary
Fields Modifier and Type Field Description private static int
CURRENCY_FORMAT
private MeasureFormat.NumberFormatterCacheEntry
formatter1
private MeasureFormat.NumberFormatterCacheEntry
formatter2
private MeasureFormat.NumberFormatterCacheEntry
formatter3
private MeasureFormat.FormatWidth
formatWidth
private static java.util.Map<MeasureUnit,java.lang.Integer>
hmsTo012
private static java.util.Map<ULocale,java.lang.String>
localeIdToRangeFormat
private static SimpleCache<ULocale,MeasureFormat.NumericFormatters>
localeToNumericDurationFormatters
private static int
MEASURE_FORMAT
(package private) static int
NUMBER_FORMATTER_CURRENCY
(package private) static int
NUMBER_FORMATTER_INTEGER
(package private) static int
NUMBER_FORMATTER_STANDARD
private NumberFormat
numberFormat
private LocalizedNumberFormatter
numberFormatter
private MeasureFormat.NumericFormatters
numericFormatters
private PluralRules
rules
(package private) static long
serialVersionUID
private static int
TIME_UNIT_FORMAT
-
Constructor Summary
Constructors Modifier Constructor Description (package private)
MeasureFormat(ULocale locale, MeasureFormat.FormatWidth formatWidth)
(package private)
MeasureFormat(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat numberFormat, PluralRules rules)
private
MeasureFormat(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat numberFormat, PluralRules rules, MeasureFormat.NumericFormatters formatters)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description (package private) void
clearCache()
boolean
equals(java.lang.Object other)
Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.java.lang.StringBuffer
format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition fpos)
Able to format Collection<? extends Measure>, Measure[], and Measure by delegating to formatMeasures.private FormattedStringBuilder
formatMeasure(Measure measure)
private FormattedStringBuilder
formatMeasureInteger(Measure measure)
java.lang.StringBuilder
formatMeasurePerUnit(Measure measure, MeasureUnit perUnit, java.lang.StringBuilder appendTo, java.text.FieldPosition pos)
Formats a single measure per unit.java.lang.String
formatMeasures(Measure... measures)
Format a sequence of measures.java.lang.StringBuilder
formatMeasures(java.lang.StringBuilder appendTo, java.text.FieldPosition fpos, Measure... measures)
Formats a sequence of measures.private void
formatMeasuresInternal(java.lang.Appendable appendTo, java.text.FieldPosition fieldPosition, Measure... measures)
private void
formatMeasuresSlowTrack(ListFormatter listFormatter, java.lang.Appendable appendTo, java.text.FieldPosition fieldPosition, Measure... measures)
private void
formatNumeric(java.lang.Number[] hms, java.lang.Appendable appendable)
private static MeasureFormat.FormatWidth
fromFormatWidthOrdinal(int ordinal)
static MeasureFormat
getCurrencyFormat()
Return a formatter for CurrencyAmount objects in the defaultFORMAT
locale.static MeasureFormat
getCurrencyFormat(ULocale locale)
Return a formatter for CurrencyAmount objects in the given locale.static MeasureFormat
getCurrencyFormat(java.util.Locale locale)
Return a formatter for CurrencyAmount objects in the givenLocale
.static MeasureFormat
getInstance(ULocale locale, MeasureFormat.FormatWidth formatWidth)
Create a format from the locale, formatWidth, and format.static MeasureFormat
getInstance(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)
Create a format from the locale, formatWidth, and format.static MeasureFormat
getInstance(java.util.Locale locale, MeasureFormat.FormatWidth formatWidth)
Create a format from theLocale
and formatWidth.static MeasureFormat
getInstance(java.util.Locale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)
Create a format from theLocale
, formatWidth, and format.ULocale
getLocale()
Get the locale of this instance.NumberFormat
getNumberFormat()
Get a copy of the number format.(package private) NumberFormat
getNumberFormatInternal()
Get a copy of the number format without cloning.(package private) LocalizedNumberFormatter
getNumberFormatter()
static java.lang.String
getRangeFormat(ULocale forLocale, MeasureFormat.FormatWidth width)
Deprecated.This API is ICU internal only.java.lang.String
getUnitDisplayName(MeasureUnit unit)
Gets the display name of the specifiedMeasureUnit
corresponding to the current locale and format width.private LocalizedNumberFormatter
getUnitFormatterFromCache(int type, MeasureUnit unit, MeasureUnit perUnit)
MeasureFormat.FormatWidth
getWidth()
Get the format width this instance is using.int
hashCode()
private static java.lang.String
loadNumericDurationFormat(ICUResourceBundle r, java.lang.String type)
private static MeasureFormat.NumericFormatters
loadNumericFormatters(ULocale locale)
Measure
parseObject(java.lang.String source, java.text.ParsePosition pos)
Parses text from a string to produce aMeasure
.(package private) java.lang.Object
toCurrencyProxy()
private static java.lang.Number[]
toHMS(Measure[] measures)
(package private) java.lang.Object
toTimeUnitProxy()
(package private) MeasureFormat
withLocale(ULocale locale)
(package private) MeasureFormat
withNumberFormat(NumberFormat format)
private java.lang.Object
writeReplace()
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
- See Also:
- Constant Field Values
-
formatWidth
private final transient MeasureFormat.FormatWidth formatWidth
-
rules
private final transient PluralRules rules
-
numericFormatters
private final transient MeasureFormat.NumericFormatters numericFormatters
-
numberFormat
private final transient NumberFormat numberFormat
-
numberFormatter
private final transient LocalizedNumberFormatter numberFormatter
-
localeToNumericDurationFormatters
private static final SimpleCache<ULocale,MeasureFormat.NumericFormatters> localeToNumericDurationFormatters
-
hmsTo012
private static final java.util.Map<MeasureUnit,java.lang.Integer> hmsTo012
-
MEASURE_FORMAT
private static final int MEASURE_FORMAT
- See Also:
- Constant Field Values
-
TIME_UNIT_FORMAT
private static final int TIME_UNIT_FORMAT
- See Also:
- Constant Field Values
-
CURRENCY_FORMAT
private static final int CURRENCY_FORMAT
- See Also:
- Constant Field Values
-
NUMBER_FORMATTER_STANDARD
static final int NUMBER_FORMATTER_STANDARD
- See Also:
- Constant Field Values
-
NUMBER_FORMATTER_CURRENCY
static final int NUMBER_FORMATTER_CURRENCY
- See Also:
- Constant Field Values
-
NUMBER_FORMATTER_INTEGER
static final int NUMBER_FORMATTER_INTEGER
- See Also:
- Constant Field Values
-
formatter1
private transient MeasureFormat.NumberFormatterCacheEntry formatter1
-
formatter2
private transient MeasureFormat.NumberFormatterCacheEntry formatter2
-
formatter3
private transient MeasureFormat.NumberFormatterCacheEntry formatter3
-
localeIdToRangeFormat
private static final java.util.Map<ULocale,java.lang.String> localeIdToRangeFormat
-
-
Constructor Detail
-
MeasureFormat
MeasureFormat(ULocale locale, MeasureFormat.FormatWidth formatWidth)
-
MeasureFormat
private MeasureFormat(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat numberFormat, PluralRules rules, MeasureFormat.NumericFormatters formatters)
-
MeasureFormat
MeasureFormat(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat numberFormat, PluralRules rules)
-
-
Method Detail
-
getInstance
public static MeasureFormat getInstance(ULocale locale, MeasureFormat.FormatWidth formatWidth)
Create a format from the locale, formatWidth, and format.- Parameters:
locale
- the locale.formatWidth
- hints how long formatted strings should be.- Returns:
- The new MeasureFormat object.
-
getInstance
public static MeasureFormat getInstance(java.util.Locale locale, MeasureFormat.FormatWidth formatWidth)
Create a format from theLocale
and formatWidth.- Parameters:
locale
- theLocale
.formatWidth
- hints how long formatted strings should be.- Returns:
- The new MeasureFormat object.
-
getInstance
public static MeasureFormat getInstance(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)
Create a format from the locale, formatWidth, and format.- Parameters:
locale
- the locale.formatWidth
- hints how long formatted strings should be.format
- This is defensively copied.- Returns:
- The new MeasureFormat object.
-
getInstance
public static MeasureFormat getInstance(java.util.Locale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)
Create a format from theLocale
, formatWidth, and format.- Parameters:
locale
- theLocale
.formatWidth
- hints how long formatted strings should be.format
- This is defensively copied.- Returns:
- The new MeasureFormat object.
-
format
public java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition fpos)
Able to format Collection<? extends Measure>, Measure[], and Measure by delegating to formatMeasures. If the pos argument identifies a NumberFormat field, then its indices are set to the beginning and end of the first such field encountered. MeasureFormat itself does not supply any fields. Calling aformatMeasures
method is preferred over calling this method as they give better performance.- Specified by:
format
in classjava.text.Format
- Parameters:
obj
- must be a Collection<? extends Measure>, Measure[], or Measure object.toAppendTo
- Formatted string appended here.fpos
- Identifies a field in the formatted text.- See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
-
parseObject
public Measure parseObject(java.lang.String source, java.text.ParsePosition pos)
Parses text from a string to produce aMeasure
.- Specified by:
parseObject
in classjava.text.Format
- Throws:
java.lang.UnsupportedOperationException
- Not supported.- See Also:
Format.parseObject(java.lang.String, java.text.ParsePosition)
-
formatMeasures
public final java.lang.String formatMeasures(Measure... measures)
Format a sequence of measures. Uses the ListFormatter unit lists. So, for example, one could format “3 feet, 2 inches”. Zero values are formatted (eg, “3 feet, 0 inches”). It is the caller’s responsibility to have the appropriate values in appropriate order, and using the appropriate Number values. Typically the units should be in descending order, with all but the last Measure having integer values (eg, not “3.2 feet, 2 inches”).- Parameters:
measures
- a sequence of one or more measures.- Returns:
- the formatted string.
-
formatMeasurePerUnit
public java.lang.StringBuilder formatMeasurePerUnit(Measure measure, MeasureUnit perUnit, java.lang.StringBuilder appendTo, java.text.FieldPosition pos)
Formats a single measure per unit. An example of such a formatted string is "3.5 meters per second."- Parameters:
measure
- the measure object. In above example, 3.5 meters.perUnit
- the per unit. In above example, it is MeasureUnit.SECONDappendTo
- formatted string appended here.pos
- The field position.- Returns:
- appendTo.
-
formatMeasures
public java.lang.StringBuilder formatMeasures(java.lang.StringBuilder appendTo, java.text.FieldPosition fpos, Measure... measures)
Formats a sequence of measures. If the fieldPosition argument identifies a NumberFormat field, then its indices are set to the beginning and end of the first such field encountered. MeasureFormat itself does not supply any fields.- Parameters:
appendTo
- the formatted string appended here.fpos
- Identifies a field in the formatted text.measures
- the measures to format.- Returns:
- appendTo.
- See Also:
formatMeasures(Measure...)
-
formatMeasuresInternal
private void formatMeasuresInternal(java.lang.Appendable appendTo, java.text.FieldPosition fieldPosition, Measure... measures)
-
getUnitDisplayName
public java.lang.String getUnitDisplayName(MeasureUnit unit)
Gets the display name of the specifiedMeasureUnit
corresponding to the current locale and format width.- Parameters:
unit
- The unit for which to get a display name.- Returns:
- The display name in the locale and width specified in
getInstance(com.ibm.icu.util.ULocale, com.ibm.icu.text.MeasureFormat.FormatWidth)
, or null if there is no display name available for the specified unit.
-
equals
public final boolean equals(java.lang.Object other)
Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.- Overrides:
equals
in classjava.lang.Object
-
hashCode
public final int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
getWidth
public MeasureFormat.FormatWidth getWidth()
Get the format width this instance is using.
-
getLocale
public final ULocale getLocale()
Get the locale of this instance.
-
getNumberFormat
public NumberFormat getNumberFormat()
Get a copy of the number format.
-
getNumberFormatInternal
NumberFormat getNumberFormatInternal()
Get a copy of the number format without cloning. Internal method.
-
getCurrencyFormat
public static MeasureFormat getCurrencyFormat(ULocale locale)
Return a formatter for CurrencyAmount objects in the given locale.- Parameters:
locale
- desired locale- Returns:
- a formatter object
-
getCurrencyFormat
public static MeasureFormat getCurrencyFormat(java.util.Locale locale)
Return a formatter for CurrencyAmount objects in the givenLocale
.- Parameters:
locale
- desiredLocale
- Returns:
- a formatter object
-
getCurrencyFormat
public static MeasureFormat getCurrencyFormat()
Return a formatter for CurrencyAmount objects in the defaultFORMAT
locale.- Returns:
- a formatter object
- See Also:
ULocale.Category.FORMAT
-
withLocale
MeasureFormat withLocale(ULocale locale)
-
withNumberFormat
MeasureFormat withNumberFormat(NumberFormat format)
-
loadNumericFormatters
private static MeasureFormat.NumericFormatters loadNumericFormatters(ULocale locale)
-
getUnitFormatterFromCache
private LocalizedNumberFormatter getUnitFormatterFromCache(int type, MeasureUnit unit, MeasureUnit perUnit)
-
clearCache
void clearCache()
-
getNumberFormatter
LocalizedNumberFormatter getNumberFormatter()
-
formatMeasure
private FormattedStringBuilder formatMeasure(Measure measure)
-
formatMeasureInteger
private FormattedStringBuilder formatMeasureInteger(Measure measure)
-
formatMeasuresSlowTrack
private void formatMeasuresSlowTrack(ListFormatter listFormatter, java.lang.Appendable appendTo, java.text.FieldPosition fieldPosition, Measure... measures)
-
loadNumericDurationFormat
private static java.lang.String loadNumericDurationFormat(ICUResourceBundle r, java.lang.String type)
-
toHMS
private static java.lang.Number[] toHMS(Measure[] measures)
-
formatNumeric
private void formatNumeric(java.lang.Number[] hms, java.lang.Appendable appendable)
-
toTimeUnitProxy
java.lang.Object toTimeUnitProxy()
-
toCurrencyProxy
java.lang.Object toCurrencyProxy()
-
writeReplace
private java.lang.Object writeReplace() throws java.io.ObjectStreamException
- Throws:
java.io.ObjectStreamException
-
fromFormatWidthOrdinal
private static MeasureFormat.FormatWidth fromFormatWidthOrdinal(int ordinal)
-
getRangeFormat
@Deprecated public static java.lang.String getRangeFormat(ULocale forLocale, MeasureFormat.FormatWidth width)
Deprecated.This API is ICU internal only.Return a formatter (compiled SimpleFormatter pattern) for a range, such as "{0}–{1}".- Parameters:
forLocale
- locale to get the format forwidth
- the format width- Returns:
- range formatter, such as "{0}–{1}"
-
-