Package org.geotools.measure
Class BaseUnitFormatter
Object
BaseUnitFormatter
- All Implemented Interfaces:
UnitFormatter
- Direct Known Subclasses:
EpsgUnitFormat
,EsriUnitFormat
,NetCDFUnitFormat
,UnitFormat
,WktUnitFormat
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 Summary
ConstructorsConstructorDescriptionBaseUnitFormatter
(List<UnitDefinition> unitDefinitions) Create a newBaseUnitFormatter
instance, initialized with provided the unit definitions. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Attaches a system-wide alias to this unit.protected void
Attaches a system-wide label to the specified unit.format
(Unit<?> unit, Appendable appendable) protected static boolean
isUnitIdentifierPart
(char ch) protected boolean
isValidIdentifier
(String name) protected String
nameFor
(Unit<?> unit) Unit<?>
parse
(CharSequence csq) Unit<?>
parse
(CharSequence csq, ParsePosition cursor) final 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 String
prefixFor
(UnitConverter converter) toString()
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface UnitFormatter
format
-
Constructor Details
-
BaseUnitFormatter
Create a newBaseUnitFormatter
instance, initialized with provided the unit definitions.- Parameters:
unitDefinitions
- a list of unit definitions used to initialize this new instance
-
-
Method Details
-
parseObject
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
null
if the string cannot be parsed. - Throws:
MeasurementParseException
-
addLabel
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
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
-
isUnitIdentifierPart
protected static boolean isUnitIdentifierPart(char ch) -
nameFor
-
prefixFor
-
parseSingleUnit
public Unit<? extends Quantity> parseSingleUnit(CharSequence csq, ParsePosition pos) throws MeasurementParseException Parses a sequence of character to produce a single unit.- Parameters:
csq
- theCharSequence
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
- theCharSequence
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
- Specified by:
format
in interfaceUnitFormatter
- Throws:
IOException
-
parse
- Specified by:
parse
in interfaceUnitFormatter
- Throws:
MeasurementParseException
-
parse
- Specified by:
parse
in interfaceUnitFormatter
- Throws:
IllegalArgumentException
-
toString
-