Package org.geotools.measure
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 theUnitFormatinterface for formatting and parsingunits.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.
Unitsare 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 Summary
Constructors Constructor Description BaseUnitFormatter(List<UnitDefinition> unitDefinitions)Create a newBaseUnitFormatterinstance, initialized with provided the unit definitions.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddAlias(Unit<?> unit, String alias)Attaches a system-wide alias to this unit.protected voidaddLabel(Unit<?> unit, String label)Attaches a system-wide label to the specified unit.Appendableformat(Unit<?> unit, Appendable appendable)protected static booleanisUnitIdentifierPart(char ch)protected booleanisValidIdentifier(String name)protected StringnameFor(Unit<?> unit)Unit<?>parse(CharSequence csq)Unit<?>parse(CharSequence csq, ParsePosition cursor)Unit<?>parseObject(String source, ParsePosition pos)Parses the text from a string to produce an object (implementsjava.text.Format).Unit<? extends Quantity>parseProductUnit(CharSequence csq, ParsePosition pos)Parses a sequence of character to produce a unit or a rational product of unit.Unit<? extends Quantity>parseSingleUnit(CharSequence csq, ParsePosition pos)Parses a sequence of character to produce a single unit.protected StringprefixFor(UnitConverter converter)StringtoString()-
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface UnitFormatter
format
-
-
-
-
Constructor Detail
-
BaseUnitFormatter
public BaseUnitFormatter(List<UnitDefinition> unitDefinitions)
Create a newBaseUnitFormatterinstance, initialized with provided the unit definitions.- Parameters:
unitDefinitions- a list of unit definitions used to initialize this new instance
-
-
Method Detail
-
parseObject
public final Unit<?> parseObject(String source, ParsePosition pos) throws MeasurementParseException
Parses the text from a string to produce an object (implementsjava.text.Format).- Parameters:
source- the string source, part of which should be parsed.pos- the cursor position.- Returns:
- the corresponding unit or
nullif 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 aisValidIdentifier(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 aisValidIdentifier(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- theCharSequenceto parse.pos- an object holding the parsing index and error position.- Returns:
- an
Unitparsed 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- theCharSequenceto parse.pos- an object holding the parsing index and error position.- Returns:
- an
Unitparsed 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:
formatin interfaceUnitFormatter- Throws:
IOException
-
parse
public Unit<?> parse(CharSequence csq) throws MeasurementParseException
- Specified by:
parsein interfaceUnitFormatter- Throws:
MeasurementParseException
-
parse
public Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException
- Specified by:
parsein interfaceUnitFormatter- Throws:
IllegalArgumentException
-
-