Top Description Constructors Methods
tech.units.indriya.format

public abstract Class AbstractUnitFormat

extends Object
implements UnitFormat
Class Inheritance
All Implemented Interfaces
javax.measure.format.UnitFormat
Known Direct Subclasses
tech.units.indriya.format.EBNFUnitFormat, tech.units.indriya.format.LocalUnitFormat, tech.units.indriya.format.SimpleUnitFormat
Imports
java.io.IOException, java.text.ParsePosition, javax.measure.MeasurementException, .Unit, javax.measure.format.UnitFormat, tech.units.indriya.AbstractUnit

This class provides the interface for formatting and parsing units.

For all metric units, the 20 SI prefixes used to form decimal multiples and sub-multiples of SI units are recognized.
As well as the 8 Binary prefixes.
For example: AbstractUnit.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS)) AbstractUnit.parse("kW").equals(MetricPrefix.KILO(Units.WATT))

Authors
Jean-Marie Dautelle, Werner Keil
Since
1.0
Version
2.0, $Date: 2020-03-02 $

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract Appendable

Returns:

The appendable destination passed in as appendable, with formatted text appended.
format
(Unit<?>
the unit to format.
unit
,
Appendable
the appendable destination.
appendable
)

Redeclares javax.measure.format.UnitFormat.format.

Formats the specified unit.

public final String

Returns:

Formatted string.
format
(Unit<?>
the Unit to format, not null
unit
)

Implements javax.measure.format.UnitFormat.format.

Formats an object to produce a string.

private final StringBuilder

Returns:

the specified StringBuilder.
format
(AbstractUnit<?>
the unit to format.
unit
,
StringBuilder
the appendable destination.
dest
)

Convenience method equivalent to format(AbstractUnit, Appendable) except it does not raise an IOException.

public void
label(Unit<?>
the unit being labeled.
unit
,
String
the new label for this unit.
label
)

Implements javax.measure.format.UnitFormat.label.

Attaches a system-wide label to the specified Unit.

public abstract Unit<?>

Returns:

the unit parsed from the specified character sub-sequence.
parse
(CharSequence
the CharSequence to parse.
csq
,
ParsePosition
the cursor holding the current parsing index.
cursor
)

Redeclares javax.measure.format.UnitFormat.parse.

Parses a portion of the specified CharSequence from the specified position to produce a unit.

protected abstract Unit<?>

Returns:

the unit parsed from the specified character sub-sequence.
parse
(CharSequence
the CharSequence to parse.
csq
,
int
the current parsing index.
index
)

Parses a portion of the specified CharSequence from the specified position to produce a unit.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

AbstractUnitFormatback to summary
public AbstractUnitFormat()

Method Detail

formatback to summary
public abstract Appendable format(Unit<?> unit, Appendable appendable) throws IOException

Redeclares javax.measure.format.UnitFormat.format.

Formats the specified unit.

Parameters
unit:Unit<?>

the unit to format.

appendable:Appendable

the appendable destination.

Returns:Appendable

The appendable destination passed in as appendable, with formatted text appended.

Exceptions
IOException:
if an error occurs.
formatback to summary
public final String format(Unit<?> unit)

Implements javax.measure.format.UnitFormat.format.

Formats an object to produce a string. This is equivalent to

format(unit, new StringBuilder()).toString();
Parameters
unit:Unit<?>

Doc from javax.measure.format.UnitFormat.format.

the Unit to format, not null

Returns:String

Formatted string.

Exceptions
IllegalArgumentException:
if the Format cannot format the given object
formatback to summary
private final StringBuilder format(AbstractUnit<?> unit, StringBuilder dest)

Convenience method equivalent to format(AbstractUnit, Appendable) except it does not raise an IOException.

Parameters
unit:AbstractUnit<?>

the unit to format.

dest:StringBuilder

the appendable destination.

Returns:StringBuilder

the specified StringBuilder.

labelback to summary
public void label(Unit<?> unit, String label)

Implements javax.measure.format.UnitFormat.label.

Doc from javax.measure.format.UnitFormat.label.

Attaches a system-wide label to the specified Unit.

This method overrides the previous unit's label (e.g. label from unit database or unit system) as units may only have one label. Depending on the UnitFormat implementation, this call may be ignored if the particular unit already has a label.

If a UnitFormat implementation is explicitly immutable, similar to e.g. the result of Collections.unmodifiableList(), then an UnsupportedOperationException may be thrown upon this call.

Since UnitFormat implementations often apply the Singleton pattern, system-wide means, the label applies to every instance of UnitFormatA implementing UnitFormat in this case, but not every instance of UnitFormatB or UnitFormatC both also implementing UnitFormat. If a UnitFormat #isLocaleSensitive() it is up to the implementation, whether the label is ignored, applied in a local-neutral manner (in addition to its local-sensitive information) or locale-specific.

Parameters
unit:Unit<?>

the unit being labeled.

label:String

the new label for this unit.

Annotations
@Override
parseback to summary
public abstract Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException

Redeclares javax.measure.format.UnitFormat.parse.

Parses a portion of the specified CharSequence from the specified position to produce a unit. If there is no unit to parse AbstractUnit#ONE is returned.

Parameters
csq:CharSequence

the CharSequence to parse.

cursor:ParsePosition

the cursor holding the current parsing index.

Returns:Unit<?>

the unit parsed from the specified character sub-sequence.

Exceptions
IllegalArgumentException:
if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
parseback to summary
protected abstract Unit<?> parse(CharSequence csq, int index) throws IllegalArgumentException

Parses a portion of the specified CharSequence from the specified position to produce a unit. If there is no unit to parse AbstractUnit#ONE is returned.

Parameters
csq:CharSequence

the CharSequence to parse.

index:int

the current parsing index.

Returns:Unit<?>

the unit parsed from the specified character sub-sequence.

Exceptions
IllegalArgumentException:
if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).