ptolemy.moml.unit
Class Unit

java.lang.Object
  extended by ptolemy.moml.unit.Unit
All Implemented Interfaces:
UnitPresentation

public class Unit
extends java.lang.Object
implements UnitPresentation

Class that contains the internal representation of a Unit. A Unit has the mathematical notation S<E1, E2, ..., En> where S is the scale and <E1, E2, ..., En> is the type of the Unit.

This class also contains methods for operating on Units, such as multiply, divide, etc.

Since:
Ptolemy II 8.0
Version:
$Id: Unit.java 57044 2010-01-27 22:41:05Z cxh $
Author:
Rowland R Johnson
Accepted Rating:
Red (rowland)
Proposed Rating:
Red (rowland)

Field Summary
(package private)  java.util.Vector _labels
           
private static int _noLabelCounter
           
private  double _scale
           
(package private)  int[] _type
           
 
Constructor Summary
Unit()
          Create a Unit with no name and the unitless type.
Unit(BaseUnit bu)
          Create a Unit from a BaseUnit.
Unit(java.lang.String name)
          Create a Unit with a specified name, and the unitless type.
 
Method Summary
protected  void _setLabels(java.util.Vector labels)
          Set the labels of the Unit.
 Unit copy()
          Make a copy of this Unit.
 java.lang.String descriptiveForm()
          The expression of the Unit that is commonly used by humans.
 Unit divideBy(Unit divisor)
          Divide this Unit by another Unit.
 boolean equals(java.lang.Object object)
          Return True if this Unit equals another object
 UnitExpr factor()
          Factor a Unit into a UnitExpr that has UnitTerms that are in the Library.
 java.util.Vector getLabels()
          Get the labels for a Unit.
 java.lang.String getLabelsString()
          Create a String that is the concatenation of all the labels.
 java.lang.String getPrimaryLabel()
          Get the primary label of a Unit.
 double getScale()
          Get the scale.
 int[] getType()
          Get the type (represented as a int array) of this Unit.
 int hashCode()
          Return a hash code value for this Unit.
 boolean hasSameType(Unit otherUnit)
          Return true if the Unit has the same type as another Unit.
 Unit invert()
          Invert this Unit.
 Unit multiplyBy(Unit multiplicand)
          Multiply this Unit by another Unit.
 Unit pow(double power)
          Returns of value of this Unit raised to the power of the argument.
 void setPrimaryLabel(java.lang.String label)
          Set the primary label.
 void setScale(double d)
          Set the scale.
 void setType(int[] type)
          Set the type.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

_labels

java.util.Vector _labels

_noLabelCounter

private static int _noLabelCounter

_scale

private double _scale

_type

int[] _type
Constructor Detail

Unit

public Unit()
Create a Unit with no name and the unitless type.


Unit

public Unit(BaseUnit bu)
Create a Unit from a BaseUnit.

Parameters:
bu - BaseUnit that provides the basis for this Unit.

Unit

public Unit(java.lang.String name)
Create a Unit with a specified name, and the unitless type.

Parameters:
name - Name of the Unit.
Method Detail

copy

public Unit copy()
Make a copy of this Unit.

Returns:
A new Unit.

descriptiveForm

public java.lang.String descriptiveForm()
The expression of the Unit that is commonly used by humans. For example, the unit 4.1868E7<2, 1, -3, 0, 0> will produce the common expression "calorie second^-1".

Specified by:
descriptiveForm in interface UnitPresentation
Returns:
The descriptive form.
See Also:
UnitPresentation.descriptiveForm()

divideBy

public Unit divideBy(Unit divisor)
Divide this Unit by another Unit.

Parameters:
divisor - The divisor unit.
Returns:
This Unit divided by the divisor.

equals

public boolean equals(java.lang.Object object)
Return True if this Unit equals another object

Overrides:
equals in class java.lang.Object
Parameters:
object - The object to be compared against.
Returns:
True if this Unit equals the other Unit. Return false if the other object is null or not an instance of Unit.

factor

public UnitExpr factor()
Factor a Unit into a UnitExpr that has UnitTerms that are in the Library. In general, factorization is a lengthy process, and this method is not a complete factorization algorithm. It only tries some of the more likely combinations.

Returns:
UnitExpr that is equivalent to to the Unit.

getLabels

public java.util.Vector getLabels()
Get the labels for a Unit.

Returns:
The labels.
See Also:
getPrimaryLabel()

getLabelsString

public java.lang.String getLabelsString()
Create a String that is the concatenation of all the labels.

Returns:
The concatenation of the labels.

getPrimaryLabel

public java.lang.String getPrimaryLabel()
Get the primary label of a Unit. A Unit can have more than one label. For example, cm, and centimeter are labels for the same Unit. There always exists a label that is primary.

Returns:
The primary label.

getScale

public double getScale()
Get the scale.

Returns:
Scale.

getType

public int[] getType()
Get the type (represented as a int array) of this Unit.

Returns:
The type (represented as a int array) of this Unit.

hashCode

public int hashCode()
Return a hash code value for this Unit. This method returns the bitwise xor of the hashCode of the label String, the categories and the hashCode() of the scale.

Overrides:
hashCode in class java.lang.Object
Returns:
A hash code value for this Unit.

hasSameType

public boolean hasSameType(Unit otherUnit)
Return true if the Unit has the same type as another Unit.

Parameters:
otherUnit -
Returns:
True if the Unit has the same type as the argument.

invert

public Unit invert()
Invert this Unit.

Returns:
The inverse of this Unit.

multiplyBy

public Unit multiplyBy(Unit multiplicand)
Multiply this Unit by another Unit.

Parameters:
multiplicand -
Returns:
The product of this Unit multiplied by the argument.

pow

public Unit pow(double power)
Returns of value of this Unit raised to the power of the argument.

Parameters:
power - The exponent.
Returns:
This Unit raised to the power of the argument.

setPrimaryLabel

public void setPrimaryLabel(java.lang.String label)
Set the primary label.

Parameters:
label - The primary label.

setScale

public void setScale(double d)
Set the scale.

Parameters:
d - The scale.

setType

public void setType(int[] type)
Set the type.

Parameters:
type -

toString

public java.lang.String toString()
Specified by:
toString in interface UnitPresentation
Overrides:
toString in class java.lang.Object

_setLabels

protected void _setLabels(java.util.Vector labels)
Set the labels of the Unit.

Parameters:
labels - The labels.