Package com.storedobject.core
Class Quantity
java.lang.Object
com.storedobject.core.Quantity
- All Implemented Interfaces:
Storable
,Comparable<Quantity>
- Direct Known Subclasses:
Angle
,Area
,Count
,Distance
,FractionalCount
,Percentage
,Pressure
,Speed
,Temperature
,TimeDuration
,Volume
,VolumeRate
,Weight
,WeightOrVolume
,WeightRate
Class that represents a quantity. This class and its descendants are immutable.
- Author:
- Syam
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic MeasurementUnit
static final BigDecimal
static final MathContext
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Quantity
(BigDecimal value, MeasurementUnit unit) Create a quantity from the quantity specified in a particular unit. -
Method Summary
Modifier and TypeMethodDescriptionabsolute()
Absolute value of this quantity.Add quantityAdd quantity valueadd
(BigDecimal quantity) Add quantityboolean
canConvert
(MeasurementUnit toUnit) boolean
canConvert
(MeasurementUnit toUnit, String message) boolean
canConvert
(Quantity another) boolean
canConvert
(Quantity another, String message) int
Convert the quantity to another unit.convert
(MeasurementUnit u, int decimals) Convert the quantity to another unit rounded to the given number of decimals.<Q extends Quantity>
Qconvert
(Q quantity) Convert the quantity to another unit.<Q extends Quantity>
Qconvert
(Q quantity, int decimals) Convert the quantity to another unit.Get the value of this quantity if converted to the given unit.static Quantity
create
(double quantity, MeasurementUnit unit) static <Q extends Quantity>
Qstatic <Q extends Quantity>
Qstatic Quantity
create
(MeasurementUnit unit) static <Q extends Quantity>
Qstatic <Q extends Quantity>
Qstatic Quantity
Construct a quantity from a string representation.static <Q extends Quantity>
Qstatic <Q extends Quantity>
Qstatic Quantity
create
(BigDecimal quantity, MeasurementUnit unit) static <Q extends Quantity>
Qcreate
(BigDecimal quantity, Class<Q> qClass) static <Q extends Quantity>
Qcreate
(BigDecimal quantity, Class<Q> qClass, String unit) divide
(double divisor) Divide the quantity with a valueDivide the money with this quantity (To get the unit price)divide
(BigDecimal divisor) Divide the quantity with a valuefinal boolean
protected boolean
equalsWhenZero
(Quantity another) Check if this quantity is equal to another quantity when both are zero, but their units are different.getClass
(MeasurementUnit unit) Get the storable value.static int
getUnit()
static MeasurementUnit
static MeasurementUnit
getValue()
Get the quantity as BigDecimal.int
hashCode()
boolean
isCompatible
(Quantity another) boolean
isConvertible
(MeasurementUnit toUnit) boolean
isConvertible
(Quantity another) boolean
isGreaterThan
(long value) Check if this is greater than 'value'.boolean
isGreaterThan
(Quantity another) Check if this is greater than 'another'.boolean
isGreaterThanOrEqual
(long value) Check if this is greater than or equal to 'value'.boolean
isGreaterThanOrEqual
(Quantity another) Check if this is greater than or equal to 'another'.boolean
isLessThan
(long value) Check if this is less than 'value'.boolean
isLessThan
(Quantity another) Check if this is less than 'another'.boolean
isLessThanOrEqual
(long value) Check if this is less than or equal to 'value'.boolean
isLessThanOrEqual
(Quantity another) Check if this is less than or equal to 'another'.boolean
Checks if this is a negative quantity or not.boolean
Checks if this is a positive non-zero quantity or not.boolean
isZero()
Checks if this is zero or not.multiply
(double multiplicand) MultiplyMultiply the money with this quantity (Given the unit price, find out the total amount)multiply
(BigDecimal multiplicand) Multiplynegate()
Reverses the sign of this quantityround
(int decimals) Round the quantity to the specified number of decimals.void
setUnit
(MeasurementUnit unit) void
Subtract quantitySubtract quantitysubtract
(BigDecimal quantity) Subtract quantitystatic Quantity
Sum quantities.toString()
Create the formatted representation of the quantity value with appropriate decimals places.toString
(boolean showSymbol) Create the formatted representation of the quantity value with appropriate decimals places.toString
(boolean showSymbol, int decimals) Create the formatted representation of the quantity value with appropriate decimals places.toString
(int decimals) Create the formatted representation of the quantity value with appropriate decimals places.types()
zero()
Create a quantity of this type with zero value.
-
Field Details
-
PRECISION
-
PI
-
defaultUnit
-
-
Constructor Details
-
Quantity
Create a quantity from the quantity specified in a particular unit.- Parameters:
value
- The BigDecimal value.unit
- MeasurementUnit
-
-
Method Details
-
create
-
create
-
create
-
create
-
create
-
create
-
create
-
create
-
create
-
create
-
create
-
create
-
getClass
-
getValue
-
getUnit
-
setUnit
-
setUnit
-
getUnit
-
getUnit
-
getTypeName
-
getType
-
isCompatible
-
isConvertible
-
isConvertible
-
canConvert
- Throws:
Invalid_State
-
canConvert
- Throws:
Invalid_State
-
canConvert
- Throws:
Invalid_State
-
canConvert
- Throws:
Invalid_State
-
zero
-
add
-
add
Add quantity- Parameters:
quantity
- The quantity value to add- Returns:
- Result
-
add
-
sum
-
subtract
-
subtract
Subtract quantity- Parameters:
quantity
- The quantity value to subtract- Returns:
- Result
-
subtract
-
multiply
Multiply- Parameters:
multiplicand
- Multiplicand- Returns:
- Result
-
multiply
Multiply- Parameters:
multiplicand
- Multiplicand- Returns:
- Result
-
multiply
-
divide
Divide the quantity with a value- Parameters:
divisor
- Divisor- Returns:
- Result
-
divide
Divide the quantity with a value- Parameters:
divisor
- Divisor- Returns:
- Result
-
divide
-
negate
-
absolute
-
convert
Convert the quantity to another unit.- Parameters:
quantity
- Unit of this quantity to convert to.- Returns:
- Converted quantity.
-
convert
Convert the quantity to another unit.- Parameters:
quantity
- Unit of this quantity to convert to.decimals
- Number of decimals to be used in the final result (Negative numbers will be ignored).- Returns:
- Converted quantity.
-
convert
Convert the quantity to another unit.- Parameters:
u
- Unit.- Returns:
- Converted quantity.
-
convertValue
Get the value of this quantity if converted to the given unit.- Parameters:
u
- Unit.- Returns:
- Value.
-
convert
Convert the quantity to another unit rounded to the given number of decimals.- Parameters:
u
- Unit.decimals
- Number of decimals to be used in the final result (Negative numbers will be ignored).- Returns:
- Converted quantity.
-
round
Round the quantity to the specified number of decimals.- Parameters:
decimals
- Number of decimals (Negative numbers will be ignored).- Returns:
- Converted quantity.
-
equals
-
equalsWhenZero
Check if this quantity is equal to another quantity when both are zero, but their units are different.Note: Some measurements such as
Temperature
are not equal when units are different.- Parameters:
another
- Another quantity.- Returns:
- True if equal.
-
isGreaterThan
Check if this is greater than 'another'.- Parameters:
another
- Value to compare.- Returns:
- True if this is greater.
-
isGreaterThan
public boolean isGreaterThan(long value) Check if this is greater than 'value'.- Parameters:
value
- Value to compare.- Returns:
- True if this is greater.
-
isLessThan
Check if this is less than 'another'.- Parameters:
another
- Value to compare.- Returns:
- True if this is less.
-
isLessThan
public boolean isLessThan(long value) Check if this is less than 'value'.- Parameters:
value
- Value to compare.- Returns:
- True if this is less.
-
isGreaterThanOrEqual
Check if this is greater than or equal to 'another'.- Parameters:
another
- Value to compare.- Returns:
- True if this is greater than or equal.
-
isGreaterThanOrEqual
public boolean isGreaterThanOrEqual(long value) Check if this is greater than or equal to 'value'.- Parameters:
value
- Value to compare.- Returns:
- True if this is greater or equal.
-
isLessThanOrEqual
Check if this is less than or equal to 'another'.- Parameters:
another
- Value to compare.- Returns:
- True if this is less than or equal.
-
isLessThanOrEqual
public boolean isLessThanOrEqual(long value) Check if this is less than or equal to 'value'.- Parameters:
value
- Value to compare.- Returns:
- True if this is less than or equal.
-
isZero
public boolean isZero()Checks if this is zero or not.- Returns:
- True if zero.
-
isPositive
public boolean isPositive()Checks if this is a positive non-zero quantity or not.- Returns:
- True if non-zero and positive.
-
isNegative
public boolean isNegative()Checks if this is a negative quantity or not.- Returns:
- True if negative.
-
hashCode
-
toString
-
toString
Create the formatted representation of the quantity value with appropriate decimals places.- Parameters:
decimals
- Number of decimals.- Returns:
- The formatted string representation of the quantity.
-
toString
Create the formatted representation of the quantity value with appropriate decimals places.- Parameters:
showSymbol
- Determines whether unit symbol should be included or not.- Returns:
- The formatted string representation of the quantity.
-
toString
Create the formatted representation of the quantity value with appropriate decimals places.- Parameters:
showSymbol
- Determines whether unit symbol should be included or not.decimals
- Number of decimals.- Returns:
- The formatted string representation of the quantity.
-
getStorableValue
Description copied from interface:Storable
Get the storable value. By default,Object.toString()
is returned.- Specified by:
getStorableValue
in interfaceStorable
- Returns:
- Storable value as a string.
-
compareTo
- Specified by:
compareTo
in interfaceComparable<Quantity>
-
types
-
quantityTypes
-