net.sourceforge.unit
Class Unit

java.lang.Object
  extended by net.sourceforge.unit.Unit
All Implemented Interfaces:
java.lang.Cloneable

public class Unit
extends java.lang.Object
implements java.lang.Cloneable

A Unit converts from one set of units to another.

Version:
$Name: $, $Revision: 1.3 $
Author:
Fred Gylys-Colwell

Field Summary
protected  double b
           
protected  int[] exp
           
 java.lang.String latex
           
protected  double m
           
protected  java.lang.String name
           
static Unit pure
          The unitless unit, such as radians, percents, or inches per inch.
 
Constructor Summary
protected Unit(java.lang.String s)
          Do not use this, use newUnit(String) instead.
 
Method Summary
 java.lang.Object clone()
           
 java.lang.String definition()
          Return the definition of this unit in terms of SI units.
static java.util.Enumeration enumerate()
          Return an enumeration of all defined units.
 boolean equals(Unit other)
          Return true if this is the same unit as other.
 Unit exponent(double n)
          Create a new unit that is this one raised to the power n.
 Unit exponent(int n)
          Create a new unit that is this one raised to the power n.
 double from(double x)
          Convert from this unit to standard SI units.
 double getB()
          Return the conversion offset (used for debugging).
 double getM()
          Return the conversion scale factor (used for debugging).
 int[] getType()
          Return the expontents relative to primitive units (used for debugging).
static Unit newUnit(java.lang.String s)
          Create, if necessary, a new unit from the given definiton.
static Unit newUnit(java.lang.String name, double m, java.lang.String s)
          Make a unit which is m times the unit s.
static Unit newUnit(java.lang.String name, double m, java.lang.String s, double b)
          Make a unit which is m times the unit s, with an offset of b.
static Unit newUnit(java.lang.String name, double m, Unit u, double b)
          Make a unit which is m times the unit u, with an offset of b.
 Unit product(Unit b)
          Create a new unit that is the product of this one and the other one.
 Unit quotient(Unit b)
          Create a new unit that is the quotient of this one divided by the other one.
 boolean sameType(Unit other)
          Return true if other measures the same thing (length, time, energy, ...).
 double to(double x)
          Convert from standard SI units to this unit.
 java.lang.String toString()
          Returns the name, or if unnamed, the definition of this unit.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected java.lang.String name

latex

public java.lang.String latex

exp

protected int[] exp

m

protected double m

b

protected double b

pure

public static final Unit pure
The unitless unit, such as radians, percents, or inches per inch.

Constructor Detail

Unit

protected Unit(java.lang.String s)
Do not use this, use newUnit(String) instead.

Method Detail

from

public double from(double x)
Convert from this unit to standard SI units.


to

public double to(double x)
Convert from standard SI units to this unit.


toString

public java.lang.String toString()
Returns the name, or if unnamed, the definition of this unit.

Overrides:
toString in class java.lang.Object

getM

public double getM()
Return the conversion scale factor (used for debugging).


getB

public double getB()
Return the conversion offset (used for debugging).


getType

public int[] getType()
Return the expontents relative to primitive units (used for debugging). This returns a copy, so you may modify it without changing the unit.


clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

definition

public java.lang.String definition()
Return the definition of this unit in terms of SI units.


sameType

public boolean sameType(Unit other)
Return true if other measures the same thing (length, time, energy, ...).


equals

public boolean equals(Unit other)
Return true if this is the same unit as other.


newUnit

public static Unit newUnit(java.lang.String s)
                    throws ParseException
Create, if necessary, a new unit from the given definiton. For example, to create killowatt hours, you would use newUnit("kW hr"). If a unit with this name has alread been created it is returned, otherwise a new unit is created.

Throws:
ParseException

newUnit

public static Unit newUnit(java.lang.String name,
                           double m,
                           java.lang.String s)
                    throws ParseException
Make a unit which is m times the unit s. For example, to define feet in terms of inches, use:
        newUnit("ft", 12.0,"in");
        
(It helps to read this as 1 foot equals 12 inches.)

Throws:
ParseException

newUnit

public static Unit newUnit(java.lang.String name,
                           double m,
                           java.lang.String s,
                           double b)
                    throws ParseException
Make a unit which is m times the unit s, with an offset of b.

For example, to define the temperature scale C from K use:

        newUnit("C", 1, "K", -273.15);
        

Note: In order to "do what I mean" for units such as "J/C" or "BTU/F", the offset is only used for units that are pure temperatures, such as "C" or "F". This means that "J/C" is the same as "J/K" or "C^2" is the same as "K^2". That's probably what you want anyway.

Throws:
ParseException

newUnit

public static Unit newUnit(java.lang.String name,
                           double m,
                           Unit u,
                           double b)
Make a unit which is m times the unit u, with an offset of b.


exponent

public Unit exponent(int n)
Create a new unit that is this one raised to the power n. If this is an offset unit, like F or C, then the offset is set to zero.


exponent

public Unit exponent(double n)
              throws UnitMismatchException
Create a new unit that is this one raised to the power n. An exception is throws i fthe resulting unit would have a fractional exponent. However, it is possible to raise something like m^2 to the 0.5 power. If this is an offset unit, like F or C, then the offset is set to zero.

Throws:
UnitMismatchException

product

public Unit product(Unit b)
Create a new unit that is the product of this one and the other one. If this is an offset unit, like F or C, then the offset is set to zero.


quotient

public Unit quotient(Unit b)
Create a new unit that is the quotient of this one divided by the other one. If this is an offset unit, like F or C, then the offset is set to zero.


enumerate

public static java.util.Enumeration enumerate()
                                       throws ParseException
Return an enumeration of all defined units.

Throws:
ParseException