Top Description Inners Fields Constructors Methods
tech.units.indriya.format

public abstract Class SimpleUnitFormat

extends AbstractUnitFormat
Class Inheritance
Known Direct Subclasses
tech.units.indriya.format.SimpleUnitFormat.DefaultFormat
Imports
java.io.IOException, java.text.FieldPosition, .ParsePosition, java.util.HashMap, .Map, java.util.stream.Collectors, .Stream, javax.measure.BinaryPrefix, .MeasurementError, .MetricPrefix, .Prefix, .Quantity, .Unit, .UnitConverter, javax.measure.format.MeasurementParseException, .UnitFormat, tech.units.indriya.AbstractUnit, tech.units.indriya.function.AddConverter, .MultiplyConverter, .RationalNumber, tech.units.indriya.unit.AlternateUnit, .AnnotatedUnit, .BaseUnit, .ProductUnit, .TransformedUnit, .Units

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))

Authors
Jean-Marie Dautelle, Werner Keil, Eric Russell, Andi Huber
Since
1.0
Version
2.15, Nov 24, 2024

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SimpleUnitFormat.ASCIIFormat

This class represents the ASCII_INSTANCE format.

pack-priv static class
SimpleUnitFormat.DefaultFormat

This class represents the default (Unicode) format.

private static class
SimpleUnitFormat.Exponent

This class represents an exponent with both a power (numerator) and a root (denominator).

public static enum
SimpleUnitFormat.Flavor

Flavor of this format

Field Summary

Modifier and TypeField and Description
private static final SimpleUnitFormat.ASCIIFormat
ASCII_INSTANCE

Holds the ASCII_INSTANCE format instance.

private static final SimpleUnitFormat.DefaultFormat
DEFAULT_INSTANCE

Holds the default format instance.

private static final String

Constructor Summary

AccessConstructor and Description
protected
SimpleUnitFormat()

Base constructor.

Method Summary

Modifier and TypeMethod and Description
public abstract void
alias(Unit<?>
the unit being aliased.
unit
,
String
the alias attached to this unit.
alias
)

Attaches a system-wide alias to this unit.

public abstract Appendable
format(Unit<?>
the unit to format.
unit
,
Appendable
the appendable destination.
appendable
)

Redeclares javax.measure.format.UnitFormat.format.

Implements abstract tech.units.indriya.format.AbstractUnitFormat.format.

Formats the specified unit.

public final StringBuffer

Returns:

toAppendTo
format
(Object
the unit to format.
unit
,
final StringBuffer
where the text is to be appended
toAppendTo
,
FieldPosition
the field position (not used).
pos
)

Formats an unit and appends the resulting text to a given string buffer (implements java.text.Format).

public static SimpleUnitFormat

Returns:

the default unit format.
getInstance
()

Returns the globally shared unit format instance (used by AbstractUnit.parse() and AbstractUnit.toString()).

public static SimpleUnitFormat

Returns:

the instance for the given Flavor.
getInstance
(SimpleUnitFormat.Flavor flavor)

Returns the SimpleUnitFormat in the desired Flavor

public static SimpleUnitFormat

Returns:

a new instance of the default unit format.
getNewInstance
()

Similar to getInstance(), but returns a new, non-shared unit format instance, instead of a shared singleton instance.

public static SimpleUnitFormat

Returns:

a new instance for the given Flavor.
getNewInstance
(SimpleUnitFormat.Flavor flavor)

Similar to getInstance(Flavor), but returns a new SimpleUnitFormat instance in the desired Flavor, instead of a shared singleton instance.

protected abstract boolean

Returns:

true if the name specified can be used as label or alias for this format;false otherwise.
isValidIdentifier
(String
the identifier to be tested.
name
)

Indicates if the specified name can be used as unit identifier.

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

Redeclares javax.measure.format.UnitFormat.label.

Overrides tech.units.indriya.format.AbstractUnitFormat.label.

Attaches a system-wide label to the specified unit.

public final Unit<?>

Returns:

the corresponding unit or null if the string cannot be parsed.
parseObject
(String
the string source, part of which should be parsed.
source
,
ParsePosition
the cursor position.
pos
)

Parses the text from a string to produce an object (implements java.text.Format).

public abstract Unit<? extends Quantity<Q extends Quantity<Q>>>

Returns:

an Unit parsed from the character sequence.
parseProductUnit
(CharSequence
the CharSequence to parse.
csq
,
ParsePosition
an object holding the parsing index and error position.
pos
)

Parses a sequence of character to produce a unit or a rational product of unit.

public abstract Unit<? extends Quantity<Q extends Quantity<Q>>>

Returns:

an Unit parsed from the character sequence.
parseSingleUnit
(CharSequence
the CharSequence to parse.
csq
,
ParsePosition
an object holding the parsing index and error position.
pos
)

Parses a sequence of character to produce a single unit.

public abstract void
removeAlias(Unit<?>
the unit for which alias shall be removed.
unit
,
String
the alias to be removed.
alias
)

Removes the given system-wide alias (added by alias(Unit, String)) for this unit and keeps the label (added by label(Unit, String))

public abstract void
removeAliases(Unit<?>
the unit for which aliases shall be removed.
unit
)

Removes all system-wide aliases (added by alias(Unit, String)) for this unit and keeps the label (added by label(Unit, String))

public abstract void
removeLabel(Unit<?>
the unit for which label shall be removed.
unit
)

Removes the system-wide label (added by label(Unit, String)) and all system-wide aliases (added by alias(Unit, String)) for this unit.

Inherited from tech.units.indriya.format.AbstractUnitFormat:
formatparseparse