com.pcauto.util
Class Distance

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

public class Distance
extends Quantity

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

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

See Also:
Serialized Form

Field Summary
protected static DistanceUnits baseUnit
           
static Distance ZERO
          Instance of Distance with zero value.
 
Fields inherited from class com.pcauto.util.Quantity
quantity, unit
 
Constructor Summary
protected Distance(double quantity, DistanceUnits unit)
          Creates new Distance with initial value double value and DistanceUnits unit.
 
Method Summary
 Distance add(Distance quant)
          Adds this Distance to the supplied Distance.
static Distance add(Distance a, Distance b)
          Adds supplied Distance to other supplied Distance.
static boolean areEqual(Distance quant1, Distance quant2)
          Determines if two Distance objects passed to the fuction are equal.
 java.lang.Object clone()
          Creates new Distance identical to this Distance.
 int compareTo(Distance quant)
          Compares this Distance with the specified Distance.
 Distance divide(java.math.BigDecimal divisor)
          Divides this Distance by supplied BigDecimal.
 java.math.BigDecimal divide(Distance quant)
          Divides this Distance by supplied Distance.
 boolean equals(Distance quant)
          Compares this Distance to the specified Distance.
static DistanceUnits getBaseUnit()
          Gets base unit with which this Distance is printed.
 boolean greaterThan(Distance quant)
          Compares this Distance to the specified Distance.
 boolean lessThan(Distance quant)
          Compares this Distance to the specified Distance.
 Distance multiply(java.math.BigDecimal multiplier)
          Multiplies this Distance by supplied BigDecimal.
static void setBaseUnit(DistanceUnits unit)
          Sets the "toString" representation of any Distance objects to be of type unit.
 Distance subtract(Distance quant)
          Subtracts supplied Distance from this Distance.
 
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, toString, wait, wait, wait
 

Field Detail

baseUnit

protected static DistanceUnits baseUnit

ZERO

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

Constructor Detail

Distance

protected Distance(double quantity,
                   DistanceUnits unit)
Creates new Distance with initial value double value and DistanceUnits unit.

Parameters:
quantity - double weight
unit - DistanceUnits
Method Detail

getBaseUnit

public static DistanceUnits getBaseUnit()
Gets base unit with which this Distance is printed.

Returns:
DistanceUnits unit with which Distance objects are printed.

setBaseUnit

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


clone

public java.lang.Object clone()
Creates new Distance identical to this Distance.

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

equals

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

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

compareTo

public int compareTo(Distance quant)
Compares this Distance with the specified Distance. null parameter returns 1.

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

greaterThan

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

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

lessThan

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

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

areEqual

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

Returns:
true if equal, false otherwise

add

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

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

add

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

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

subtract

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

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

divide

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

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

multiply

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

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

divide

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

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