public abstract class SimpleUnitFormat extends AbstractUnitFormat
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))
| Modifier and Type | Class | Description |
|---|---|---|
static class |
SimpleUnitFormat.Flavor |
Flavor of this format
|
| Modifier | Constructor | Description |
|---|---|---|
protected |
SimpleUnitFormat() |
Base constructor.
|
| Modifier and Type | Method | Description |
|---|---|---|
abstract void |
alias(javax.measure.Unit<?> unit,
String alias) |
Attaches a system-wide alias to this unit.
|
StringBuffer |
format(Object unit,
StringBuffer toAppendTo,
FieldPosition pos) |
Formats an unit and appends the resulting text to a given string buffer (implements
java.text.Format). |
abstract Appendable |
format(javax.measure.Unit<?> unit,
Appendable appendable) |
Formats the specified unit.
|
static SimpleUnitFormat |
getInstance() |
Returns the globally shared unit format instance (used by
AbstractUnit.parse() and
AbstractUnit.toString()). |
static SimpleUnitFormat |
getInstance(SimpleUnitFormat.Flavor flavor) |
Returns the
SimpleUnitFormat in the desired SimpleUnitFormat.Flavor |
static SimpleUnitFormat |
getNewInstance() |
Similar to
getInstance(), but returns a new, non-shared unit format instance,
instead of a shared singleton instance. |
static SimpleUnitFormat |
getNewInstance(SimpleUnitFormat.Flavor flavor) |
Similar to
getInstance(Flavor), but returns a new SimpleUnitFormat instance in the desired
SimpleUnitFormat.Flavor, instead of a shared singleton instance. |
protected abstract boolean |
isValidIdentifier(String name) |
Indicates if the specified name can be used as unit identifier.
|
abstract void |
label(javax.measure.Unit<?> unit,
String label) |
Attaches a system-wide label to the specified unit.
|
javax.measure.Unit<?> |
parseObject(String source,
ParsePosition pos) |
Parses the text from a string to produce an object (implements
java.text.Format). |
abstract javax.measure.Unit<? extends javax.measure.Quantity> |
parseProductUnit(CharSequence csq,
ParsePosition pos) |
Parses a sequence of character to produce a unit or a rational product of unit.
|
abstract javax.measure.Unit<? extends javax.measure.Quantity> |
parseSingleUnit(CharSequence csq,
ParsePosition pos) |
Parses a sequence of character to produce a single unit.
|
abstract void |
removeAlias(javax.measure.Unit<?> unit,
String alias) |
Removes the given system-wide alias (added by
alias(Unit, String)) for this unit and keeps the label (added by label(Unit, String)) |
abstract void |
removeAliases(javax.measure.Unit<?> unit) |
Removes all system-wide aliases (added by
alias(Unit, String)) for this unit and keeps the label (added by label(Unit, String)) |
abstract void |
removeLabel(javax.measure.Unit<?> unit) |
Removes the system-wide label (added by
label(Unit, String)) and all system-wide aliases (added by alias(Unit, String)) for this unit. |
format, parse, parseprotected SimpleUnitFormat()
public static SimpleUnitFormat getInstance()
AbstractUnit.parse() and
AbstractUnit.toString()).public static SimpleUnitFormat getInstance(SimpleUnitFormat.Flavor flavor)
SimpleUnitFormat in the desired SimpleUnitFormat.FlavorSimpleUnitFormat.Flavor.public static SimpleUnitFormat getNewInstance()
getInstance(), but returns a new, non-shared unit format instance,
instead of a shared singleton instance.getInstance()public static SimpleUnitFormat getNewInstance(SimpleUnitFormat.Flavor flavor)
getInstance(Flavor), but returns a new SimpleUnitFormat instance in the desired
SimpleUnitFormat.Flavor, instead of a shared singleton instance.SimpleUnitFormat.Flavor.getInstance(Flavor)public abstract Appendable format(javax.measure.Unit<?> unit, Appendable appendable) throws IOException
format in class AbstractUnitFormatunit - the unit to format.appendable - the appendable destination.appendable, with formatted text appended.IOException - if an error occurs.public abstract javax.measure.Unit<? extends javax.measure.Quantity> parseProductUnit(CharSequence csq, ParsePosition pos) throws javax.measure.format.MeasurementParseException
csq - the CharSequence to parse.pos - an object holding the parsing index and error position.Unit parsed from the character sequence.IllegalArgumentException - if the character sequence contains an illegal syntax.javax.measure.format.MeasurementParseExceptionpublic abstract javax.measure.Unit<? extends javax.measure.Quantity> parseSingleUnit(CharSequence csq, ParsePosition pos) throws javax.measure.format.MeasurementParseException
csq - the CharSequence to parse.pos - an object holding the parsing index and error position.Unit parsed from the character sequence.IllegalArgumentException - if the character sequence does not contain a valid unit identifier.javax.measure.format.MeasurementParseExceptionpublic abstract void label(javax.measure.Unit<?> unit, String label)
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, or removeAliases(Unit)) on the given unit, the old label is overwritten for labeling/ purposes, but it remains like an alias (it still works for parsing).
label in class AbstractUnitFormatunit - the unit being labeled.label - the new label for this unit.IllegalArgumentException - if the label is not a isValidIdentifier(String) valid identifier.public abstract void removeLabel(javax.measure.Unit<?> unit)
label(Unit, String)) and all system-wide aliases (added by alias(Unit, String)) for this unit.unit - the unit for which label shall be removed.public abstract void alias(javax.measure.Unit<?> unit, String alias)
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.unit - the unit being aliased.alias - the alias attached to this unit.IllegalArgumentException - if the label is not a isValidIdentifier(String) valid identifier.public abstract void removeAlias(javax.measure.Unit<?> unit, String alias)
alias(Unit, String)) for this unit and keeps the label (added by label(Unit, String))unit - the unit for which alias shall be removed.alias - the alias to be removed.public abstract void removeAliases(javax.measure.Unit<?> unit)
alias(Unit, String)) for this unit and keeps the label (added by label(Unit, String))unit - the unit for which aliases shall be removed.protected abstract boolean isValidIdentifier(String name)
name - the identifier to be tested.true if the name specified can be used as label or alias for this format;false otherwise.public final StringBuffer format(Object unit, StringBuffer toAppendTo, FieldPosition pos)
java.text.Format).unit - the unit to format.toAppendTo - where the text is to be appendedpos - the field position (not used).toAppendTopublic final javax.measure.Unit<?> parseObject(String source, ParsePosition pos) throws javax.measure.format.MeasurementParseException
java.text.Format).source - the string source, part of which should be parsed.pos - the cursor position.null if the string cannot be parsed.javax.measure.format.MeasurementParseExceptionCopyright © 2005–2023 Units of Measurement project. All rights reserved.