javax.measure
Class MeasureFormat

java.lang.Object
  extended by java.text.Format
      extended by javax.measure.MeasureFormat
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public abstract class MeasureFormat
extends java.text.Format

This class provides the interface for formatting and parsing measurements.

Instances of this class should be able to format measurements stated in CompoundUnit. See formatCompound(...).

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

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
java.text.Format.Field
 
Constructor Summary
MeasureFormat()
           
 
Method Summary
abstract  java.lang.Appendable format(Measure<?> measure, java.lang.Appendable dest)
          Formats the specified measure into an Appendable.
 java.lang.StringBuilder format(Measure<?> measure, java.lang.StringBuilder dest)
          Convenience method equivalent to format(Measure, Appendable) except it does not raise an IOException.
 java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
           
protected  java.lang.Appendable formatCompound(double value, CompoundUnit<?> unit, java.lang.Appendable dest)
          Formats the specified value using CompoundUnit compound units}.
static MeasureFormat getInstance()
          Returns the measure format for the default locale.
static MeasureFormat getInstance(java.text.NumberFormat numberFormat, UnitFormat unitFormat)
          Returns the measure format using the specified number format and unit format (the number and unit are separated by one space).
static MeasureFormat getStandard()
          Returns the culture invariant format based upon BigDecimal canonical format and the standard unit format.
abstract  Measure<?> parse(java.lang.CharSequence csq, java.text.ParsePosition cursor)
          Parses a portion of the specified CharSequence from the specified position to produce an object.
 Measure<?> 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

MeasureFormat

public MeasureFormat()
Method Detail

getInstance

public static MeasureFormat getInstance()
Returns the measure format for the default locale. The default format assumes the measure is composed of a decimal number and a Unit separated by whitespace(s).

Returns:
MeasureFormat.getInstance(NumberFormat.getInstance(), UnitFormat.getInstance())

getInstance

public static MeasureFormat getInstance(java.text.NumberFormat numberFormat,
                                        UnitFormat unitFormat)
Returns the measure format using the specified number format and unit format (the number and unit are separated by one space).

Parameters:
numberFormat - the number format.
unitFormat - the unit format.
Returns:
the corresponding format.

getStandard

public static MeasureFormat getStandard()
Returns the culture invariant format based upon BigDecimal canonical format and the standard unit format. This format is not locale-sensitive and can be used for unambiguous electronic communication of quantities together with their units without loss of information. For example: "1.23456789 kg.m/s2" returns Measure.valueOf(new BigDecimal("1.23456789"), Unit.valueOf("kg.m/s2")));

Returns:
the standard measure format.

format

public abstract java.lang.Appendable format(Measure<?> measure,
                                            java.lang.Appendable dest)
                                     throws java.io.IOException
Formats the specified measure into an Appendable.

Parameters:
measure - the measure to format.
dest - the appendable destination.
Returns:
the specified Appendable.
Throws:
java.io.IOException - if an I/O exception occurs.

parse

public abstract Measure<?> 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 an object. If parsing succeeds, then the index of the cursor argument is updated to the index after the last character used.

Parameters:
csq - the CharSequence to parse.
cursor - the cursor holding the current parsing index.
Returns:
the object 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).

formatCompound

protected java.lang.Appendable formatCompound(double value,
                                              CompoundUnit<?> unit,
                                              java.lang.Appendable dest)
                                       throws java.io.IOException
Formats the specified value using CompoundUnit compound units}. The default implementation is locale sensitive and does not use space to separate units. For example:
     Unit<Length> FOOT_INCH = FOOT.compound(INCH);
     Measure<Length> height = Measure.valueOf(1.81, METER);
     System.out.println(height.to(FOOT_INCH));
 
     > 5ft11,26in // French Local
 
     Unit<Angle> DMS = DEGREE_ANGLE.compound(MINUTE_ANGLE).compound(SECOND_ANGLE);
     Measure<Angle> rotation = Measure.valueOf(35.857497, DEGREE_ANGLE);
     System.out.println(rotation.to(DMS));
 
     > 35°51'26,989" // French Local 
 

Parameters:
value - the value to format using compound units.
unit - the compound unit.
dest - the appendable destination.
Returns:
the specified Appendable.
Throws:
java.io.IOException - if an I/O exception occurs.

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 Measure<?> parseObject(java.lang.String source,
                                    java.text.ParsePosition pos)
Specified by:
parseObject in class java.text.Format

format

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

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


Copyright © 2009 JScience. All Rights Reserved.