Package com.ibm.icu.text
Class MeasureFormat
- java.lang.Object
-
- java.text.Format
-
- com.ibm.icu.text.UFormat
-
- com.ibm.icu.text.MeasureFormat
-
- All Implemented Interfaces:
Serializable,Cloneable
- Direct Known Subclasses:
TimeUnitFormat
public class MeasureFormat extends UFormat
A formatter for Measure objects.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 classMeasureFormat.FormatWidthFormatting width enum.-
Nested classes/interfaces inherited from class java.text.Format
Format.Field
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanequals(Object other)Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.StringBufferformat(Object obj, StringBuffer toAppendTo, FieldPosition pos)Able to format Collection<?StringBuilderformatMeasurePerUnit(Measure measure, MeasureUnit perUnit, StringBuilder appendTo, FieldPosition pos)Formats a single measure per unit.StringformatMeasureRange(Measure lowValue, Measure highValue)Deprecated.This API is ICU internal only.StringformatMeasures(Measure... measures)Format a sequence of measures.StringBuilderformatMeasures(StringBuilder appendTo, FieldPosition fieldPosition, Measure... measures)Formats a sequence of measures.static MeasureFormatgetCurrencyFormat()Return a formatter for CurrencyAmount objects in the defaultFORMATlocale.static MeasureFormatgetCurrencyFormat(ULocale locale)Return a formatter for CurrencyAmount objects in the given locale.static MeasureFormatgetCurrencyFormat(Locale locale)Return a formatter for CurrencyAmount objects in the given JDK locale.static MeasureFormatgetInstance(ULocale locale, MeasureFormat.FormatWidth formatWidth)Create a format from the locale, formatWidth, and format.static MeasureFormatgetInstance(ULocale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)Create a format from the locale, formatWidth, and format.static MeasureFormatgetInstance(Locale locale, MeasureFormat.FormatWidth formatWidth)Create a format from the JDK locale, formatWidth, and format.static MeasureFormatgetInstance(Locale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)Create a format from the JDK locale, formatWidth, and format.ULocalegetLocale()Get the locale of this instance.NumberFormatgetNumberFormat()Get a copy of the number format.static SimplePatternFormattergetRangeFormat(ULocale forLocale, MeasureFormat.FormatWidth width)Deprecated.This API is ICU internal only.static StringgetRangePattern(ULocale forLocale, MeasureFormat.FormatWidth width)Deprecated.This API is ICU internal only.MeasureFormat.FormatWidthgetWidth()Get the format width this instance is using.inthashCode()Returns an integer hash code for this object.MeasureparseObject(String source, ParsePosition pos)Parses text from a string to produce aMeasure.-
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
-
-
-
-
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(Locale locale, MeasureFormat.FormatWidth formatWidth)
Create a format from the JDK locale, formatWidth, and format.- Parameters:
locale- the JDK locale.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(Locale locale, MeasureFormat.FormatWidth formatWidth, NumberFormat format)
Create a format from the JDK locale, formatWidth, and format.- Parameters:
locale- the JDK locale.formatWidth- hints how long formatted strings should be.format- This is defensively copied.- Returns:
- The new MeasureFormat object.
-
format
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
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 aformatMeasuresmethod is preferred over calling this method as they give better performance.- Specified by:
formatin classFormat- Parameters:
obj- must be a Collection extends Measure>, Measure[], or Measure object.toAppendTo- Formatted string appended here.pos- Identifies a field in the formatted text.- Returns:
- the string buffer.
- See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
-
parseObject
public Measure parseObject(String source, ParsePosition pos)
Parses text from a string to produce aMeasure.- Specified by:
parseObjectin classFormat- Parameters:
source- the string to parse.pos- input/output parameter, specifies the start index instringfrom where to start parsing. If parsing is successful, it is updated with the index following the parsed text; on error, the index is unchanged and the error index is set to the index where the error occurred.- Returns:
- the object resulting from the parse or
nullif there is an error. - Throws:
UnsupportedOperationException- Not supported.- See Also:
Format.parseObject(java.lang.String, java.text.ParsePosition)
-
formatMeasures
public final 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.
-
formatMeasureRange
@Deprecated public final String formatMeasureRange(Measure lowValue, Measure highValue)
Deprecated.This API is ICU internal only.Format a range of measures, such as "3.4-5.1 meters". It is the caller’s responsibility to have the appropriate values in appropriate order, and using the appropriate Number values.
Note: If the format doesn’t have enough decimals, or lowValue ≥ highValue, the result will be a degenerate range, like “5-5 meters”.
Currency Units are not yet supported.- Parameters:
lowValue- low value in rangehighValue- high value in range- Returns:
- the formatted string.
-
formatMeasurePerUnit
public StringBuilder formatMeasurePerUnit(Measure measure, MeasureUnit perUnit, StringBuilder appendTo, 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 StringBuilder formatMeasures(StringBuilder appendTo, FieldPosition fieldPosition, 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.fieldPosition- Identifies a field in the formatted text.measures- the measures to format.- Returns:
- appendTo.
- See Also:
formatMeasures(Measure...)
-
equals
public final boolean equals(Object other)
Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.- Overrides:
equalsin classObject- Parameters:
other- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
hashCode
public final int hashCode()
Returns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.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.
-
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(Locale locale)
Return a formatter for CurrencyAmount objects in the given JDK locale.- Parameters:
locale- desired JDK locale- Returns:
- a formatter object
-
getCurrencyFormat
public static MeasureFormat getCurrencyFormat()
Return a formatter for CurrencyAmount objects in the defaultFORMATlocale.- Returns:
- a formatter object
- See Also:
ULocale.Category.FORMAT
-
getRangeFormat
@Deprecated public static SimplePatternFormatter getRangeFormat(ULocale forLocale, MeasureFormat.FormatWidth width)
Deprecated.This API is ICU internal only.Return a simple pattern formatter 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}"
-
getRangePattern
@Deprecated public static String getRangePattern(ULocale forLocale, MeasureFormat.FormatWidth width)
Deprecated.This API is ICU internal only.Return a simple pattern pattern for a range, such as "{0}–{1}" or "{0}~{1}".- Parameters:
forLocale- locale to get the range pattern forwidth- the format width.- Returns:
- range pattern
-
-