Package tech.units.indriya.format
Class SimpleQuantityFormat
- java.lang.Object
-
- java.text.Format
-
- tech.units.indriya.format.AbstractQuantityFormat
-
- tech.units.indriya.format.SimpleQuantityFormat
-
- All Implemented Interfaces:
Serializable,Cloneable,QuantityFormat,tech.uom.lib.common.function.Parser<CharSequence,Quantity>
public class SimpleQuantityFormat extends AbstractQuantityFormat
A simple implementation of QuantityFormat
The following pattern letters are defined:
Pattern letters are usually repeated, as their number determines the exact presentation:Letter Quantity Component Presentation Examples nNumeric value Number 27uUnit Text m~Mixed radix Text 1 m; 27cm- Text:
For formatting, if the number of pattern letters is 4 or more,
the full form is used; otherwise a short or abbreviated form
is used if available.
For parsing, both forms are accepted, independent of the number
of pattern letters.
- Number:
For formatting, the number of pattern letters is the minimum
number of digits, and shorter numbers are zero-padded to this amount.
For parsing, the number of pattern letters is ignored unless
it's needed to separate two adjacent fields.
- Mixed Radix:
The Mixed radix marker
"~"is followed by a character sequence acting as mixed radix delimiter. This character sequence must not contain"~"itself or any numeric values.
- Since:
- 2.0
- Version:
- 2.2, Jan 7, 2024
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.text.Format
Format.Field
-
-
Field Summary
-
Fields inherited from class tech.units.indriya.format.AbstractQuantityFormat
DEFAULT_DELIMITER
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleQuantityFormat()Constructs aSimpleQuantityFormatusing the default pattern.SimpleQuantityFormat(String pattern)Constructs aSimpleQuantityFormatusing the given pattern.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Appendableformat(Quantity<?> quantity, Appendable dest)Formats the specified quantity into anAppendable.protected StringBufferformatMixed(MixedQuantity<?> mixed, StringBuffer dest)Convenience method equivalent to#format(MixedQuantity, Appendable)except it does not raise an IOException.static SimpleQuantityFormatgetInstance()Returns the quantity format for the default locale.static SimpleQuantityFormatgetInstance(String pattern)Returns aSimpleQuantityFormatusing the given pattern.StringgetPattern()Returns the pattern of this format.Quantity<?>parse(CharSequence csq)Parses a portion of the specifiedCharSequencefrom the specified position to produce an object.protected Quantity<?>parse(CharSequence csq, int index)Parses a portion of the specifiedCharSequencefrom the specified position to produce an object.Quantity<?>parse(CharSequence csq, ParsePosition cursor)Parses a portion of the specifiedCharSequencefrom the specified position to produce an object.StringtoString()-
Methods inherited from class tech.units.indriya.format.AbstractQuantityFormat
format, format, format, parseObject
-
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface javax.measure.format.QuantityFormat
isLocaleSensitive
-
-
-
-
Constructor Detail
-
SimpleQuantityFormat
public SimpleQuantityFormat(String pattern)
Constructs aSimpleQuantityFormatusing the given pattern.- Parameters:
pattern- the pattern describing the quantity and unit format- Throws:
NullPointerException- if the given pattern is nullIllegalArgumentException- if the given pattern is invalid
-
SimpleQuantityFormat
protected SimpleQuantityFormat()
Constructs aSimpleQuantityFormatusing the default pattern. For full coverage, use the factory methods.
-
-
Method Detail
-
format
public Appendable format(Quantity<?> quantity, Appendable dest) throws IOException
Description copied from class:AbstractQuantityFormatFormats the specified quantity into anAppendable.- Specified by:
formatin interfaceQuantityFormat- Specified by:
formatin classAbstractQuantityFormat- Parameters:
quantity- the quantity to format.dest- the appendable destination.- Returns:
- the specified
Appendable. - Throws:
IOException- if an I/O exception occurs.
-
parse
public Quantity<?> parse(CharSequence csq, ParsePosition cursor) throws MeasurementParseException
Description copied from class:AbstractQuantityFormatParses a portion of the specifiedCharSequencefrom the specified position to produce an object. If parsing succeeds, then the index of thecursorargument is updated to the index after the last character used.- Specified by:
parsein interfaceQuantityFormat- Specified by:
parsein classAbstractQuantityFormat- Parameters:
csq- theCharSequenceto parse.cursor- the cursor holding the current parsing index.- Returns:
- the object parsed from the specified character sub-sequence.
- Throws:
MeasurementParseException
-
parse
protected Quantity<?> parse(CharSequence csq, int index) throws MeasurementParseException
Description copied from class:AbstractQuantityFormatParses a portion of the specifiedCharSequencefrom the specified position to produce an object. If parsing succeeds, then the index of thecursorargument is updated to the index after the last character used.- Specified by:
parsein classAbstractQuantityFormat- Parameters:
csq- theCharSequenceto parse.index- the current parsing index.- Returns:
- the object parsed from the specified character sub-sequence.
- Throws:
MeasurementParseException
-
parse
public Quantity<?> parse(CharSequence csq) throws MeasurementParseException
Description copied from class:AbstractQuantityFormatParses a portion of the specifiedCharSequencefrom the specified position to produce an object. If parsing succeeds, then the index of thecursorargument is updated to the index after the last character used.- Specified by:
parsein interfacetech.uom.lib.common.function.Parser<CharSequence,Quantity>- Specified by:
parsein interfaceQuantityFormat- Specified by:
parsein classAbstractQuantityFormat- Parameters:
csq- theCharSequenceto parse.- Returns:
- the object parsed from the specified character sub-sequence.
- Throws:
MeasurementParseException
-
getInstance
public static SimpleQuantityFormat getInstance()
Returns the quantity format for the default locale. The default format assumes the quantity is composed of a decimal number and aUnitseparated by whitespace(s).- Returns:
- a default
SimpleQuantityFormatinstance.
-
getInstance
public static SimpleQuantityFormat getInstance(String pattern)
Returns aSimpleQuantityFormatusing the given pattern.- Parameters:
pattern- the pattern describing the quantity and unit format- Returns:
SimpleQuantityFormat.getInstance(a pattern)
-
getPattern
public String getPattern()
Returns the pattern of this format.- Returns:
- a
pattern
-
formatMixed
protected StringBuffer formatMixed(MixedQuantity<?> mixed, StringBuffer dest)
Description copied from class:AbstractQuantityFormatConvenience method equivalent to#format(MixedQuantity, Appendable)except it does not raise an IOException.- Specified by:
formatMixedin classAbstractQuantityFormat- Parameters:
mixed- the mixed quantity to format.dest- the appendable destination.- Returns:
- the specified
StringBuilder.
-
-