abstract class ScalarQuantity<U extends TypedUnit<U>> extends java.lang.Number implements ITypedQuantity<U>
Note: This class has a natural ordering that is inconsistent with equals
.
(This is because different instances may express the same quantity with different precision. This
is inherent to implementations of IQuantity
, irrespective of class hierarchies.)
Modifier and Type | Class and Description |
---|---|
static class |
ScalarQuantity.DoubleStored<U extends TypedUnit<U>> |
static class |
ScalarQuantity.LongStored<U extends TypedUnit<U>> |
Modifier and Type | Field and Description |
---|---|
private static long |
serialVersionUID |
protected U |
unit |
AUTO, EXACT, VERBOSE
Modifier | Constructor and Description |
---|---|
protected |
ScalarQuantity(U unit) |
Modifier and Type | Method and Description |
---|---|
IQuantity |
add(IQuantity addend)
Returns a new quantity that is the arithmetic sum of this quantity and
addend , if
such an operation is defined for this kind of quantity. |
int |
compareTo(IQuantity other) |
java.lang.String |
displayUsing(java.lang.String formatIdentifier)
Format this object for display purposes, preferably using the formatter hinted by
formatHint . |
abstract boolean |
equals(java.lang.Object obj) |
KindOfQuantity<U> |
getType()
Get the kind of this quantity.
|
U |
getUnit() |
abstract int |
hashCode() |
IQuantity |
in(IUnit targetUnit)
Get this quantity expressed in the unit
targetUnit . |
ITypedQuantity<U> |
in(U targetUnit)
Get this quantity expressed in the unit
targetUnit . |
java.lang.String |
interactiveFormat()
An exact string representation taking locale and internationalization into account.
|
java.lang.String |
interactiveFormat(boolean allowCustomUnit) |
boolean |
isLinear()
If this quantity is linear.
|
long |
longValueIn(IUnit targetUnit)
Get the numerical quantity value that this quantity would have if expressed in the unit
targetUnit , rounded to a mathematical integer, if that numerical value can be
represented in a long . |
IQuantity |
subtract(IQuantity subtrahend)
Returns a new quantity that is the arithmetic difference of this quantity and
subtrahend , if such an operation is defined for this kind of quantity. |
protected abstract ITypedQuantity<U> |
subtractLinear(ITypedQuantity<LinearUnit> subtrahend) |
byteValue, doubleValue, floatValue, intValue, longValue, shortValue
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
add, floorQuantize, localizedFormat, multiply, multiply, subtract
clampedFloorIn, clampedIntFloorIn, clampedLongValueIn, doubleValue, doubleValueIn, longValue, longValueIn, numberValue, numberValueIn, persistableString, ratioTo
private static final long serialVersionUID
protected ScalarQuantity(U unit)
public U getUnit()
public KindOfQuantity<U> getType()
IQuantity
public int compareTo(IQuantity other)
compareTo
in interface java.lang.Comparable<IQuantity>
public abstract boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public abstract int hashCode()
hashCode
in class java.lang.Object
public IQuantity in(IUnit targetUnit)
IQuantity
targetUnit
. Note that as a result of this
conversion, precision may be lost. Partly due to that fact, this method should generally not
be used. If the ultimate goal is some pure numerical value, there are more suitable methods,
some of which are listed below, which directly provides such values. In either case, the
quantity returned from this method should not be kept alive, as the original quantity
provides better precision.in
in interface IQuantity
targetUnit
IQuantity.doubleValueIn(IUnit)
,
IQuantity.numberValueIn(IUnit)
,
IQuantity.clampedLongValueIn(IUnit)
,
IQuantity.ratioTo(IQuantity)
public ITypedQuantity<U> in(U targetUnit)
ITypedQuantity
targetUnit
. Note that as a result of this
conversion, precision may be lost. Note that this method differs from IQuantity.in(IUnit)
only
by stricter typing.in
in interface ITypedQuantity<U extends TypedUnit<U>>
targetUnit
public long longValueIn(IUnit targetUnit) throws QuantityConversionException
IQuantity
targetUnit
, rounded to a mathematical integer, if that numerical value can be
represented in a long
. Otherwise, an QuantityConversionException
will be
thrown, with the violation encoded. Note that as a result of this conversion, precision may
be lost.
This method is equivalent to longValueIn(IUnit,
Long.MAX_VALUE)
.
longValueIn
in interface IQuantity
long
QuantityConversionException
- if the result would be out of rangeIQuantity.numberValueIn(IUnit)
public final java.lang.String interactiveFormat()
IQuantity
KindOfQuantity.parseInteractive(String)
(by this quantity's kind of
quantity), in the same locale, yields a value that is equal
to
this quantity. That is, the exact representation must be preserved.interactiveFormat
in interface IQuantity
public final java.lang.String interactiveFormat(boolean allowCustomUnit)
interactiveFormat
in interface ITypedQuantity<U extends TypedUnit<U>>
public java.lang.String displayUsing(java.lang.String formatIdentifier)
IDisplayable
formatHint
. If no such formatter is defined for this type, the default formatter will
be used.displayUsing
in interface IDisplayable
formatIdentifier
- the format hintpublic boolean isLinear()
IQuantity
public IQuantity add(IQuantity addend) throws java.lang.IllegalArgumentException
IQuantity
addend
, if
such an operation is defined for this kind of quantity. This operation is commutative, so
that a.add(b)
produces the same result, or throws the same kind of exception, as
b.add(a)
.
Note that if this quantity is linear, adding a quantity of the same kind is guaranteed to succeed and produces a quantity of the same kind. Also, the sum of a non-linear quantity and a linear quantity of a specific related (delta) kind of quantity is defined and produces a non-linear quantity. However, two non-linear quantities may never be added.
public IQuantity subtract(IQuantity subtrahend) throws java.lang.IllegalArgumentException
IQuantity
subtrahend
, if such an operation is defined for this kind of quantity. This operation
is anti-commutative when this quantity and subtrahend
are of the same kind. That is,
when the result is linear.
Quantities of the same kind may be subtracted, giving a linear result. Additionally, a linear quantity may be subtracted from a non-linear quantity, provided their kinds are related.
protected abstract ITypedQuantity<U> subtractLinear(ITypedQuantity<LinearUnit> subtrahend)