com.pcauto.util
Class Quantity

java.lang.Object
  extended bycom.pcauto.util.Quantity
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable
Direct Known Subclasses:
Currency, Density, Dimension, Distance, ElapsedTime, Temperature, Volume, Weight

public abstract class Quantity
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable

Quantity objects used to hold quantity information. Objects can only be instantiated using the QuantityFormat class. A Quantity object's concept of unit is hidden from class users, and instead inferred from the formatter class at instantiation.

The baseUnit is used to store the unit with which Quantity objects will be printed using the overrided toString() method.

A set of arithmetic operations have been included to compare, add, subtract, divide and multiply quantity objects. All arithmetic operations have been designed to be null safe.

See Also:
Serialized Form

Field Summary
protected  double quantity
           
protected  Units unit
           
 
Constructor Summary
protected Quantity()
          Creates new Quantity with an initial value double value and int unit.
protected Quantity(double quantity, Units n_unit)
          Creates new Quantity with an initial value double value and int unit.
 
Method Summary
protected  Quantity add(Quantity quant)
          Adds this Quantity to the supplied Quantity.
protected static Quantity add(Quantity a, Quantity b)
          Adds supplied Quantity to other supplied Quantity.
protected static boolean areEqual(Quantity firstVal, Quantity secondVal)
          Determines if two Quantity objects passed to the fuction are equal.
abstract  java.lang.Object clone()
          Creates new Quantity with an initial value double value and int unit.
 int compareTo(java.lang.Object obj)
          Compares this Quantity with the specified Quantity.
protected  int compareTo(Quantity quant)
          Compares this Quantity with the specified Quantity.
protected  java.math.BigDecimal divide(Quantity quant)
          Divides this Quantity by supplied Quantity.
 boolean equals(java.lang.Object obj)
           
protected  boolean equals(Quantity quant)
          Compares this Quantity to the specified Quantity.
protected  double getQuantity()
          Gets raw numeric value of stored quantity.
protected  Units getUnit()
          Gets unit of this Quantity.
protected  boolean greaterThan(Quantity quant)
          Compares this Quantity to the specified Quantity.
protected  boolean lessThan(Quantity quant)
          Compares this Quantity to the specified Quantity.
protected  Quantity myDivide(java.math.BigDecimal divisor)
          Divides this Quantity by supplied BigDecimal.
protected  Quantity myMultiply(java.math.BigDecimal multiplier)
          Multiplies this Quantity by supplied BigDecimal.
protected  void setQuantity(double quantity)
          Sets quantity of this quantity to numeric value contained by supplied double.
protected  void setUnit(Units unit)
          Sets unit of this Quantity.
protected static void setZERO(Quantity n_zero)
          Sets specific Quantity ZERO implementation for internal use
protected  Quantity subtract(Quantity quant)
          Subtracts supplied Quantity from this Quantity.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

quantity

protected double quantity

unit

protected Units unit
Constructor Detail

Quantity

protected Quantity()
Creates new Quantity with an initial value double value and int unit.


Quantity

protected Quantity(double quantity,
                   Units n_unit)
Creates new Quantity with an initial value double value and int unit.

Parameters:
quantity - double quantity & @param unit Units
Method Detail

clone

public abstract java.lang.Object clone()
Creates new Quantity with an initial value double value and int unit.

Returns:
new Quantity object

setZERO

protected static void setZERO(Quantity n_zero)
Sets specific Quantity ZERO implementation for internal use


getQuantity

protected double getQuantity()
Gets raw numeric value of stored quantity. This numeric value should not be used without consideration of the base unit.

Returns:
double numeric quantity value in base units

setQuantity

protected void setQuantity(double quantity)
Sets quantity of this quantity to numeric value contained by supplied double.


getUnit

protected Units getUnit()
Gets unit of this Quantity.

Returns:
unit Units

setUnit

protected void setUnit(Units unit)
Sets unit of this Quantity.

Parameters:
unit - Units for this Quantity

equals

protected boolean equals(Quantity quant)
Compares this Quantity to the specified Quantity. The result is true if and only if the argument is not null and is a Quantity object that has the same value. null parameter returns false.

Parameters:
quant - Quantity with which to compare
Returns:
true if AbstractQuantities are equivalent; false othewise.

equals

public boolean equals(java.lang.Object obj)

compareTo

public int compareTo(java.lang.Object obj)
Compares this Quantity with the specified Quantity. null parameter returns 1.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
obj - object (must be an Quantity) with which to compare.
Returns:
0 if they are equal. 1 if this Quantity is larger -1 if this Quantity is smaller.

compareTo

protected int compareTo(Quantity quant)
Compares this Quantity with the specified Quantity. null parameter returns 1.

Parameters:
quant - Quantity object with which to compare.
Returns:
0 if they are equal. 1 if this Quantity is larger -1 if this Quantity is smaller.

greaterThan

protected boolean greaterThan(Quantity quant)
Compares this Quantity to the specified Quantity.

Parameters:
quant - Quantity object with which to compare.
Returns:
true if this is greater than quant; false otherwise

lessThan

protected boolean lessThan(Quantity quant)
Compares this Quantity to the specified Quantity.

Parameters:
quant - Quantity object with which to compare.
Returns:
true if this is less than quant; false otherwise

areEqual

protected static boolean areEqual(Quantity firstVal,
                                  Quantity secondVal)
Determines if two Quantity objects passed to the fuction are equal. Two null are considered equal.

Returns:
true if equal, false otherwise

add

protected Quantity add(Quantity quant)
Adds this Quantity to the supplied Quantity. null parameter is considered equal to Quantity with ZERO quantity.

Parameters:
quant - Quantity object to add
Returns:
Quantity object resulting from addition operation

add

protected static Quantity add(Quantity a,
                              Quantity b)
Adds supplied Quantity to other supplied Quantity. null parameter(s) is considered to be equal to ZERO quantity.

Parameters:
a - first Quantity object
b - second Quantity object
Returns:
Quantity object resulting from addition of a and b in Quantity a's unit

subtract

protected Quantity subtract(Quantity quant)
Subtracts supplied Quantity from this Quantity. null parameter is considered equal to Quantity with zero quantity

Parameters:
quant - Quantity object to subtract
Returns:
Quantity object resulting from subtraction operation in Units unit

divide

protected java.math.BigDecimal divide(Quantity quant)
Divides this Quantity by supplied Quantity.

Parameters:
quant - Quantity object to divide by
Returns:
BigDecimal object resulting from division operation
Throws:
ArithmeticException - on the attempt to divide by null or zero

myMultiply

protected Quantity myMultiply(java.math.BigDecimal multiplier)
Multiplies this Quantity by supplied BigDecimal.

Returns:
Quantity object resulting from multiplication operation
Throws:
ArithmeticException - on the attempt to multiply by null

myDivide

protected Quantity myDivide(java.math.BigDecimal divisor)
Divides this Quantity by supplied BigDecimal.

Returns:
Quantity object resulting from division operation
Throws:
ArithmeticException - on the attempt to divide by null or zero