Class LinearKindOfQuantity
- All Implemented Interfaces:
IDescribable
,IType<IQuantity>
,IConstraint<IQuantity>
,IPersister<IQuantity>
Each kind of quantity has a single "atom unit" which may be used with prefixes. Where applicable, this unit is the SI base unit with any prefix stripped (that is, "kg" is the base unit, but the atom unit is "g"). According to BIPM/VIM above, units that are not formed by a prefix and the atom unit may be permitted, but are treated as off-system units. This means that they cannot have prefixes. (Also, SI only permits a specified set of off-system units to be used with SI, in order to avoid ambiguity. For instance, since there's a unit "Pa", for Pascal, and prefixes "P" for Peta and "E" for Exa, no in-system unit may be designated by "a", and no off-system unit may be designated with "EPa".) To avoid future problems, where we might persist a unit without content type, we should stick to this.
Note that there's a relevant specification for representing units, SI as well as conventional, without ambiguity, in ASCII, with or without case sensitivity, see UCUM. We should strive for "limited conformance" with this specification. That is, it will hardly be worth the trouble of parsing every possible UCUM expression. (There's an Eclipse project for that, UOMo, but it has too much dependencies.) But what we produce when persisting quantities and units should be valid UCUM expressions. That will allow us to export data into a standardized format. And also to switch to a full UCUM implementation, should a suitable one become available.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
static interface
Nested classes/interfaces inherited from class org.openjdk.jmc.common.unit.KindOfQuantity
KindOfQuantity.ExactFormatter<U extends TypedUnit<U>>, KindOfQuantity.VerboseFormatter<U extends TypedUnit<U>>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Collection<LinearUnit>
protected final LinearUnit
protected final String
protected final Collection<LinearUnit>
protected final Map<String,
LinearUnit> protected final Map<Object,
LinearUnit> protected LinearKindOfQuantity.LinearUnitSelector
Fields inherited from class org.openjdk.jmc.common.unit.ContentType
m_identifier
-
Constructor Summary
ConstructorsConstructorDescriptionLinearKindOfQuantity
(String id, String atomUnitId) Create a new linear kind of quantity, with content type identifierid
and sole unit identifieratomUnitId
.LinearKindOfQuantity
(String id, String atomUnitId, P minPrefix, P maxPrefix) Create a new linear kind of quantity, with content type identifierid
and atom unit identifieratomUnitId
, using prefixes in the rangeminPrefix
tomaxPrefix
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addUnit
(LinearUnit unit) Add a common unit (displayed to the user when selecting a unit).protected void
addUnit
(LinearUnit unit, boolean common) Add a unit so that it is available for parsing and content assist, optionally also for direct selection by users.protected ScaleFactor
getBestScaleFactorFor
(long value) getCachedUnit
(Object key) getFormatterResolving
(IRange<IQuantity> range) Get a formatter with sufficient resolution to produce unique strings for both ends ofrange
, and consecutive equally spaced quantities.getLargestExactUnit
(IQuantity quantity) Get the largest unit, if any, in which this quantity can be expressed exactly, typically with an integer.static NumberFormat
getNumberFormat
(boolean interactive) Get a thread local configuredNumberFormat
suitable for display or interactive formatting, or interactive parsing.getPreferredUnit
(IQuantity quantity, double minNumericalValue, double maxNumericalValue) makeCustomUnit
(ITypedQuantity<LinearUnit> quantity) Create a custom unit.makeUnit
(String id, ITypedQuantity<LinearUnit> quantity) Create an off-system unit with no default localized name.makeUnit
(String id, ITypedQuantity<LinearUnit> quantity, String localizedName) parseInteractive
(String interactiveQuantity) Parse an interactive string.parseInteractive
(String interactiveQuantity, Map<String, ? extends LinearUnit> symbolToUnitMap) parsePersisted
(String persistedQuantity) Parse a persisted string.protected String
resolveLocalizedName
(IPrefix<?> prefix) protected String
resolveLocalizedSymbol
(IPrefix<?> prefix) protected void
setDefaultSelector
(LinearKindOfQuantity.LinearUnitSelector unitSelector) snapToBestBetweenHalfAndEqual
(ITypedQuantity<LinearUnit> upperLimit) toString()
Methods inherited from class org.openjdk.jmc.common.unit.KindOfQuantity
combine, format, getAccessor, getAccessorKeys, getAttributes, getFirstBucket, getFirstBucket, getPersister, getRangeFormatter, interactiveFormat, persistableString, validate
Methods inherited from class org.openjdk.jmc.common.unit.ContentType
addFormatter, getDefaultFormatter, getDescription, getFormatter, getFormatters, getIdentifier, getName, hasAttribute, lookupNameFor
-
Field Details
-
atomUnit
-
atomUnitName
-
commonUnits
-
allUnits
-
interactiveSymbolToUnitMap
-
unitSelector
-
unitCache
-
-
Constructor Details
-
LinearKindOfQuantity
Create a new linear kind of quantity, with content type identifierid
and sole unit identifieratomUnitId
. -
LinearKindOfQuantity
Create a new linear kind of quantity, with content type identifierid
and atom unit identifieratomUnitId
, using prefixes in the rangeminPrefix
tomaxPrefix
.
-
-
Method Details
-
getNumberFormat
Get a thread local configuredNumberFormat
suitable for display or interactive formatting, or interactive parsing. The returned formatter mustn't be modified except as by theNumberFormat.parse(String)
andFormat.format(Object)
family of methods.- Parameters:
interactive
- true to format/parse for interactive use, false otherwise- Returns:
- a thread local configured
NumberFormat
-
setDefaultSelector
-
getDeltaKind
- Specified by:
getDeltaKind
in classKindOfQuantity<LinearUnit>
-
getDefaultUnit
- Specified by:
getDefaultUnit
in classKindOfQuantity<LinearUnit>
-
resolveLocalizedName
-
resolveLocalizedSymbol
-
getUnit
-
getUnit
- Overrides:
getUnit
in classKindOfQuantity<LinearUnit>
-
getCachedUnit
-
getBestScaleFactorFor
-
makeUnit
Create an off-system unit with no default localized name.- Parameters:
id
- unit identifier which also is used as the unit symbol
-
makeUnit
-
makeCustomUnit
Create a custom unit. That is, one that normally isn't available for parsing (persisted or interactive). -
toString
- Overrides:
toString
in classContentType<IQuantity>
-
getPreferredUnit
public LinearUnit getPreferredUnit(IQuantity quantity, double minNumericalValue, double maxNumericalValue) - Specified by:
getPreferredUnit
in classKindOfQuantity<LinearUnit>
-
getLargestExactUnit
Description copied from class:KindOfQuantity
Get the largest unit, if any, in which this quantity can be expressed exactly, typically with an integer. If the quantity has zero magnitude (
),quantity.doubleValue()
== 0.0quantity.getUnit()
will be returned. Thus, if you want to find out a maximum common unit for a set of quantities (not recommended), only use the non-zero quantities.Note that this may be a fairly expensive operation, and isn't intended to be used excessively. The only valid use case is for guessing the original unit in which a quantity was expressed, after it has been stored or transmitted using a legacy mechanism with a fixed unit.
- Specified by:
getLargestExactUnit
in classKindOfQuantity<LinearUnit>
- Returns:
- a unit or
null
-
snapToBestBetweenHalfAndEqual
public ITypedQuantity<LinearUnit> snapToBestBetweenHalfAndEqual(ITypedQuantity<LinearUnit> upperLimit) -
getFormatterResolving
Description copied from class:KindOfQuantity
Get a formatter with sufficient resolution to produce unique strings for both ends ofrange
, and consecutive equally spaced quantities. The returned formatter might implementIIncrementalFormatter
, in which case its method can be used to reduce redundant information between adjacent quantities.- Specified by:
getFormatterResolving
in classKindOfQuantity<LinearUnit>
-
parsePersisted
public ITypedQuantity<LinearUnit> parsePersisted(String persistedQuantity) throws QuantityConversionException Description copied from class:KindOfQuantity
Parse a persisted string. Only guaranteed to be able to parse strings produced byIQuantity.persistableString()
for quantities of this kind of quantity. Only use this on persisted strings, never for interactive input.- Specified by:
parsePersisted
in interfaceIConstraint<IQuantity>
- Specified by:
parsePersisted
in classKindOfQuantity<LinearUnit>
- Parameters:
persistedQuantity
- persisted string to parse- Returns:
- a valid quantity for this kind of quantity
- Throws:
QuantityConversionException
- if parsing failed
-
getAllUnits
- Overrides:
getAllUnits
in classKindOfQuantity<LinearUnit>
- Returns:
- all units, suitable for parsing, content assist and similar.
- See Also:
-
getCommonUnits
- Overrides:
getCommonUnits
in classKindOfQuantity<LinearUnit>
- Returns:
- the most common units, suitable to display a unit selection to the user.
- See Also:
-
addUnit
Description copied from class:KindOfQuantity
Add a common unit (displayed to the user when selecting a unit).- Overrides:
addUnit
in classKindOfQuantity<LinearUnit>
-
addUnit
Add a unit so that it is available for parsing and content assist, optionally also for direct selection by users.- Parameters:
common
- whether to include when showing most common units to users
-
parseInteractive
public ITypedQuantity<LinearUnit> parseInteractive(String interactiveQuantity) throws QuantityConversionException Description copied from class:KindOfQuantity
Parse an interactive string. Only guaranteed to be able to parse strings produced byIQuantity.interactiveFormat()
for quantities of this kind of quantity and in the same locale. Only use this for interactive input, never for persisted strings.- Specified by:
parseInteractive
in interfaceIConstraint<IQuantity>
- Specified by:
parseInteractive
in classKindOfQuantity<LinearUnit>
- Parameters:
interactiveQuantity
- interactive string to parse- Returns:
- a valid quantity for this kind of quantity
- Throws:
QuantityConversionException
- if parsing failed
-
parseInteractive
public ITypedQuantity<LinearUnit> parseInteractive(String interactiveQuantity, Map<String, ? extends LinearUnit> symbolToUnitMap) throws QuantityConversionException- Parameters:
interactiveQuantity
- string to parse, interactive stylesymbolToUnitMap
- map of additional symbols to parse into units, or null- Returns:
- the parsed quantity
- Throws:
QuantityConversionException
- containing detailed structured information, if parsing failed
-