Package ptolemy.moml.unit
Class Unit
- java.lang.Object
-
- 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$
- Author:
- Rowland R Johnson
- Pt.AcceptedRating:
- Red (rowland)
- Pt.ProposedRating:
- Red (rowland)
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 objectUnitExpr
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()
-
-
-
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 interfaceUnitPresentation
- 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 classjava.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 classjava.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 interfaceUnitPresentation
- Overrides:
toString
in classjava.lang.Object
-
_setLabels
protected void _setLabels(java.util.Vector labels)
Set the labels of the Unit.- Parameters:
labels
- The labels.
-
-