public class Quantity extends Object implements Comparable<Quantity>, Serializable, 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.Modifier and Type | Field and Description |
---|---|
protected BigDecimal |
amount
BigDecimal object, holding the value of this Quantity . |
Constructor and Description |
---|
Quantity(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.
|
Object |
clone() |
int |
compareTo(Quantity o) |
<T extends Quantity> |
divide(T quantity)
Divides this by another quantity.
|
boolean |
equals(Object obj) |
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 . |
int |
hashCode() |
boolean |
isGreaterThan(Quantity other)
Compares Quantities.
|
boolean |
isGreaterThanOrEqualTo(Quantity other)
Compares Quantities.
|
boolean |
isLessThan(Quantity other)
Compares Quantities
|
boolean |
isNegative()
Check if this quantity is negative
|
<T extends Quantity> |
multiply(T quantity)
Multiplies two quantities.
|
<T extends Quantity> |
subtract(T quantity)
Subtracts a quantity from this.
|
String |
toString() |
protected BigDecimal amount
BigDecimal
object, holding the value of this Quantity
.public Quantity(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 BigDecimal getAmount()
amount
of this as float
.amount
of this as float
.public int compareTo(Quantity o)
compareTo
in interface Comparable<Quantity>
public boolean isNegative()
public boolean isLessThan(Quantity other)
other
- Quantity
to which this Quantity is to be compared.public boolean isGreaterThan(Quantity other)
public boolean isGreaterThanOrEqualTo(Quantity other)
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.Copyright © 2014 Pivotal Software, Inc.. All rights reserved.