com.pcauto.util
Class Weight

java.lang.Object
  extended bycom.pcauto.util.Quantity
      extended bycom.pcauto.util.Weight
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable

public class Weight
extends Quantity

Weight objects used to hold weight information. Objects can only be instantiated using the WeightFormat class. A Weight 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 Weight objects will be printed using the overrided toString() method.

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

See Also:
Serialized Form

Field Summary
protected static WeightUnits baseUnit
           
static Weight ZERO
          Instance of Weight with zero value.
 
Fields inherited from class com.pcauto.util.Quantity
quantity, unit
 
Constructor Summary
protected Weight(double quantity, WeightUnits unit)
          Creates new Weight with an initial value double value and WeightUnits unit.
 
Method Summary
 Weight add(Weight quant)
          Adds this Weight to the supplied Weight.
static Weight add(Weight a, Weight b)
          Adds supplied Weight to other supplied Weight.
static boolean areEqual(Weight quant1, Weight quant2)
          Determines if two Weight objects passed to the fuction are equal.
 java.lang.Object clone()
          Creates new Weight using an instance of Weight
 int compareTo(Weight quant)
          Compares this Weight with the specified Weight.
 Weight divide(java.math.BigDecimal divisor)
          Divides this Weight by supplied BigDecimal.
 java.math.BigDecimal divide(Weight quant)
          Divides this Weight by supplied Weight.
 boolean equals(Weight quant)
          Compares this Weight to the specified Weight.
static WeightUnits getBaseUnit()
          Gets base unit with which this weight is printed.
 boolean greaterThan(Weight quant)
          Compares this Weight to the specified Weight.
 boolean lessThan(Weight quant)
          Compares this Weight to the specified Weight.
 Weight multiply(java.math.BigDecimal multiplier)
          Multiplies this Weight by supplied BigDecimal.
static void setBaseUnit(WeightUnits unit)
          Sets the "toString" representation of any Weight objects to be of type unit.
 Weight subtract(Weight quant)
          Subtracts supplied Weight from this Weight.
 java.lang.String toString()
          Returns String representation of quantity of Quantity object in baseUnit.
 
Methods inherited from class com.pcauto.util.Quantity
add, add, areEqual, compareTo, compareTo, divide, equals, equals, getQuantity, getUnit, greaterThan, lessThan, myDivide, myMultiply, setQuantity, setUnit, setZERO, subtract
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

baseUnit

protected static WeightUnits baseUnit

ZERO

public static final Weight ZERO
Instance of Weight with zero value.

Constructor Detail

Weight

protected Weight(double quantity,
                 WeightUnits unit)
Creates new Weight with an initial value double value and WeightUnits unit.

Parameters:
quantity - double weight
unit - int
Method Detail

setBaseUnit

public static void setBaseUnit(WeightUnits unit)
Sets the "toString" representation of any Weight objects to be of type unit.


getBaseUnit

public static WeightUnits getBaseUnit()
Gets base unit with which this weight is printed.

Returns:
WeightUnits unit with which weights are printed.

clone

public java.lang.Object clone()
Creates new Weight using an instance of Weight

Specified by:
clone in class Quantity
Returns:
new Quantity object

equals

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

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

compareTo

public int compareTo(Weight quant)
Compares this Weight with the specified Weight. Null parameter returns 1.

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

greaterThan

public boolean greaterThan(Weight quant)
Compares this Weight to the specified Weight.

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

lessThan

public boolean lessThan(Weight quant)
Compares this Weight to the specified Weight.

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

areEqual

public static boolean areEqual(Weight quant1,
                               Weight quant2)
Determines if two Weight objects passed to the fuction are equal. Two null are considered equal.

Returns:
true if equal, false otherwise

add

public Weight add(Weight quant)
Adds this Weight to the supplied Weight. Null parameter is considered equal to Weight with ZERO quantity.

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

add

public static Weight add(Weight a,
                         Weight b)
Adds supplied Weight to other supplied Weight. Null parameter(s) is considered to be equal to ZERO quantity.

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

subtract

public Weight subtract(Weight quant)
Subtracts supplied Weight from this Weight. Null parameter is considered equal to Weight with zero quantity

Parameters:
quant - Weight object to subtract
Returns:
Weight object resulting from subtraction operation in this unit

divide

public java.math.BigDecimal divide(Weight quant)
Divides this Weight by supplied Weight.

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

multiply

public Weight multiply(java.math.BigDecimal multiplier)
Multiplies this Weight by supplied BigDecimal.

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

divide

public Weight divide(java.math.BigDecimal divisor)
Divides this Weight by supplied BigDecimal.

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

toString

public java.lang.String toString()
Returns String representation of quantity of Quantity object in baseUnit.

Returns:
String