edu.stanford.hci.r3.units
Class Units

java.lang.Object
  extended by edu.stanford.hci.r3.units.Units
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
Centimeters, Feet, Inches, Meters, Millimeters, PatternDots, Percentage, Pixels, Points, PrinterDots

public abstract class Units
extends java.lang.Object
implements java.lang.Cloneable

Allows fluid conversion between length measurements in different units.

This software is distributed under the BSD License.


Field Summary
static double POINTS_PER_INCH
          Java handles everything in 1/72nd of an inch (1 point).
 
Constructor Summary
protected Units(double val)
          The only time you can set the value.
  Units(double val, java.lang.String name)
          Used when you want to create (and name) an anonymous unit.
 
Method Summary
static Units add(Units a, Units b)
           
protected  Units clone()
           
 boolean equals(java.lang.Object o)
          Check if the lengths match.
 double getConversionTo(Units destUnits)
           
 Units getCopy()
          Returns a copy of this unit.
protected abstract  double getNumberOfUnitsInOneInch()
           
 java.lang.String getUnitName()
           
 Units getUnitsObjectOfSameLengthIn(Units destUnits)
          Gets a new Units object of the same type as destUnits, with the same physical length as this object.
 Units getUnitsObjectOfSameTypeWithValue(double val)
          Gets a new Units object of the same type as this unit, but with a new value.
 double getValue()
           
 double getValueIn(Units destUnits)
          Gets the value in a new unit.
 double getValueInCentimeters()
           
 double getValueInInches()
          This is a CONVENIENCE method.
 double getValueInMillimeters()
           
 double getValueInPatternDots()
          CONVENIENCE method for converting this unit to PatternDots.
 double getValueInPixels()
          CONVENIENCE method for converting to Pixels.
 double getValueInPoints()
          CONVENIENCE method for converting this unit to Points.
static Units subtract(Units a, Units b)
           
 Inches toInches()
          Get an Inches object representing the same physical length.
 Pixels toPixels()
           
 Points toPoints()
          Get a Points object representing the same physical length.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

POINTS_PER_INCH

public static final double POINTS_PER_INCH
Java handles everything in 1/72nd of an inch (1 point). We should do the same to be compatible.

See Also:
Constant Field Values
Constructor Detail

Units

protected Units(double val)
The only time you can set the value. A Units object should be immutable.

Parameters:
val -

Units

public Units(double val,
             java.lang.String name)
Used when you want to create (and name) an anonymous unit. This is useful in situations where you need a temporary unit.

Parameters:
val -
name -
Method Detail

add

public static Units add(Units a,
                        Units b)
Parameters:
a -
b -
Returns:
a unit object of type a representing a+b

subtract

public static Units subtract(Units a,
                             Units b)
Parameters:
a -
b -
Returns:
a Unit object of type a representing a-b

clone

protected Units clone()
Overrides:
clone in class java.lang.Object
See Also:
Object.clone()

equals

public boolean equals(java.lang.Object o)
Check if the lengths match.

Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)

getConversionTo

public double getConversionTo(Units destUnits)
Parameters:
destUnits -
Returns:
the number of destination Units that fit into this Unit. Alternatively, the number n that we would need to multiply the current value to in order to achieve the same length in the destination unit. For example, if we are one inch, and the destination is Points, then the number we need is 72, because we would have to multiply our value (1) by 72 to achieve 72 points, which is the same physical length.

getCopy

public Units getCopy()
Returns a copy of this unit. WARNING: If you have a unit that depends on other factors, you should override this method. This is particularly true for Screen Pixels and Printer Dots. TODO (Implement override for Pixels and PrinterDots)

Returns:

getNumberOfUnitsInOneInch

protected abstract double getNumberOfUnitsInOneInch()
Returns:
how many of these units fit in one inch.

getUnitName

public java.lang.String getUnitName()
Returns:
Inches, Points, Pixels...

getUnitsObjectOfSameLengthIn

public Units getUnitsObjectOfSameLengthIn(Units destUnits)
Gets a new Units object of the same type as destUnits, with the same physical length as this object.

Parameters:
destUnits -
Returns:

getUnitsObjectOfSameTypeWithValue

public Units getUnitsObjectOfSameTypeWithValue(double val)
Gets a new Units object of the same type as this unit, but with a new value.

Parameters:
val -
Returns:

getValue

public double getValue()
Returns:
the numerical value of this unit measure.

getValueIn

public double getValueIn(Units destUnits)
Gets the value in a new unit. For example, you have a Units object that represents 2 Inches. You want to see what the value will be in Points. getConversionTo(new Points()) will return 72. 2 * 72 is 144.

Parameters:
destUnits -
Returns:

getValueInCentimeters

public double getValueInCentimeters()
Returns:

getValueInInches

public double getValueInInches()
This is a CONVENIENCE method. Yes, Yes... it's probably poor programming style to have the Units class be aware of its subclass (Inches). However, it makes code a lot more readable. Plus, if you implement your own Units class, you can use the generic method getValueIn(Units).

Returns:
the double value of this unit, converted to Inches.

getValueInMillimeters

public double getValueInMillimeters()
Returns:

getValueInPatternDots

public double getValueInPatternDots()
CONVENIENCE method for converting this unit to PatternDots.

Returns:

getValueInPixels

public double getValueInPixels()
CONVENIENCE method for converting to Pixels. This uses the DEFAULT pixelsPerInch as specified by the configuration xml file. Unfortunately, Java cannot detect this value yet.

Returns:
the value after converting to pixels.

getValueInPoints

public double getValueInPoints()
CONVENIENCE method for converting this unit to Points.

Returns:

toInches

public Inches toInches()
Get an Inches object representing the same physical length.

Returns:

toPixels

public Pixels toPixels()
Returns:

toPoints

public Points toPoints()
Get a Points object representing the same physical length.

Returns:

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

Copyright 2006 Stanford University