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 MeasurementUnitstatic final BigDecimalstatic final MathContext -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedQuantity(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 quantitybooleancanConvert(MeasurementUnit toUnit) booleancanConvert(MeasurementUnit toUnit, String message) booleancanConvert(Quantity another) booleancanConvert(Quantity another, String message) intConvert 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 Quantitycreate(double quantity, MeasurementUnit unit) static <Q extends Quantity>
Qstatic <Q extends Quantity>
Qstatic Quantitycreate(MeasurementUnit unit) static <Q extends Quantity>
Qstatic <Q extends Quantity>
Qstatic QuantityConstruct a quantity from a string representation.static <Q extends Quantity>
Qstatic <Q extends Quantity>
Qstatic Quantitycreate(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 booleanprotected booleanequalsWhenZero(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 intgetUnit()static MeasurementUnitstatic MeasurementUnitgetValue()Get the quantity as BigDecimal.inthashCode()booleanisCompatible(Quantity another) booleanisConvertible(MeasurementUnit toUnit) booleanisConvertible(Quantity another) booleanisGreaterThan(long value) Check if this is greater than 'value'.booleanisGreaterThan(Quantity another) Check if this is greater than 'another'.booleanisGreaterThanOrEqual(long value) Check if this is greater than or equal to 'value'.booleanisGreaterThanOrEqual(Quantity another) Check if this is greater than or equal to 'another'.booleanisLessThan(long value) Check if this is less than 'value'.booleanisLessThan(Quantity another) Check if this is less than 'another'.booleanisLessThanOrEqual(long value) Check if this is less than or equal to 'value'.booleanisLessThanOrEqual(Quantity another) Check if this is less than or equal to 'another'.booleanChecks if this is a negative quantity or not.booleanChecks if this is a positive non-zero quantity or not.booleanisZero()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.voidsetUnit(MeasurementUnit unit) voidSubtract quantitySubtract quantitysubtract(BigDecimal quantity) Subtract quantitystatic QuantitySum 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
Temperatureare 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:StorableGet the storable value. By default,Object.toString()is returned.- Specified by:
getStorableValuein interfaceStorable- Returns:
- Storable value as a string.
-
compareTo
- Specified by:
compareToin interfaceComparable<Quantity>
-
types
-
quantityTypes
-