javax.measure.unit
Class UnitFormat

java.lang.Object
  extended by java.text.Format
      extended by javax.measure.unit.UnitFormat
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
LocalFormat, UCUMFormat

public abstract class UnitFormat
extends java.text.Format

This class provides the interface for formatting and parsing units.

For all SI units, the 20 SI prefixes used to form decimal multiples and sub-multiples of SI units are recognized. NonSI units are directly recognized. For example:

        Unit.valueOf("m°C").equals(SI.MILLI(SI.CELSIUS))
        Unit.valueOf("kW").equals(SI.KILO(SI.WATT))
        Unit.valueOf("ft").equals(SI.METRE.multiply(3048).divide(10000))

Version:
1.0, April 15, 2009
Author:
Jean-Marie Dautelle, Eric Russell
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
java.text.Format.Field
 
Constructor Summary
protected UnitFormat()
          Base constructor.
 
Method Summary
 java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
           
abstract  java.lang.Appendable format(Unit<?> unit, java.lang.Appendable appendable)
          Formats the specified unit.
 java.lang.StringBuilder format(Unit<?> unit, java.lang.StringBuilder dest)
          Convenience method equivalent to format(Unit, Appendable) except it does not raise an IOException.
static UnitFormat getInstance()
          Returns the unit format for the default locale.
static UnitFormat getInstance(java.util.Locale locale)
          Returns the unit format for the specified locale.
static UnitFormat getStandard()
          Returns the standard UCUM unit format.
abstract  Unit<?> parse(java.lang.CharSequence csq, java.text.ParsePosition cursor)
          Parses a portion of the specified CharSequence from the specified position to produce a unit.
 Unit<?> parseObject(java.lang.String source, java.text.ParsePosition pos)
           
 
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnitFormat

protected UnitFormat()
Base constructor.

Method Detail

getInstance

public static UnitFormat getInstance()
Returns the unit format for the default locale.

Returns:
LocalFormat.getInstance()

getInstance

public static UnitFormat getInstance(java.util.Locale locale)
Returns the unit format for the specified locale.

Parameters:
locale - the locale for which the format is returned.
Returns:
LocalFormat.getInstance(java.util.Locale)

getStandard

public static UnitFormat getStandard()
Returns the standard UCUM unit format. This is the format used by Unit.valueOf(CharSequence) and Unit.toString()).

This format uses characters range 0000-007F exclusively and is not locale-sensitive. For example: kg.m/s2

For a description of the UCUM format including BNF, see: The Unified Code for Units of Measure

Returns:
UCUMFormat.getCaseSensitiveInstance()

format

public abstract java.lang.Appendable format(Unit<?> unit,
                                            java.lang.Appendable appendable)
                                     throws java.io.IOException
Formats the specified unit.

Parameters:
unit - the unit to format.
appendable - the appendable destination.
Throws:
java.io.IOException - if an error occurs.

parse

public abstract Unit<?> parse(java.lang.CharSequence csq,
                              java.text.ParsePosition cursor)
                       throws java.lang.IllegalArgumentException
Parses a portion of the specified CharSequence from the specified position to produce a unit. If there is no unit to parse Unit.ONE is returned.

Parameters:
csq - the CharSequence to parse.
cursor - the cursor holding the current parsing index.
Returns:
the unit parsed from the specified character sub-sequence.
Throws:
java.lang.IllegalArgumentException - if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).

format

public final java.lang.StringBuffer format(java.lang.Object obj,
                                           java.lang.StringBuffer toAppendTo,
                                           java.text.FieldPosition pos)
Specified by:
format in class java.text.Format

parseObject

public final Unit<?> parseObject(java.lang.String source,
                                 java.text.ParsePosition pos)
Specified by:
parseObject in class java.text.Format

format

public final java.lang.StringBuilder format(Unit<?> unit,
                                            java.lang.StringBuilder dest)
Convenience method equivalent to format(Unit, Appendable) except it does not raise an IOException.

Parameters:
unit - the unit to format.
dest - the appendable destination.
Returns:
the specified StringBuilder.


Copyright © 2009 JScience. All Rights Reserved.