public class LinearKindOfQuantity extends KindOfQuantity<LinearUnit>
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.
Modifier and Type | Class and Description |
---|---|
static class |
LinearKindOfQuantity.AutoFormatter |
static class |
LinearKindOfQuantity.DualUnitFormatter |
static interface |
LinearKindOfQuantity.LinearUnitSelector |
KindOfQuantity.ExactFormatter<U extends TypedUnit<U>>, KindOfQuantity.VerboseFormatter<U extends TypedUnit<U>>
Modifier and Type | Field and Description |
---|---|
protected Collection<LinearUnit> |
allUnits |
protected LinearUnit |
atomUnit |
protected String |
atomUnitName |
protected Collection<LinearUnit> |
commonUnits |
protected Map<String,LinearUnit> |
interactiveSymbolToUnitMap |
protected Map<Object,LinearUnit> |
unitCache |
protected LinearKindOfQuantity.LinearUnitSelector |
unitSelector |
m_identifier
Constructor and Description |
---|
LinearKindOfQuantity(String id,
String atomUnitId)
Create a new linear kind of quantity, with content type identifier
id and sole unit
identifier atomUnitId . |
LinearKindOfQuantity(String id,
String atomUnitId,
P minPrefix,
P maxPrefix)
Create a new linear kind of quantity, with content type identifier
id and atom unit
identifier atomUnitId , using prefixes in the range minPrefix to
maxPrefix . |
Modifier and Type | Method and Description |
---|---|
protected 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.
|
Collection<LinearUnit> |
getAllUnits() |
protected ScaleFactor |
getBestScaleFactorFor(long value) |
LinearUnit |
getCachedUnit(Object key) |
Collection<LinearUnit> |
getCommonUnits() |
LinearUnit |
getDefaultUnit() |
KindOfQuantity<LinearUnit> |
getDeltaKind() |
IFormatter<IQuantity> |
getFormatterResolving(IRange<IQuantity> range)
Get a formatter with sufficient resolution to produce unique strings for both ends of
range , and consecutive equally spaced quantities. |
LinearUnit |
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 configured
NumberFormat suitable for display or interactive
formatting, or interactive parsing. |
LinearUnit |
getPreferredUnit(IQuantity quantity,
double minNumericalValue,
double maxNumericalValue) |
LinearUnit |
getUnit(IPrefix<?> prefix) |
LinearUnit |
getUnit(String id) |
LinearUnit |
makeCustomUnit(ITypedQuantity<LinearUnit> quantity)
Create a custom unit.
|
LinearUnit |
makeUnit(String id,
ITypedQuantity<LinearUnit> quantity)
Create an off-system unit with no default localized name.
|
LinearUnit |
makeUnit(String id,
ITypedQuantity<LinearUnit> quantity,
String localizedName) |
ITypedQuantity<LinearUnit> |
parseInteractive(String interactiveQuantity)
Parse an interactive string.
|
ITypedQuantity<LinearUnit> |
parseInteractive(String interactiveQuantity,
Map<String,? extends LinearUnit> symbolToUnitMap) |
ITypedQuantity<LinearUnit> |
parsePersisted(String persistedQuantity)
Parse a persisted string.
|
protected String |
resolveLocalizedName(IPrefix<?> prefix) |
protected String |
resolveLocalizedSymbol(IPrefix<?> prefix) |
protected void |
setDefaultSelector(LinearKindOfQuantity.LinearUnitSelector unitSelector) |
ITypedQuantity<LinearUnit> |
snapToBestBetweenHalfAndEqual(ITypedQuantity<LinearUnit> upperLimit) |
String |
toString() |
combine, format, getAccessor, getAccessorKeys, getAttributes, getFirstBucket, getFirstBucket, getPersister, getRangeFormatter, interactiveFormat, persistableString, validate
addFormatter, getDefaultFormatter, getDescription, getFormatter, getFormatters, getIdentifier, getName, hasAttribute, lookupNameFor
protected final LinearUnit atomUnit
protected final String atomUnitName
protected final Collection<LinearUnit> commonUnits
protected final Collection<LinearUnit> allUnits
protected final Map<String,LinearUnit> interactiveSymbolToUnitMap
protected LinearKindOfQuantity.LinearUnitSelector unitSelector
protected final Map<Object,LinearUnit> unitCache
public LinearKindOfQuantity(String id, String atomUnitId)
id
and sole unit
identifier atomUnitId
.public static NumberFormat getNumberFormat(boolean interactive)
NumberFormat
suitable for display or interactive
formatting, or interactive parsing. The returned formatter mustn't be modified except as by
the NumberFormat.parse(String)
and Format.format(Object)
family of
methods.interactive
- true to format/parse for interactive use, false otherwiseNumberFormat
protected void setDefaultSelector(LinearKindOfQuantity.LinearUnitSelector unitSelector)
public KindOfQuantity<LinearUnit> getDeltaKind()
getDeltaKind
in class KindOfQuantity<LinearUnit>
public LinearUnit getDefaultUnit()
getDefaultUnit
in class KindOfQuantity<LinearUnit>
public LinearUnit getUnit(IPrefix<?> prefix)
public LinearUnit getUnit(String id)
getUnit
in class KindOfQuantity<LinearUnit>
public LinearUnit getCachedUnit(Object key)
protected ScaleFactor getBestScaleFactorFor(long value)
public LinearUnit makeUnit(String id, ITypedQuantity<LinearUnit> quantity)
id
- unit identifier which also is used as the unit symbolpublic LinearUnit makeUnit(String id, ITypedQuantity<LinearUnit> quantity, String localizedName)
public LinearUnit makeCustomUnit(ITypedQuantity<LinearUnit> quantity)
public String toString()
toString
in class ContentType<IQuantity>
public LinearUnit getPreferredUnit(IQuantity quantity, double minNumericalValue, double maxNumericalValue)
getPreferredUnit
in class KindOfQuantity<LinearUnit>
public LinearUnit getLargestExactUnit(IQuantity quantity)
KindOfQuantity
quantity.doubleValue()
== 0.0
),
quantity.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.
getLargestExactUnit
in class KindOfQuantity<LinearUnit>
null
public ITypedQuantity<LinearUnit> snapToBestBetweenHalfAndEqual(ITypedQuantity<LinearUnit> upperLimit)
public IFormatter<IQuantity> getFormatterResolving(IRange<IQuantity> range)
KindOfQuantity
range
, and consecutive equally spaced quantities. The returned formatter might
implement IIncrementalFormatter
, in which case its method can be used to reduce
redundant information between adjacent quantities.getFormatterResolving
in class KindOfQuantity<LinearUnit>
public ITypedQuantity<LinearUnit> parsePersisted(String persistedQuantity) throws QuantityConversionException
KindOfQuantity
IQuantity.persistableString()
for quantities of this kind of quantity. Only use this
on persisted strings, never for interactive input.parsePersisted
in interface IConstraint<IQuantity>
parsePersisted
in class KindOfQuantity<LinearUnit>
persistedQuantity
- persisted string to parseQuantityConversionException
- if parsing failedpublic Collection<LinearUnit> getAllUnits()
getAllUnits
in class KindOfQuantity<LinearUnit>
KindOfQuantity.getCommonUnits()
public Collection<LinearUnit> getCommonUnits()
getCommonUnits
in class KindOfQuantity<LinearUnit>
KindOfQuantity.getAllUnits()
protected void addUnit(LinearUnit unit)
KindOfQuantity
addUnit
in class KindOfQuantity<LinearUnit>
protected void addUnit(LinearUnit unit, boolean common)
common
- whether to include when showing most common units to userspublic ITypedQuantity<LinearUnit> parseInteractive(String interactiveQuantity) throws QuantityConversionException
KindOfQuantity
IQuantity.interactiveFormat()
for quantities of this kind of quantity and in the same
locale. Only use this for interactive input, never for persisted strings.parseInteractive
in interface IConstraint<IQuantity>
parseInteractive
in class KindOfQuantity<LinearUnit>
interactiveQuantity
- interactive string to parseQuantityConversionException
- if parsing failedpublic ITypedQuantity<LinearUnit> parseInteractive(String interactiveQuantity, Map<String,? extends LinearUnit> symbolToUnitMap) throws QuantityConversionException
interactiveQuantity
- string to parse, interactive stylesymbolToUnitMap
- map of additional symbols to parse into units, or nullQuantityConversionException
- containing detailed structured information, if parsing failedCopyright © 2019. All rights reserved.