public class Quantity extends java.lang.Object implements java.lang.Comparable<Quantity>, java.io.Serializable, java.lang.Cloneable
Metric
. A RoundingStrategy
accommodates amounts that do not fit the value set of a given metric (Think
of money: 0.0001€ has no meaning in the real world). The
roundingStrategy
is applied to amount
in the class
constructor. This way, every instance has a valid amount
.
To allow arithmetic operations on Quantity
objects and instances
of subclasses of Quantity
to return the correct type (and thus
avoiding casts), Quantity
instances are immutable and all
subclasses of Quantity
have to be immutable or implement a
suitable clone()
-method.Constructor and Description |
---|
Quantity(java.math.BigDecimal amount,
Metric metric,
RoundingStrategy roundingStrategy)
Parameterized class constructor.
|
Quantity(double amount,
Metric metric,
RoundingStrategy roundingStrategy)
Translates a
double to a Quantity . |
Quantity(float amount,
Metric metric,
RoundingStrategy roundingStrategy)
Translates a
float to a Quantity . |
Quantity(int amount,
Metric metric,
RoundingStrategy roundingStrategy)
Translates an
int to a Quantity . |
Quantity(long amount,
Metric metric,
RoundingStrategy roundingStrategy)
Translates a
long to a Quantity . |
Modifier and Type | Method and Description |
---|---|
<T extends Quantity> |
add(T quantity)
Sums two quantities up.
|
java.lang.Object |
clone() |
int |
compareTo(Quantity o) |
<T extends Quantity> |
divide(T quantity)
Divides this by another quantity.
|
boolean |
equals(java.lang.Object obj) |
java.math.BigDecimal |
getAmount()
Returns the
amount of this as float . |
Metric |
getMetric()
Returns the
metric associated with this
Quantity . |
RoundingStrategy |
getRoundingStrategy()
Returns the
RoundingStrategy of this Quantity . |
boolean |
greaterThan(Quantity other)
Compares Quantities
|
int |
hashCode() |
boolean |
isNegative()
Check if this quantity is negative
|
boolean |
lessThan(Quantity other)
Compares Quantities
|
<T extends Quantity> |
multiply(T quantity)
Multiplies two quantities.
|
<T extends Quantity> |
subtract(T quantity)
Subtracts a quantity from this.
|
java.lang.String |
toString() |
public Quantity(java.math.BigDecimal amount, Metric metric, RoundingStrategy roundingStrategy)
amount
is immediately
rounded using the supplied roundingStrategy
.amount
- amount
represented by this Quantity
metric
- metric
used for this Quantity
roundingStrategy
- roundingStrategy
to be used with this
Quantity
public Quantity(int amount, Metric metric, RoundingStrategy roundingStrategy)
int
to a Quantity
.
The Integer is rounded according to the supplied
RoudingStrategy
.amount
- int
value to be converted to
Quantity
metric
- metric
to be used with this Quantity
roundingStrategy
- roudingStrategy
to be used with this
Quantity
public Quantity(long amount, Metric metric, RoundingStrategy roundingStrategy)
long
to a Quantity
.amount
- long
value to be converted to a
Quantity
metric
- metric
to be used with this Quantity
roundingStrategy
- roundingStrategy
to be used with this
Quantity
public Quantity(float amount, Metric metric, RoundingStrategy roundingStrategy)
float
to a Quantity
.amount
- float
value to be converted to a
Quantity
metric
- metric
to be used with this Quantity
roundingStrategy
- roundingStrategy
to be used with this
Quantity
public Quantity(double amount, Metric metric, RoundingStrategy roundingStrategy)
double
to a Quantity
.amount
- double
value to be converted to a
Quantity
metric
- metric
to be used with this Quantity
roundingStrategy
- roundingStrategy
to be used with this
Quantity
public Metric getMetric()
metric
associated with this
Quantity
.Metric
of this Quantity
.public RoundingStrategy getRoundingStrategy()
RoundingStrategy
of this Quantity
.RoundingStrategy
of this Quantity
.public java.math.BigDecimal getAmount()
amount
of this as float
.amount
of this as float
.public int compareTo(Quantity o)
compareTo
in interface java.lang.Comparable<Quantity>
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public boolean isNegative()
public boolean lessThan(Quantity other)
other
- Quantity to which this Quantity is to be compared.public boolean greaterThan(Quantity other)
other
- Quantity to which this Quantity is to be compared.public java.lang.String toString()
toString
in class java.lang.Object
public final int hashCode()
hashCode
in class java.lang.Object
public java.lang.Object clone()
clone
in class java.lang.Object
public <T extends Quantity> T add(T quantity)
amount
of this and quantity are
added, and a new Quantity
-instance is returned, representing
the sum. The metric and rounding strategy of quantity
are
used for the new instance. No check is performed, whether the metrics of
both quantities are compatible, i.e. it is possible to combine, say kg
and s.
To avoid casting, arithmetic methods are generic. If a sub class of
Quantity
is passed as parameter, the result will have the
same type, as the parameter.
T
- type of the parameter and returned objectquantity
- Quantity
to be added to this.Quantity
object representing the sum of this
and quantity
.public <T extends Quantity> T subtract(T quantity)
amount
of this is
subtracted by that of quantity, and a new Quantity
-instance
is returned, representing the difference. The metric and rounding
strategy of quantity
are used for the new instance. No check
is performed, whether the metrics of both quantities are compatible, i.e.
it is possible to combine, say kg and s.
To avoid casting, arithmetic methods are generic. If a sub class of
Quantity
is passed as parameter, the result will have the
same type, as the parameter.
T
- type of the parameter and returned objectquantity
- Quantity
to be subtracted from this.Quantity
object representing the difference of
this and quantity
.public <T extends Quantity> T multiply(T quantity)
amount
of this and quantity
are multiplied, and a new Quantity
-instance is returned,
representing the product. The metric and rounding Strategy of this are
used for the new instance. No check is performed, whether the metrics of
both quantities are compatible, i.e. it is possible to combine, say kg
and s.
To avoid casting, arithmetic methods are generic. If a sub class of
Quantity
is passed as parameter, the result will have the
same type, as the parameter.
T
- type of the parameter and returned objectquantity
- Quantity
to be multiplied with this.Quantity
object representing the product of
this and quantity
.public <T extends Quantity> T divide(T quantity)
amount
of this the
dividend, quantity
the divisor, and a new
Quantity
-instance is returned as quotient. The metric and
rounding Strategy of this are used for the new instance. No check is
performed, whether the metrics of both quantities are compatible, i.e. it
is possible to combine, say kg and s.
To avoid casting, arithmetic methods are generic. If a sub class of
Quantity
is passed as parameter, the result will have the
same type, as the parameter.
T
- type of the parameter and returned objectquantity
- Quantity
to be used as divisor.Quantity
object representing the quotient.