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 java.util.Collection<LinearUnit> |
allUnits |
protected LinearUnit |
atomUnit |
protected java.lang.String |
atomUnitName |
protected java.util.Collection<LinearUnit> |
commonUnits |
protected java.util.Map<java.lang.String,LinearUnit> |
interactiveSymbolToUnitMap |
private static FormatThreadLocal<java.text.NumberFormat> |
NUMBER_FORMAT_DISPLAY_HOLDER |
private static FormatThreadLocal<java.text.NumberFormat> |
NUMBER_FORMAT_INTERACTIVE_HOLDER |
private static java.util.regex.Pattern |
NUMBER_UNIT_PATTERN |
protected java.util.Map<java.lang.Object,LinearUnit> |
unitCache |
protected LinearKindOfQuantity.LinearUnitSelector |
unitSelector |
m_identifier
Constructor and Description |
---|
LinearKindOfQuantity(java.lang.String id,
java.lang.String atomUnitId)
Create a new linear kind of quantity, with content type identifier
id and sole unit
identifier atomUnitId . |
LinearKindOfQuantity(java.lang.String id,
java.lang.String atomUnitId,
java.util.Collection<P> prefixes)
Create a new linear kind of quantity, with content type identifier
id and atom unit
identifier atomUnitId , using the prefixes in prefixes . |
LinearKindOfQuantity(java.lang.String id,
java.lang.String atomUnitId,
java.util.Collection<P> commonPrefixes,
java.util.Collection<P> allPrefixes)
Create a new linear kind of quantity, with content type identifier
id and atom unit
identifier atomUnitId , using the prefixes in allPrefixes but only designating
those in commonPrefixes as common. |
LinearKindOfQuantity(java.lang.String id,
java.lang.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.
|
java.util.Collection<LinearUnit> |
getAllUnits() |
protected ScaleFactor |
getBestScaleFactorFor(long value) |
LinearUnit |
getCachedUnit(java.lang.Object key) |
java.util.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. |
private static java.lang.String |
getHexNumberString(java.lang.String interactiveQuantity) |
LinearUnit |
getLargestExactUnit(IQuantity quantity)
Get the largest unit, if any, in which this quantity can be expressed exactly, typically with
an integer.
|
static java.text.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(java.lang.String id) |
private static boolean |
isHexDigit(char character) |
private boolean |
isHexString(java.lang.String interactiveQuantity) |
LinearUnit |
makeCustomUnit(ITypedQuantity<LinearUnit> quantity)
Create a custom unit.
|
LinearUnit |
makeUnit(java.lang.String id,
ITypedQuantity<LinearUnit> quantity)
Create an off-system unit with no default localized name.
|
LinearUnit |
makeUnit(java.lang.String id,
ITypedQuantity<LinearUnit> quantity,
java.lang.String localizedName) |
private LinearUnit |
makeUnit(java.lang.String id,
java.lang.String localizedSymbol,
ITypedQuantity<LinearUnit> quantity,
java.lang.String localizedName,
boolean custom)
Create an off-system unit.
|
private java.lang.Number |
parseHexNumberString(java.lang.String numberString) |
ITypedQuantity<LinearUnit> |
parseInteractive(java.lang.String interactiveQuantity)
Parse an interactive string.
|
ITypedQuantity<LinearUnit> |
parseInteractive(java.lang.String interactiveQuantity,
java.util.Map<java.lang.String,? extends LinearUnit> symbolToUnitMap) |
ITypedQuantity<LinearUnit> |
parsePersisted(java.lang.String persistedQuantity)
Parse a persisted string.
|
protected java.lang.String |
resolveLocalizedName(IPrefix<?> prefix) |
protected java.lang.String |
resolveLocalizedSymbol(IPrefix<?> prefix) |
protected void |
setDefaultSelector(LinearKindOfQuantity.LinearUnitSelector unitSelector) |
ITypedQuantity<LinearUnit> |
snapToBestBetweenHalfAndEqual(ITypedQuantity<LinearUnit> upperLimit) |
java.lang.String |
toString() |
combine, format, getAccessor, getAccessorKeys, getAttributes, getFirstBucket, getFirstBucket, getPersister, getRangeFormatter, interactiveFormat, persistableString, resolveLocalizedName, resolveLocalizedSymbol, validate
addFormatter, getDefaultFormatter, getDescription, getFormatter, getFormatters, getIdentifier, getName, hasAttribute, lookupNameFor
private static final FormatThreadLocal<java.text.NumberFormat> NUMBER_FORMAT_INTERACTIVE_HOLDER
private static final FormatThreadLocal<java.text.NumberFormat> NUMBER_FORMAT_DISPLAY_HOLDER
protected final LinearUnit atomUnit
protected final java.lang.String atomUnitName
protected final java.util.Collection<LinearUnit> commonUnits
protected final java.util.Collection<LinearUnit> allUnits
protected final java.util.Map<java.lang.String,LinearUnit> interactiveSymbolToUnitMap
protected LinearKindOfQuantity.LinearUnitSelector unitSelector
protected final java.util.Map<java.lang.Object,LinearUnit> unitCache
private static final java.util.regex.Pattern NUMBER_UNIT_PATTERN
public LinearKindOfQuantity(java.lang.String id, java.lang.String atomUnitId)
id
and sole unit
identifier atomUnitId
.public LinearKindOfQuantity(java.lang.String id, java.lang.String atomUnitId, P minPrefix, P maxPrefix)
id
and atom unit
identifier atomUnitId
, using prefixes in the range minPrefix
to
maxPrefix
.LinearKindOfQuantity(java.lang.String id, java.lang.String atomUnitId, java.util.Collection<P> prefixes)
id
and atom unit
identifier atomUnitId
, using the prefixes in prefixes
.LinearKindOfQuantity(java.lang.String id, java.lang.String atomUnitId, java.util.Collection<P> commonPrefixes, java.util.Collection<P> allPrefixes)
id
and atom unit
identifier atomUnitId
, using the prefixes in allPrefixes
but only designating
those in commonPrefixes
as common.public static java.text.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>
protected java.lang.String resolveLocalizedName(IPrefix<?> prefix)
protected java.lang.String resolveLocalizedSymbol(IPrefix<?> prefix)
public LinearUnit getUnit(IPrefix<?> prefix)
public LinearUnit getUnit(java.lang.String id)
getUnit
in class KindOfQuantity<LinearUnit>
public LinearUnit getCachedUnit(java.lang.Object key)
protected ScaleFactor getBestScaleFactorFor(long value)
public LinearUnit makeUnit(java.lang.String id, ITypedQuantity<LinearUnit> quantity)
id
- unit identifier which also is used as the unit symbolpublic LinearUnit makeUnit(java.lang.String id, ITypedQuantity<LinearUnit> quantity, java.lang.String localizedName)
public LinearUnit makeCustomUnit(ITypedQuantity<LinearUnit> quantity)
private LinearUnit makeUnit(java.lang.String id, java.lang.String localizedSymbol, ITypedQuantity<LinearUnit> quantity, java.lang.String localizedName, boolean custom)
localizedSymbol
- Default unit symbol. May be an empty string and may be overridden by translation.localizedName
- Default unit name. May be null
and may be overridden by translation.public java.lang.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(java.lang.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 java.util.Collection<LinearUnit> getAllUnits()
getAllUnits
in class KindOfQuantity<LinearUnit>
KindOfQuantity.getCommonUnits()
public java.util.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(java.lang.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(java.lang.String interactiveQuantity, java.util.Map<java.lang.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 failedprivate boolean isHexString(java.lang.String interactiveQuantity)
private java.lang.Number parseHexNumberString(java.lang.String numberString) throws QuantityConversionException
QuantityConversionException
private static java.lang.String getHexNumberString(java.lang.String interactiveQuantity)
private static boolean isHexDigit(char character)