JScience v2.0

org.jscience.physics.units
Class UnitFormat

java.lang.Object
  extended by javolution.lang.TextFormat<Unit>
      extended by org.jscience.physics.units.UnitFormat

public abstract class UnitFormat
extends TextFormat<Unit>

This is the abstract base class for all unit formats.

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") == SI.MILLI(SI.CELSIUS)
        Unit.valueOf("kW")  == SI.KILO(SI.WATT)
        Unit.valueOf("ft")  == SI.METER.multiply(0.3048)

Version:
1.0, October 24, 2004
Author:
Jean-Marie Dautelle

Nested Class Summary
 
Nested classes/interfaces inherited from class javolution.lang.TextFormat
TextFormat.Cursor
 
Field Summary
static UnitFormat ASCII
          Holds the ASCII unit format.
static UnitFormat HTML
          Holds the HTML unit format.
static UnitFormat STANDARD
          Holds the standard unit format.
 
Constructor Summary
protected UnitFormat()
          Base constructor.
 
Method Summary
static UnitFormat current()
          Returns the local unit format (default STANDARD).
static boolean isUnitIdentifierPart(char ch)
          Determines if the specified character may be part of a unit identifier.
 java.lang.CharSequence labelFor(Unit unit)
          Returns the label for the specified unit.
 Unit parse(java.lang.CharSequence csq, TextFormat.Cursor pos)
          Parses text to produce an unit.
static void setCurrent(UnitFormat format)
          Sets the local unit format.
 Unit unitFor(java.lang.CharSequence label)
          Returns the unit identified by the specified name (symbol, label or alias).
 
Methods inherited from class javolution.lang.TextFormat
format, format, parse
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STANDARD

public static final UnitFormat STANDARD
Holds the standard unit format. This format is not locale sensitive (international) and uses UTF Latin-1 Supplement (range 0080-00FF) supported by the majority of fonts. For example: cm³·A²/kg


ASCII

public static final UnitFormat ASCII
Holds the ASCII unit format. This format uses characters range 0000-007F exclusively. For example: cm^3*kg^-1*A^2


HTML

public static final UnitFormat HTML
Holds the HTML unit format. This format makes use of HTML tags to represent product units. For example: cm<sup>3</sup>&#183;kg<sup>-1 </sup>&#183;A<sup>2</sup> (cm3·kg-1·A2)

Constructor Detail

UnitFormat

protected UnitFormat()
Base constructor.

Method Detail

current

public static UnitFormat current()
Returns the local unit format (default STANDARD).

Returns:
the unit format for the current thread.

setCurrent

public static void setCurrent(UnitFormat format)
Sets the local unit format.

Parameters:
format - the new local/global unit format.

isUnitIdentifierPart

public static boolean isUnitIdentifierPart(char ch)
Determines if the specified character may be part of a unit identifier. Any letter or symbol which cannot be mistaken for a separator is allowed.

Parameters:
ch - the character to be tested.
Returns:
true if the character may be part of a unit identifier; false otherwise.

parse

public Unit parse(java.lang.CharSequence csq,
                  TextFormat.Cursor pos)
Parses text to produce an unit.

The expected form of the character sequence is: {<name>{<power>{<root>}}}

For examples:

HTML tags are ignored (e.g. <sup>...</sup>).

Escape sequences are considered as separators (e.g. &#183;).

Space characters are ignored.

Any level of parenthesis can be used.

Specified by:
parse in class TextFormat<Unit>
Parameters:
csq - the CharSequence to parse.
pos - an object holding the parsing index and error position.
Returns:
an Unit parsed from the character sequence.
Throws:
java.lang.IllegalArgumentException - if the character sequence contains an illegal syntax.
See Also:
Character.isSpaceChar(char)

labelFor

public java.lang.CharSequence labelFor(Unit unit)
Returns the label for the specified unit. The default behavior of this method (which may be overridden) is first to search the label database.

Parameters:
unit - the unit for which the label is searched for.
Returns:
the database label, the unit symbol, some other representation of the specified unit (e.g. [K+273.15], [m*0.01]) or null if the unit has no intrinsic label (e.g. ProductUnit).
See Also:
unitFor(java.lang.CharSequence)

unitFor

public Unit unitFor(java.lang.CharSequence label)
Returns the unit identified by the specified name (symbol, label or alias).

Parameters:
label - the label, alias, or symbol identifying the unit.
Returns:
the unit identified by the specified label or null if the identification fails.
See Also:
labelFor(org.jscience.physics.units.Unit)

JScience v2.0

Copyright © 2005 JScience.