Class SimpleUnitFormat
- java.lang.Object
-
- tech.units.indriya.format.AbstractUnitFormat
-
- tech.units.indriya.format.SimpleUnitFormat
-
- All Implemented Interfaces:
UnitFormat
public abstract class SimpleUnitFormat extends AbstractUnitFormat
This class implements the
UnitFormatinterface 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
- Version:
- 2.15, Nov 24, 2024
- Author:
- Jean-Marie Dautelle, Werner Keil, Eric Russell, Andi Huber
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSimpleUnitFormat.FlavorFlavor of this format
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleUnitFormat()Base constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidalias(Unit<?> unit, String alias)Attaches a system-wide alias to this unit.StringBufferformat(Object unit, StringBuffer toAppendTo, FieldPosition pos)Formats an unit and appends the resulting text to a given string buffer (implementsjava.text.Format).abstract Appendableformat(Unit<?> unit, Appendable appendable)Formats the specified unit.static SimpleUnitFormatgetInstance()Returns the globally shared unit format instance (used byAbstractUnit.parse()andAbstractUnit.toString()).static SimpleUnitFormatgetInstance(SimpleUnitFormat.Flavor flavor)Returns theSimpleUnitFormatin the desiredSimpleUnitFormat.Flavorstatic SimpleUnitFormatgetNewInstance()Similar togetInstance(), but returns a new, non-shared unit format instance, instead of a shared singleton instance.static SimpleUnitFormatgetNewInstance(SimpleUnitFormat.Flavor flavor)Similar togetInstance(Flavor), but returns a newSimpleUnitFormatinstance in the desiredSimpleUnitFormat.Flavor, instead of a shared singleton instance.protected abstract booleanisValidIdentifier(String name)Indicates if the specified name can be used as unit identifier.abstract voidlabel(Unit<?> unit, String label)Attaches a system-wide label to the specified unit.Unit<?>parseObject(String source, ParsePosition pos)Parses the text from a string to produce an object (implementsjava.text.Format).abstract Unit<? extends Quantity>parseProductUnit(CharSequence csq, ParsePosition pos)Parses a sequence of character to produce a unit or a rational product of unit.abstract Unit<? extends Quantity>parseSingleUnit(CharSequence csq, ParsePosition pos)Parses a sequence of character to produce a single unit.abstract voidremoveAlias(Unit<?> unit, String alias)Removes the given system-wide alias (added byalias(Unit, String)) for this unit and keeps the label (added bylabel(Unit, String))abstract voidremoveAliases(Unit<?> unit)Removes all system-wide aliases (added byalias(Unit, String)) for this unit and keeps the label (added bylabel(Unit, String))abstract voidremoveLabel(Unit<?> unit)Removes the system-wide label (added bylabel(Unit, String)) and all system-wide aliases (added byalias(Unit, String)) for this unit.-
Methods inherited from class tech.units.indriya.format.AbstractUnitFormat
format, parse, parse
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface javax.measure.format.UnitFormat
isLocaleSensitive, parse
-
-
-
-
Constructor Detail
-
SimpleUnitFormat
protected SimpleUnitFormat()
Base constructor.
-
-
Method Detail
-
getInstance
public static SimpleUnitFormat getInstance()
Returns the globally shared unit format instance (used byAbstractUnit.parse()andAbstractUnit.toString()).- Returns:
- the default unit format.
-
getInstance
public static SimpleUnitFormat getInstance(SimpleUnitFormat.Flavor flavor)
Returns theSimpleUnitFormatin the desiredSimpleUnitFormat.Flavor- Returns:
- the instance for the given
SimpleUnitFormat.Flavor.
-
getNewInstance
public static SimpleUnitFormat getNewInstance()
Similar togetInstance(), but returns a new, non-shared unit format instance, instead of a shared singleton instance.- Returns:
- a new instance of the default unit format.
- Since:
- 2.7
- See Also:
getInstance()
-
getNewInstance
public static SimpleUnitFormat getNewInstance(SimpleUnitFormat.Flavor flavor)
Similar togetInstance(Flavor), but returns a newSimpleUnitFormatinstance in the desiredSimpleUnitFormat.Flavor, instead of a shared singleton instance.- Returns:
- a new instance for the given
SimpleUnitFormat.Flavor. - Since:
- 2.7
- See Also:
getInstance(Flavor)
-
format
public abstract Appendable format(Unit<?> unit, Appendable appendable) throws IOException
Formats the specified unit.- Specified by:
formatin interfaceUnitFormat- Specified by:
formatin classAbstractUnitFormat- Parameters:
unit- the unit to format.appendable- the appendable destination.- Returns:
- The appendable destination passed in as
appendable, with formatted text appended. - Throws:
IOException- if an error occurs.
-
parseProductUnit
public abstract 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
-
parseSingleUnit
public abstract 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
-
label
public abstract void label(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 an unit the previous association is discarded or ignored.If you set a different label without calling
removeLabel(Unit)),removeAlias(Unit, String)), using the old label, orremoveAliases(Unit)) on the given unit, the old label is overwritten for labeling/ purposes, but it remains like an alias (it still works for parsing).- Specified by:
labelin interfaceUnitFormat- Overrides:
labelin classAbstractUnitFormat- Parameters:
unit- the unit being labeled.label- the new label for this unit.- Throws:
IllegalArgumentException- if the label is not aisValidIdentifier(String)valid identifier.
-
removeLabel
public abstract void removeLabel(Unit<?> unit)
Removes the system-wide label (added bylabel(Unit, String)) and all system-wide aliases (added byalias(Unit, String)) for this unit.- Parameters:
unit- the unit for which label shall be removed.
-
alias
public abstract void alias(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(METRE.multiply(0.3048), "feet"); SimpleUnitFormat.getInstance().alias(METER, "meter"); SimpleUnitFormat.getInstance().alias(METRE, "meter");If the specified alias is already associated to a unit or applied as a label, the association is replaced by the new one.- Parameters:
unit- the unit being aliased.alias- the alias attached to this unit.- Throws:
IllegalArgumentException- if the label is not aisValidIdentifier(String)valid identifier.
-
removeAlias
public abstract void removeAlias(Unit<?> unit, String alias)
Removes the given system-wide alias (added byalias(Unit, String)) for this unit and keeps the label (added bylabel(Unit, String))- Parameters:
unit- the unit for which alias shall be removed.alias- the alias to be removed.
-
removeAliases
public abstract void removeAliases(Unit<?> unit)
Removes all system-wide aliases (added byalias(Unit, String)) for this unit and keeps the label (added bylabel(Unit, String))- Parameters:
unit- the unit for which aliases shall be removed.
-
isValidIdentifier
protected abstract boolean isValidIdentifier(String name)
Indicates if the specified name can be used as unit identifier.- Parameters:
name- the identifier to be tested.- Returns:
trueif the name specified can be used as label or alias for this format;falseotherwise.
-
format
public final StringBuffer format(Object unit, StringBuffer toAppendTo, FieldPosition pos)
Formats an unit and appends the resulting text to a given string buffer (implementsjava.text.Format).- Parameters:
unit- the unit to format.toAppendTo- where the text is to be appendedpos- the field position (not used).- Returns:
toAppendTo
-
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
-
-