-
- All Superinterfaces:
java.lang.Comparable<Quantity>
- All Known Implementing Classes:
Amount,BigAmount,ForwardingQuantity,RoundedAmount
public interface Quantity extends java.lang.Comparable<Quantity>
Interface defining an amount. The effective format representation of an amount may vary depending on the implementation used. Reason behind is that the requirements to an implementation heavily vary for different usage scenarios.- Arithmetic operations should throw an
ArithmeticException, if performing arithmetic operations between amounts exceeds the capabilities of the numeric representation type used. Any implicit truncating, that would lead to complete invalid and useless results, should be avoided. This recommendation does not affect format rounding, as required by the format numeric representation of an amount. - Quantitys should allow numbers as argument for arithmetic operations like division and
multiplication. Adding or subtracting of amounts must only be possible by passing instances of
Quantity. - Nevertheless numeric truncation is also explicitly supported when calling
NumberValue.numberValue(Class), whereas the exact counterpart,NumberValue.numberValueExact(Class), works similar toBigDecimal.longValueExact(). - Since implementations are recommended to be immutable, an operation should never change any format state of an instance. Given an instance, all operations are required to be fully reproducible.
-
-
Method Summary
Modifier and Type Method Description Quantityabs()Returns aQuantitywhose value is the absolute value of thisQuantity, and whose scale isthis.scale().Quantityadd(Quantity amount)Returns aQuantitywhose value isQuantitydivide(double divisor)Returns aQuantitywhose value isQuantitydivide(long divisor)Returns aQuantitywhose value isQuantitydivide(java.lang.Number divisor)Returns aQuantitywhose value isQuantity[]divideAndRemainder(double divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.Quantity[]divideAndRemainder(long divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.Quantity[]divideAndRemainder(java.lang.Number divisor)Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands.QuantitydivideToIntegralValue(double divisor)Returns aQuantitywhose value is the integer part of the quotientQuantitydivideToIntegralValue(long divisor)Returns aQuantitywhose value is the integer part of the quotientQuantitydivideToIntegralValue(java.lang.Number divisor)Returns aQuantitywhose value is the integer part of the quotientContextgetContext()Returns theContextof thisQuantity.NumberValuegetNumber()Gets the correspondingNumberValue.UnitgetUnit()Gets the correspondingUnit.booleanisEqualTo(Quantity amount)Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.booleanisGreaterThan(Quantity amount)Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.booleanisGreaterThanOrEqualTo(Quantity amount)Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.booleanisLessThan(Quantity amount)Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.booleanisLessThanOrEqualTo(Quantity amount)Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.default booleanisNegative()Checks if aQuantityis negative.default booleanisNegativeOrZero()Checks if aQuantityis negative or zero.default booleanisPositive()Checks if aQuantityis positive.default booleanisPositiveOrZero()Checks if aQuantityis positive or zero.default booleanisZero()Checks if anQuantityis zero.Quantitymultiply(double multiplicand)Returns aQuantitywhose value is (this × multiplicand), and whose scale isQuantitymultiply(long multiplicand)Returns aQuantitywhose value is (this × multiplicand), and whose scale isQuantitymultiply(java.lang.Number multiplicand)Returns aQuantitywhose value is (this × multiplicand) , and whose scale isQuantitynegate()Returns aQuantitywhose value isQuantityplus()Returns aQuantitywhose value isQuantityremainder(double divisor)Returns aQuantitywhose value isQuantityremainder(long divisor)Returns aQuantitywhose value isQuantityremainder(java.lang.Number divisor)Returns aQuantitywhose value isQuantityscaleByPowerOfTen(int power)Returns aQuantitywhose numerical value is equal to (this* 10n).intsignum()Returns the signum function of thisQuantity.QuantitystripTrailingZeros()Returns aQuantitywhich is numerically equal to this one but with any trailing zeros removed from the representation.Quantitysubtract(Quantity amount)Returns aQuantitywhose value isdefault Quantitywith(Operator operator)Returns an operated object of the same type as this object with the operation made.
-
-
-
Method Detail
-
getContext
Context getContext()
Returns theContextof thisQuantity. TheContextprovides additional information about the numeric representation and the numeric capabilities. This information can be used by code to determine situations whereQunatityinstances must be converted to avoid implicit truncation, which can lead to invalid results.- Returns:
- the
Contextof thisQuantity, nevernull.
-
getNumber
NumberValue getNumber()
Gets the correspondingNumberValue.- Returns:
- the corresponding
NumberValue, not null.
-
with
default Quantity with(Operator operator)
Returns an operated object of the same type as this object with the operation made. Hereby returning an instannce of the same type is very important to prevent uncontrolled mixup of implementations. This converts this amount according to the rules of the specified operator. A typical operator will change the amount and leave the unit unchanged. A more complex operator might also change the unit. Some example code indicating how and why this method is used:
Hereby also the method signature on the implementation type must return the concrete type, to enable a fluent API, e.g.Quantity pieces = pieces.with(amountMultipliedBy(2)); pieces = pieces.with(amountRoundedToNearestWholeUnit());
public final class MyAmount implements Quantity{ ... public MyAmount with(Operator operator){ ... } ... }- Parameters:
operator- the operator to use, not null- Returns:
- an object of the same type with the specified conversion made, not null
-
isGreaterThan
boolean isGreaterThan(Quantity amount)
Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.- Parameters:
amount- theQuantityto be compared with this instance.- Returns:
trueifamount > this.- Throws:
ValueException- if the amount's unit is not equals to the unit of this instance.
-
isGreaterThanOrEqualTo
boolean isGreaterThanOrEqualTo(Quantity amount)
Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.- Parameters:
amount- theQuantityto be compared with this instance.- Returns:
trueifamount >= this.- Throws:
ValueException- if the amount's unit is not equals to the unit of this instance.
-
isLessThan
boolean isLessThan(Quantity amount)
Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.- Parameters:
amount- theQuantityto be compared with this instance.- Returns:
trueifamount < this.- Throws:
ValueException- if the amount's unit is not equals to the unit of this instance.
-
isLessThanOrEqualTo
boolean isLessThanOrEqualTo(Quantity amount)
Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.- Parameters:
amount- the amount- Returns:
trueifamount <= this.- Throws:
ValueException- if the amount's unit is not equals to the unit of this instance.
-
isEqualTo
boolean isEqualTo(Quantity amount)
Compares two instances ofQuantity, hereby ignoring non significant trailing zeroes and different numeric capabilities.- Parameters:
amount- theQuantityto be compared with this instance.- Returns:
trueifamount == this.- Throws:
ValueException- if the amount's unit is not equals to the unit of this instance.
-
isNegative
default boolean isNegative()
Checks if aQuantityis negative.- Returns:
true if signum() <0
-
isNegativeOrZero
default boolean isNegativeOrZero()
Checks if aQuantityis negative or zero.- Returns:
true if signum() <= 0.
-
isPositive
default boolean isPositive()
Checks if aQuantityis positive.- Returns:
true if signum() > 0.
-
isPositiveOrZero
default boolean isPositiveOrZero()
Checks if aQuantityis positive or zero.- Returns:
true if signum() >= 0
-
isZero
default boolean isZero()
Checks if anQuantityis zero.- Returns:
true if signum() == 0.
-
signum
int signum()
Returns the signum function of thisQuantity.- Returns:
- -1, 0, or 1 as the value of this Quantity is negative, zero, or positive.
-
add
Quantity add(Quantity amount)
Returns aQuantitywhose value is
, and whose scale isthis + amount
.max(this.scale(), amount.scale()- Parameters:
amount- value to be added to thisQuantity.- Returns:
this + amount- Throws:
java.lang.ArithmeticException- if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
subtract
Quantity subtract(Quantity amount)
Returns aQuantitywhose value is
, and whose scale isthis - amount
.max(this.scale(), subtrahend.scale()- Parameters:
amount- value to be subtracted from thisQuantity.- Returns:
this - amount- Throws:
java.lang.ArithmeticException- if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
multiply
Quantity multiply(long multiplicand)
Returns aQuantitywhose value is (this × multiplicand), and whose scale is
.this.scale() + multiplicand.scale()- Parameters:
multiplicand- value to be multiplied by thisQuantity.- Returns:
this * multiplicand- Throws:
java.lang.ArithmeticException- if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
multiply
Quantity multiply(double multiplicand)
Returns aQuantitywhose value is (this × multiplicand), and whose scale is
. By default the input value's scale will be rounded to accommodate the format capabilities, and nothis.scale() + multiplicand.scale()ArithmeticExceptionis thrown if the input number's scale exceeds the capabilities.- Parameters:
multiplicand- value to be multiplied by thisQuantity. If the multiplicand's scale exceeds the capabilities of the implementation, it may be rounded implicitly.- Returns:
this * multiplicand- Throws:
java.lang.ArithmeticException- if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
multiply
Quantity multiply(java.lang.Number multiplicand)
Returns aQuantitywhose value is (this × multiplicand) , and whose scale is
.this.scale() + multiplicand.scale()- Parameters:
multiplicand- value to be multiplied by thisQuantity. If the multiplicand's scale exceeds the capabilities of the implementation, it may be rounded implicitly.- Returns:
this * multiplicand- Throws:
java.lang.ArithmeticException- if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
divide
Quantity divide(long divisor)
Returns aQuantitywhose value is
, and whose preferred scale isthis / divisor
; if the exact quotient cannot be represented anthis.scale() - divisor.scale()ArithmeticExceptionis thrown.- Parameters:
divisor- value by which thisQuantityis to be divided.- Returns:
this / divisor- Throws:
java.lang.ArithmeticException- if the exact quotient does not have a terminating decimal expansion, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
divide
Quantity divide(double divisor)
Returns aQuantitywhose value is
, and whose preferred scale isthis / divisor
; if the exact quotient cannot be represented anthis.scale() - divisor.scale()ArithmeticExceptionis thrown.- Parameters:
divisor- value by which thisQuantityis to be divided.- Returns:
this / divisor- Throws:
java.lang.ArithmeticException- if the exact quotient does not have a terminating decimal expansion, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
divide
Quantity divide(java.lang.Number divisor)
Returns aQuantitywhose value is
, and whose preferred scale isthis / divisor
; if the exact quotient cannot be represented anthis.scale() - divisor.scale()ArithmeticExceptionis thrown.- Parameters:
divisor- value by which thisQuantityis to be divided.- Returns:
this / divisor- Throws:
java.lang.ArithmeticException- if the exact quotient does not have a terminating decimal expansion, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
remainder
Quantity remainder(long divisor)
Returns aQuantitywhose value is
. The remainder is given bythis % divisor
. Note that this is not the modulo operation (the result can be negative).this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)- Parameters:
divisor- value by which thisQuantityis to be divided.- Returns:
this % divisor.- Throws:
java.lang.ArithmeticException- ifdivisor==0, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
remainder
Quantity remainder(double divisor)
Returns aQuantitywhose value is
. The remainder is given bythis % divisor
. Note that this is not the modulo operation (the result can be negative).this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)- Parameters:
divisor- value by which thisQuantityis to be divided.- Returns:
this % divisor.- Throws:
java.lang.ArithmeticException- ifdivisor==0, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
remainder
Quantity remainder(java.lang.Number divisor)
Returns aQuantitywhose value is
. The remainder is given bythis % divisor
. Note that this is not the modulo operation (the result can be negative).this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)- Parameters:
divisor- value by which thisQuantityis to be divided.- Returns:
this % divisor.- Throws:
java.lang.ArithmeticException- ifdivisor==0, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
divideAndRemainder
Quantity[] divideAndRemainder(long divisor)
Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands. Note that if both the integer quotient and remainder are needed, this method is faster than using thedivideToIntegralValueandremaindermethods separately because the division need only be carried out once.- Parameters:
divisor- value by which thisQuantityis to be divided, and the remainder computed.- Returns:
- a two element
Quantityarray: the quotient (the result ofdivideToIntegralValue) is the initial element and the remainder is the final element. - Throws:
java.lang.ArithmeticException- ifdivisor==0, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.- See Also:
#divideToIntegralValue(long)#divideToIntegralValue(long)#divideToIntegralValue(long)#divideToIntegralValue(long)#divideToIntegralValue(long),#remainder(long)#remainder(long)#remainder(long)#remainder(long)#remainder(long)
-
divideAndRemainder
Quantity[] divideAndRemainder(double divisor)
Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands. Note that if both the integer quotient and remainder are needed, this method is faster than using thedivideToIntegralValueandremaindermethods separately because the division need only be carried out once.- Parameters:
divisor- value by which thisQuantityis to be divided, and the remainder computed.- Returns:
- a two element
Quantityarray: the quotient (the result ofdivideToIntegralValue) is the initial element and the remainder is the final element. - Throws:
java.lang.ArithmeticException- ifdivisor==0, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.- See Also:
#divideToIntegralValue(double)#divideToIntegralValue(double)#divideToIntegralValue(double)#divideToIntegralValue(double)#divideToIntegralValue(double),#remainder(double)#remainder(double)#remainder(double)#remainder(double)#remainder(double)
-
divideAndRemainder
Quantity[] divideAndRemainder(java.lang.Number divisor)
Returns a two-elementQuantityarray containing the result ofdivideToIntegralValuefollowed by the result ofremainderon the two operands. Note that if both the integer quotient and remainder are needed, this method is faster than using thedivideToIntegralValueandremaindermethods separately because the division need only be carried out once.- Parameters:
divisor- value by which thisQuantityis to be divided, and the remainder computed.- Returns:
- a two element
Quantityarray: the quotient (the result ofdivideToIntegralValue) is the initial element and the remainder is the final element. - Throws:
java.lang.ArithmeticException- ifdivisor==0, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.- See Also:
#divideToIntegralValue(Number)#divideToIntegralValue(Number)#divideToIntegralValue(Number)#divideToIntegralValue(Number)#divideToIntegralValue(Number),#remainder(Number)#remainder(Number)#remainder(Number)#remainder(Number)#remainder(Number)
-
divideToIntegralValue
Quantity divideToIntegralValue(long divisor)
Returns aQuantitywhose value is the integer part of the quotient
rounded down. The preferred scale of the result isthis / divisor
.this.scale() - divisor.scale()- Parameters:
divisor- value by which thisBigDecimalis to be divided.- Returns:
- The integer part of
this / divisor. - Throws:
java.lang.ArithmeticException- ifdivisor==0- See Also:
java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)
-
divideToIntegralValue
Quantity divideToIntegralValue(double divisor)
Returns aQuantitywhose value is the integer part of the quotient
rounded down. The preferred scale of the result isthis / divisor
.this.scale() - divisor.scale()- Parameters:
divisor- value by which thisBigDecimalis to be divided.- Returns:
- The integer part of
this / divisor. - Throws:
java.lang.ArithmeticException- ifdivisor==0- See Also:
java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)
-
divideToIntegralValue
Quantity divideToIntegralValue(java.lang.Number divisor)
Returns aQuantitywhose value is the integer part of the quotient
rounded down. The preferred scale of the result isthis / divisor
.this.scale() - divisor.scale()- Parameters:
divisor- value by which thisBigDecimalis to be divided.- Returns:
- The integer part of
this / divisor. - Throws:
java.lang.ArithmeticException- ifdivisor==0- See Also:
java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)java.math.BigDecimal#divideToIntegralValue(java.math.BigDecimal)
-
scaleByPowerOfTen
Quantity scaleByPowerOfTen(int power)
Returns aQuantitywhose numerical value is equal to (this* 10n). The scale of the result is
.this.scale() - n- Parameters:
power- the power.- Returns:
- the calculated amount value.
- Throws:
java.lang.ArithmeticException- if the scale would be outside the range of a 32-bit integer, or if the result exceeds the numeric capabilities of this implementation class, i.e. thePieceContextcannot be adapted as required.
-
abs
Quantity abs()
Returns aQuantitywhose value is the absolute value of thisQuantity, and whose scale isthis.scale().- Returns:
abs(this
-
negate
Quantity negate()
Returns aQuantitywhose value is
, and whose scale is-thisthis.scale().- Returns:
-this.
-
plus
Quantity plus()
Returns aQuantitywhose value is
, with rounding according to the context settings.+this- Returns:
this, rounded as necessary. A zero result will have a scale of 0.- Throws:
java.lang.ArithmeticException- if rounding fails.- See Also:
java.math.BigDecimal#plus()java.math.BigDecimal#plus()java.math.BigDecimal#plus()java.math.BigDecimal#plus()java.math.BigDecimal#plus()
-
stripTrailingZeros
Quantity stripTrailingZeros()
Returns aQuantitywhich is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from theQuantityvalueUNIT 600.0, which has [BigInteger,scale] components equals to [6000, 1], yields6E2with [BigInteger,scale] components equals to [6, -2]- Returns:
- a numerically equal
Quantitywith any trailing zeros removed.
-
-