Class BaseUnitFormatter

Object
BaseUnitFormatter
All Implemented Interfaces:
UnitFormatter
Direct Known Subclasses:
EpsgUnitFormat, EsriUnitFormat, NetCDFUnitFormat, UnitFormat, WktUnitFormat

public class BaseUnitFormatter extends Object implements UnitFormatter
This class implements the UnitFormat interface for formatting and parsing units.

For all SI units, the 24 SI prefixes used to form decimal multiples and sub-multiples are recognized. As well as the 8 binary prefixes.
Units are directly recognized. For example:
UnitFormat format = SimpleUnitFormat.getInstance();
format.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS));
format.parse("kW").equals(MetricPrefix.KILO(Units.WATT));
format.parse("ft").equals(Units.METRE.multiply(0.3048))

Since:
1.0
Author:
Jean-Marie Dautelle, Werner Keil, Eric Russell, Andi Huber
  • Constructor Details

    • BaseUnitFormatter

      public BaseUnitFormatter(List<UnitDefinition> unitDefinitions)
      Create a new BaseUnitFormatter instance, initialized with provided the unit definitions.
      Parameters:
      unitDefinitions - a list of unit definitions used to initialize this new instance
  • Method Details

    • parseObject

      public final Unit<?> parseObject(String source, ParsePosition pos) throws MeasurementParseException
      Parses the text from a string to produce an object (implements java.text.Format ).
      Parameters:
      source - the string source, part of which should be parsed.
      pos - the cursor position.
      Returns:
      the corresponding unit or null if the string cannot be parsed.
      Throws:
      MeasurementParseException
    • addLabel

      protected void addLabel(Unit<?> unit, String label)
      Attaches a system-wide label to the specified unit. For example: SimpleUnitFormat.getInstance().label(DAY.multiply(365), "year"); SimpleUnitFormat.getInstance().label(METER.multiply(0.3048), "ft"); If the specified label is already associated to a unit the previous association is discarded or ignored. The old label is overwritten for labeling/ purposes, but it remains like an alias (it still works for parsing)
      Parameters:
      unit - the unit being labeled.
      label - the new label for this unit.
      Throws:
      IllegalArgumentException - if the label is not a isValidIdentifier(String) valid identifier.
    • addAlias

      protected void addAlias(Unit<?> unit, String alias)
      Attaches a system-wide alias to this unit. Multiple aliases may be attached to the same unit. Aliases are used during parsing to recognize different variants of the same unit. For example: SimpleUnitFormat.getInstance().alias(METER.multiply(0.3048), "foot"); SimpleUnitFormat.getInstance().alias(METER.multiply(0.3048), "feet"); SimpleUnitFormat.getInstance().alias(METER, "meter"); SimpleUnitFormat.getInstance().alias(METER, "metre"); If the specified label is already associated to an unit the previous association is discarded or ignored.
      Parameters:
      unit - the unit being aliased.
      alias - the alias attached to this unit.
      Throws:
      IllegalArgumentException - if the label is not a isValidIdentifier(String) valid identifier.
    • isValidIdentifier

      protected boolean isValidIdentifier(String name)
    • isUnitIdentifierPart

      protected static boolean isUnitIdentifierPart(char ch)
    • nameFor

      protected String nameFor(Unit<?> unit)
    • prefixFor

      protected String prefixFor(UnitConverter converter)
    • parseSingleUnit

      public Unit<? extends Quantity> parseSingleUnit(CharSequence csq, ParsePosition pos) throws MeasurementParseException
      Parses a sequence of character to produce a single 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:
      IllegalArgumentException - if the character sequence does not contain a valid unit identifier.
      MeasurementParseException
    • parseProductUnit

      public Unit<? extends Quantity> parseProductUnit(CharSequence csq, ParsePosition pos) throws MeasurementParseException
      Parses a sequence of character to produce a unit or a rational product of 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:
      IllegalArgumentException - if the character sequence contains an illegal syntax.
      MeasurementParseException
    • format

      public Appendable format(Unit<?> unit, Appendable appendable) throws IOException
      Specified by:
      format in interface UnitFormatter
      Throws:
      IOException
    • parse

      public Unit<?> parse(CharSequence csq) throws MeasurementParseException
      Specified by:
      parse in interface UnitFormatter
      Throws:
      MeasurementParseException
    • parse

      public Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException
      Specified by:
      parse in interface UnitFormatter
      Throws:
      IllegalArgumentException
    • toString

      public String toString()
      Overrides:
      toString in class Object