public abstract class SimpleUnitFormat extends AbstractUnitFormat
This class implements the UnitFormat interface for formatting and parsing units.
For all SI units, the 20 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 and Description |
|---|---|
protected static class |
SimpleUnitFormat.ASCIIFormat
This class represents the ASCII format.
|
protected static class |
SimpleUnitFormat.DefaultFormat
This class represents the standard format.
|
static class |
SimpleUnitFormat.Flavor
Flavor of this format
|
| Modifier | Constructor and Description |
|---|---|
protected |
SimpleUnitFormat()
Base constructor.
|
| Modifier and Type | Method and 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 unit format for the default locale (format used by
AbstractUnit.parse(CharSequence) and
Unit.toString()). |
static SimpleUnitFormat |
getInstance(SimpleUnitFormat.Flavor flavor)
Returns the
SimpleUnitFormat in the desired SimpleUnitFormat.Flavor |
protected static SimpleUnitFormat.ASCIIFormat |
initASCIIFormat(SimpleUnitFormat.ASCIIFormat asciiFormat) |
protected static SimpleUnitFormat.DefaultFormat |
initDefaultFormat(SimpleUnitFormat.DefaultFormat defaultFormat) |
protected static boolean |
isAllASCII(String input)
to check if a string only contains US-ASCII characters
|
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.
|
format, parse, parseprotected SimpleUnitFormat()
public static SimpleUnitFormat getInstance()
AbstractUnit.parse(CharSequence) and
Unit.toString()).public static SimpleUnitFormat getInstance(SimpleUnitFormat.Flavor flavor)
SimpleUnitFormat in the desired SimpleUnitFormat.FlavorSimpleUnitFormat.Flavor.public abstract Appendable format(javax.measure.Unit<?> unit, Appendable appendable) throws IOException
format in interface javax.measure.format.UnitFormatformat 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.label in interface javax.measure.format.UnitFormatlabel 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 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.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.MeasurementParseExceptionprotected static boolean isAllASCII(String input)
protected static SimpleUnitFormat.DefaultFormat initDefaultFormat(SimpleUnitFormat.DefaultFormat defaultFormat)
protected static SimpleUnitFormat.ASCIIFormat initASCIIFormat(SimpleUnitFormat.ASCIIFormat asciiFormat)
Copyright © 2005–2021 Units of Measurement project. All rights reserved.