public final class Amount<Q extends Quantity> extends java.lang.Object implements Measurable<Q>, Field<Amount<?>>, java.io.Serializable, javolution.lang.Immutable
This class represents a determinate or estimated amount for which
operations such as addition, subtraction, multiplication and division
can be performed (it implements the Field
interface).
The nature of an amount can be deduced from its parameterization
(compile time) or its unit
(run time).
Its precision is given by its error
.
Amounts can be exact
, in which case they can be
expressed as an exact long
integer in the amount unit.
The framework tries to keep amount exact as much as possible.
For example:[code]
Amount
Errors (including numeric errors) are calculated using numeric intervals.
It is possible to resolve systems of linear equations involving
matrices
, even if the
system is close to singularity; in which case the error associated with
some (or all) components of the solution may be large.
By default, non-exact amounts are shown using the plus/minus
character ('±') (see AmountFormat
). For example,
"(2.0 ± 0.001) km/s"
represents a velocity of
2 km/s with an absolute error of ± 1 m/s. Exact amount use an
integer notation (no decimal point, e.g. "2000 m"
).
Operations between different amounts may or may not be authorized
based upon the current PhysicalModel
. For example, adding Amount<Length> and
Amount<Duration> is not allowed by the
StandardModel
,
but is authorized with the RelativisticModel
.
Modifier and Type | Field and Description |
---|---|
static Amount<Dimensionless> |
ONE
Holds a dimensionless measure of one (exact).
|
static Amount<Dimensionless> |
ZERO
Holds a dimensionless measure of zero (exact).
|
Modifier and Type | Method and Description |
---|---|
Amount<Q> |
abs()
Returns the absolute value of this measure.
|
boolean |
approximates(Amount that)
Indicates if this measure approximates that measure.
|
int |
compareTo(Measurable<Q> that)
Compares this measure with the specified measurable object.
|
Amount<Q> |
copy()
Returns a deep copy of this object allocated in the memory area (RTSJ)
and/or
context (Javolution)
of the calling thread (the one making the copy). |
Amount<? extends Quantity> |
divide(Amount that)
Returns this measure divided by the one specified.
|
Amount<Q> |
divide(double divisor)
Returns this measure divided by the specified approximate divisor
(dimensionless).
|
Amount<Q> |
divide(long divisor)
Returns this measure divided by the specified exact divisor
(dimensionless).
|
double |
doubleValue(Unit<Q> unit)
Returns the value of this measurable stated in the specified unit as
a
double . |
boolean |
equals(java.lang.Object that)
Compares this measure against the specified object for strict
equality (same value interval and same units).
|
double |
getAbsoluteError()
Returns the value by which the
estimated
value may differ from the true value (all stated in base units). |
double |
getEstimatedValue()
Returns the estimated value for this measure stated in this measure
unit . |
long |
getExactValue()
Returns the exact value for this measure stated in this measure
unit . |
double |
getMaximumValue()
Returns the upper bound interval value for this measure stated in
this measure unit.
|
double |
getMinimumValue()
Returns the lower bound interval value for this measure stated in
this measure unit.
|
double |
getRelativeError()
Returns the percentage by which the estimated amount may differ
from the true amount.
|
Unit<Q> |
getUnit()
Returns the unit in which the
estimated value and absolute error
are stated. |
int |
hashCode()
Returns the hash code for this measure.
|
Amount<? extends Quantity> |
inverse()
Returns the multiplicative inverse of this measure.
|
boolean |
isExact()
Indicates if this measure amount is exact.
|
boolean |
isGreaterThan(Amount<Q> that)
Indicates if this measure is ordered after that measure
(independently of the measure unit).
|
boolean |
isLargerThan(Amount<Q> that)
Compares this measure with that measure ignoring the sign.
|
boolean |
isLessThan(Amount<Q> that)
Indicates if this measure is ordered before that measure
(independently of the measure unit).
|
long |
longValue(Unit<Q> unit)
Returns the estimated integral value of this measurable stated in
the specified unit as a
long . |
Amount<Q> |
minus(Amount that)
Returns the difference of this measure with the one specified.
|
Amount<Q> |
opposite()
Returns the opposite of this measure.
|
Amount<Q> |
plus(Amount that)
Returns the sum of this measure with the one specified.
|
Amount<? extends Quantity> |
pow(int exp)
Returns this measure raised at the specified exponent.
|
static <Q extends Quantity> |
rangeOf(double minimum,
double maximum,
Unit<Q> unit)
Returns the measure corresponding to the specified interval stated
in the specified unit.
|
Amount<? extends Quantity> |
root(int n)
Returns the given root of this measure.
|
Amount<? extends Quantity> |
sqrt()
Returns the square root of this measure.
|
Amount<? extends Quantity> |
times(Amount that)
Returns the product of this measure with the one specified.
|
Amount<Q> |
times(double factor)
Returns this measure scaled by the specified approximate factor
(dimensionless).
|
Amount<Q> |
times(long factor)
Returns this measure scaled by the specified exact factor
(dimensionless).
|
<R extends Quantity> |
to(Unit<R> unit)
Returns the measure equivalent to this measure but stated in the
specified unit.
|
java.lang.String |
toString()
Returns the text representation of this amount as a
java.lang.String . |
javolution.text.Text |
toText()
Returns the text representation of this measure.
|
static Amount<?> |
valueOf(java.lang.CharSequence csq)
Returns the measure represented by the specified character sequence.
|
static <Q extends Quantity> |
valueOf(double value,
double error,
Unit<Q> unit)
Returns the measure corresponding to the specified approximate value
and measurement error, both stated in the specified unit.
|
static <Q extends Quantity> |
valueOf(double value,
Unit<Q> unit)
Returns the measure corresponding to an approximate value
(
double ) stated in the specified unit;
the precision of the measure is assumed to be the
double precision (64 bits IEEE 754 format). |
static <Q extends Quantity> |
valueOf(long value,
Unit<Q> unit)
Returns the exact measure corresponding to the value stated in the
specified unit.
|
public static final Amount<Dimensionless> ZERO
public static final Amount<Dimensionless> ONE
public static <Q extends Quantity> Amount<Q> valueOf(long value, Unit<Q> unit)
value
- the exact value stated in the specified unit.unit
- the unit in which the value is stated.public static <Q extends Quantity> Amount<Q> valueOf(double value, Unit<Q> unit)
double
) stated in the specified unit;
the precision of the measure is assumed to be the
double
precision (64 bits IEEE 754 format).value
- the estimated value (± LSB) stated in the specified unit.unit
- the unit in which the value is stated.public static <Q extends Quantity> Amount<Q> valueOf(double value, double error, Unit<Q> unit)
value
- the estimated amount (± error) stated in the specified unit.error
- the measurement error (absolute).unit
- the unit in which the amount and the error are stated.java.lang.IllegalArgumentException
- if error < 0.0
public static <Q extends Quantity> Amount<Q> rangeOf(double minimum, double maximum, Unit<Q> unit)
minimum
- the lower bound for the measure value.maximum
- the upper bound for the measure value.unit
- the unit for both the minimum and maximum values.java.lang.IllegalArgumentException
- if minimum > maximum
public static Amount<?> valueOf(java.lang.CharSequence csq)
csq
- the character sequence.AmountFormat.getInstance().parse(csq)
public boolean isExact()
this.longValue()
); stating the amount
in any other unit may introduce conversion errors.true
if this measure is exact;
false
otherwise.public Unit<Q> getUnit()
estimated value
and absolute error
are stated.public long getExactValue() throws AmountException
unit
.long
) stated
in this measure's unit
AmountException
- if this measure is not isExact()
public double getEstimatedValue()
unit
.public double getMinimumValue()
public double getMaximumValue()
public double getAbsoluteError()
estimated
value
may differ from the true value (all stated in base units).public double getRelativeError()
public <R extends Quantity> Amount<R> to(Unit<R> unit)
unit
- the unit of the measure to be returned.ConversionException
- if the current model does not allows for
conversion to the specified unit.public Amount<Q> opposite()
opposite
in interface GroupAdditive<Amount<?>>
-this
.public Amount<Q> plus(Amount that) throws ConversionException
plus
in interface GroupAdditive<Amount<?>>
that
- the measure to be added.this + that
.ConversionException
- if the current model does not allows for
these quantities to be added.public Amount<Q> minus(Amount that) throws ConversionException
that
- the measure to be subtracted.this - that
.ConversionException
- if the current model does not allows for
these quantities to be subtracted.public Amount<Q> times(long factor)
factor
- the scaling factor.this · factor
.public Amount<Q> times(double factor)
factor
- the scaling factor.this · factor
.public Amount<? extends Quantity> times(Amount that)
public Amount<? extends Quantity> inverse()
inverse
in interface GroupMultiplicative<Amount<?>>
1 / this
.public Amount<Q> divide(long divisor)
divisor
- the exact divisor.this / divisor
.java.lang.ArithmeticException
- if this measure is exact and the
specified divisor is zero.public Amount<Q> divide(double divisor)
divisor
- the approximated divisor.this / divisor
.public Amount<? extends Quantity> divide(Amount that)
that
- the measure divisor.this / that
.public Amount<? extends Quantity> sqrt()
sqrt(this)
public Amount<? extends Quantity> root(int n)
n
- the root's order (n != 0).java.lang.ArithmeticException
- if n == 0
.public Amount<? extends Quantity> pow(int exp)
exp
- the exponent.thisexp
public int compareTo(Measurable<Q> that)
compareTo
in interface java.lang.Comparable<Measurable<Q extends Quantity>>
that
- the measure to compare with.ConversionException
- if the current model does not allows for
these measure to be compared.public boolean equals(java.lang.Object that)
equals
in class java.lang.Object
that
- the object to compare with.true
if this measure is identical to that
measure; false
otherwise.public int hashCode()
hashCode
in class java.lang.Object
public boolean approximates(Amount that)
this ≅ that
public boolean isLessThan(Amount<Q> that)
this.compareTo(that) < 0
.public boolean isGreaterThan(Amount<Q> that)
this.compareTo(that) > 0
.public boolean isLargerThan(Amount<Q> that)
|this| > |that|
public javolution.text.Text toText()
AmountFormat.getInstance().format(this)
public final java.lang.String toString()
java.lang.String
.toString
in class java.lang.Object
toText().toString()
public double doubleValue(Unit<Q> unit)
Measurable
double
. If the measurable has too great a magnitude to
be represented as a double
, it will be converted to
Double.NEGATIVE_INFINITY
or
Double.POSITIVE_INFINITY
as appropriate.doubleValue
in interface Measurable<Q extends Quantity>
unit
- the unit in which this measurable value is stated.double
.public final long longValue(Unit<Q> unit)
Measurable
long
.
Note: This method differs from the Number.longValue()
in the sense that the closest integer value is returned
and an ArithmeticException is raised instead
of a bit truncation in case of overflow (safety critical).
longValue
in interface Measurable<Q extends Quantity>
unit
- the unit in which the measurable value is stated.long
.public Amount<Q> copy()
javolution.lang.ValueType
context
(Javolution)
of the calling thread (the one making the copy).copy
in interface javolution.lang.ValueType