Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public Class MeasureFormat

extends UFormat
Class Inheritance
Known Direct Subclasses
com.ibm.icu.text.TimeUnitFormat, com.ibm.icu.text.CurrencyFormat
Imports
java.io.Externalizable, .IOException, .InvalidObjectException, .ObjectInput, .ObjectOutput, .ObjectStreamException, java.math.RoundingMode, java.text.FieldPosition, .ParsePosition, java.util.Arrays, .Collection, .HashMap, .Locale, .Map, .MissingResourceException, java.util.concurrent.ConcurrentHashMap, com.ibm.icu.impl.DontCareFieldPosition, .FormattedStringBuilder, .FormattedValueStringBuilderImpl, .ICUData, .ICUResourceBundle, .SimpleCache, .SimpleFormatterImpl, .Utility, com.ibm.icu.impl.number.DecimalQuantity, .DecimalQuantity_DualStorageBCD, .LongNameHandler, .RoundingUtils, com.ibm.icu.number.IntegerWidth, .LocalizedNumberFormatter, .NumberFormatter, .NumberFormatter.UnitWidth, .Precision, com.ibm.icu.text.ListFormatter.FormattedListBuilder, com.ibm.icu.util.Currency, .ICUUncheckedIOException, .Measure, .MeasureUnit, .ULocale, .ULocale.Category, .UResourceBundle

A formatter for Measure objects.

Important Note

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.

Author
Alan Liu
ICU Status
Stable since ICU 3.0.
See Also
com.ibm.icu.text.UFormat

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
MeasureFormat.FormatWidth

Formatting width enum.

pack-priv static class
pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final int
private transient MeasureFormat.NumberFormatterCacheEntry
private transient MeasureFormat.NumberFormatterCacheEntry
private transient MeasureFormat.NumberFormatterCacheEntry
private final transient MeasureFormat.FormatWidth
private static final Map<MeasureUnit, Integer>
private static final Map<ULocale, String>
private static final SimpleCache<ULocale, MeasureFormat.NumericFormatters>
private static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
private final transient NumberFormat
private final transient LocalizedNumberFormatter
private final transient MeasureFormat.NumericFormatters
private final transient PluralRules
pack-priv static final long
private static final int

Constructor Summary

AccessConstructor and Description
pack-priv
private
pack-priv
MeasureFormat(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat numberFormat, PluralRules rules)

Method Summary

Modifier and TypeMethod and Description
pack-priv synchronized void
public final boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.
public StringBuffer
format(Object
must be a Collection<? extends Measure>, Measure[], or Measure object.
obj
,
StringBuffer
Formatted string appended here.
toAppendTo
,
FieldPosition
Identifies a field in the formatted text.
fpos
)

Implements abstract java.text.Format.format.

Able to format Collection<?
private FormattedStringBuilder
private FormattedStringBuilder
public StringBuilder

Returns:

appendTo.
formatMeasurePerUnit
(Measure
the measure object. In above example, 3.5 meters.
measure
,
MeasureUnit
the per unit. In above example, it is MeasureUnit.SECOND
perUnit
,
StringBuilder
formatted string appended here.
appendTo
,
FieldPosition
The field position.
pos
)

Formats a single measure per unit.

public final String

Returns:

the formatted string.
formatMeasures
(Measure...
a sequence of one or more measures.
measures
)

Format a sequence of measures.

public StringBuilder

Returns:

appendTo.
formatMeasures
(StringBuilder
the formatted string appended here.
appendTo
,
FieldPosition
Identifies a field in the formatted text.
fpos
,
Measure...
the measures to format.
measures
)

Formats a sequence of measures.

private void
formatMeasuresInternal(Appendable appendTo, FieldPosition fieldPosition, Measure... measures)

private void
formatMeasuresSlowTrack(ListFormatter listFormatter, Appendable appendTo, FieldPosition fieldPosition, Measure... measures)

private void
formatNumeric(Number[] hms, Appendable appendable)

private static MeasureFormat.FormatWidth
public static MeasureFormat

Returns:

a formatter object
getCurrencyFormat
(ULocale
desired locale
locale
)

Return a formatter for CurrencyAmount objects in the given locale.

public static MeasureFormat

Returns:

a formatter object
getCurrencyFormat
(Locale locale)

Return a formatter for CurrencyAmount objects in the given java.util.Locale.

public static MeasureFormat

Returns:

a formatter object
getCurrencyFormat
()

Return a formatter for CurrencyAmount objects in the default FORMAT locale.

public static MeasureFormat

Returns:

The new MeasureFormat object.
getInstance
(ULocale
the locale.
locale
,
MeasureFormat.FormatWidth
hints how long formatted strings should be.
formatWidth
)

Create a format from the locale, formatWidth, and format.

public static MeasureFormat

Returns:

The new MeasureFormat object.
getInstance
(Locale locale, MeasureFormat.FormatWidth
hints how long formatted strings should be.
formatWidth
)

Create a format from the java.util.Locale and formatWidth.

public static MeasureFormat

Returns:

The new MeasureFormat object.
getInstance
(ULocale
the locale.
locale
,
MeasureFormat.FormatWidth
hints how long formatted strings should be.
formatWidth
,
NumberFormat
This is defensively copied.
format
)

Create a format from the locale, formatWidth, and format.

public static MeasureFormat

Returns:

The new MeasureFormat object.
getInstance
(Locale locale, MeasureFormat.FormatWidth
hints how long formatted strings should be.
formatWidth
,
NumberFormat
This is defensively copied.
format
)

Create a format from the java.util.Locale, formatWidth, and format.

public final ULocale
getLocale()

Get the locale of this instance.

public NumberFormat
getNumberFormat()

Get a copy of the number format.

pack-priv NumberFormat
getNumberFormatInternal()

Get a copy of the number format without cloning.

pack-priv LocalizedNumberFormatter
public static String

Returns:

range formatter, such as "{0}–{1}"
getRangeFormat
(ULocale
locale to get the format for
forLocale
,
MeasureFormat.FormatWidth
the format width
width
)

Deprecated This API is ICU internal only.
Return a formatter (compiled SimpleFormatter pattern) for a range, such as "{0}–{1}".
public String

Returns:

The display name in the locale and width specified in MeasureFormat#getInstance, or null if there is no display name available for the specified unit.
getUnitDisplayName
(MeasureUnit
The unit for which to get a display name.
unit
)

Gets the display name of the specified MeasureUnit corresponding to the current locale and format width.

private synchronized LocalizedNumberFormatter
public MeasureFormat.FormatWidth
getWidth()

Get the format width this instance is using.

public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
private static String
private static MeasureFormat.NumericFormatters
public Measure
parseObject(String
A String, part of which should be parsed.
source
,
ParsePosition
A ParsePosition object with index and error index information as described above.
pos
)

Implements abstract java.text.Format.parseObject.

Parses text from a string to produce a Measure.
pack-priv Object
private static Number[]
toHMS(Measure[] measures)

pack-priv Object
pack-priv MeasureFormat
pack-priv MeasureFormat
private Object
Inherited from com.ibm.icu.text.UFormat:
getLocalesetLocale