UnitTerm Class Reference

Representation of a term in a unit, expressed in terms of a scalefactor relative to a BaseUnit, and the power to which that base unit is raised. More...

List of all members.


Public Member Functions

 UnitTerm (BaseUnit baseunit, SiPrefix prefix, Integer power)
 Constructor.
 UnitTerm (UnitTerm otherterm)
 Copy constructor.
 UnitTerm (String termstr) throws HDUnitException
 Constructor from string.
UnitTerm fromString (String termstr) throws HDUnitException
 Build from a string.
Double convFactor ()
 Multiplicative conversion factor between the base unit and this term's derived unit.
String toString ()
 String representation.
boolean equals (Object obj)

Public Attributes

BaseUnit baseunit
 Base unit around which this term is constructed.
SiPrefix prefix
 SI prefix of the conversion factor.
Integer power
 What power the base unit is raised to in this term.

Static Package Attributes

static Logger log = Logger.getLogger(UnitTerm.class)

Detailed Description

Representation of a term in a unit, expressed in terms of a scalefactor relative to a BaseUnit, and the power to which that base unit is raised.

As it is just a handy container, we'll break with nice object privacy rules and make the three data fields public.

Definition at line 15 of file UnitTerm.java.


Constructor & Destructor Documentation

UnitTerm ( BaseUnit  baseunit,
SiPrefix  prefix,
Integer  power 
)

Constructor.

Definition at line 32 of file UnitTerm.java.

00032                                                                        {
00033         this.baseunit = baseunit;
00034         this.prefix = prefix;
00035         this.power = power;
00036     }

UnitTerm ( UnitTerm  otherterm  ) 

Copy constructor.

Definition at line 39 of file UnitTerm.java.

References UnitTerm.baseunit, UnitTerm.power, and UnitTerm.prefix.

00039                                         {
00040         this.baseunit = otherterm.baseunit;
00041         this.prefix = otherterm.prefix;
00042         this.power = otherterm.power;
00043     }

UnitTerm ( String  termstr  )  throws HDUnitException

Constructor from string.

Definition at line 46 of file UnitTerm.java.

References UnitTerm.fromString().

00046                                                            {
00047         fromString(termstr);
00048     }


Member Function Documentation

UnitTerm fromString ( String  termstr  )  throws HDUnitException

Build from a string.

Definition at line 53 of file UnitTerm.java.

References UnitTerm.baseunit, UnitTerm.log, UnitTerm.power, and UnitTerm.prefix.

Referenced by UnitTerm.UnitTerm().

00053                                                                       {
00054         prefix = null;
00055         baseunit = null;
00056         power = null;
00057         for (BaseUnit bu : BaseUnit.values()) {
00058             String baseunitMatches = "("
00059                 + bu.toString() + "|"
00060                 + bu.toString().toUpperCase() + "|"
00061                 + bu.toString().toLowerCase() + ")";
00062             String regex = "\\A(/?)" // division indicator
00063                 + "([YZEPTGMkmunpfazy]?)" // SI prefix patterns
00064                 + baseunitMatches // base unit patterns
00065                 + "(|[-+]\\d+|[E\\^][-+]?\\d+|\\*\\*[-+]?\\d+)\\Z"; // exponent patterns
00066             log.debug(regex);
00067             Pattern p = Pattern.compile(regex);
00068             Matcher m = p.matcher(termstr);
00069             if (m.matches()) {
00070                 // Base unit
00071                 baseunit = bu;
00072                 // SI prefix
00073                 final String prefixstr = m.group(2);
00074                 for (SiPrefix testprefix : SiPrefix.values()) {
00075                     //log.trace(prefixstr + " vs. " + testprefix.toString());
00076                     if (testprefix.toString().equals(prefixstr)) {
00077                         prefix = testprefix;
00078                         break;
00079                     }
00080                 }
00081 
00082                 // Power
00083                 String exppart = m.group(4).replaceAll("[\\*\\^E]", "");
00084                 if (exppart.length() == 0) exppart = "1";
00085                 try {
00086                     power = Integer.parseInt(exppart);
00087                 } catch (Exception e) {
00088                     String err = "Could not build a valid unit term from '" + termstr + "'. ";
00089                     err += "Problem with exponent part '" + exppart + "'.";
00090                     throw new HDUnitException(err);
00091                 }
00092                 // Finally, invert the power if this was a division term
00093                 if (m.group(1).length() != 0) {
00094                     power = -power;
00095                 }
00096 
00097                 // Stop looping over base units
00098                 break;
00099             }
00100         }
00101         log.debug(prefix + " " + baseunit + " " + power);
00102         if (prefix == null || baseunit == null || power == null) {
00103             throw new HDUnitException("Could not build a valid unit term from '" + termstr + "'");
00104         }
00105         log.info(termstr + " -> [" + prefix.toString() + ", " + baseunit.toString() + ", " + power + "]");
00106         return this;
00107     }

Double convFactor (  ) 

Multiplicative conversion factor between the base unit and this term's derived unit.

In other words, how many base units make up one of the units represented in this term. Calculated as 10**(convpower*power).

Definition at line 112 of file UnitTerm.java.

References UnitTerm.power, and UnitTerm.prefix.

00112                                {
00113         return Math.pow(10.0, new Double(prefix.powerOf10() * power));
00114     }

String toString (  ) 

String representation.

Add the power if required

Definition at line 117 of file UnitTerm.java.

References UnitTerm.baseunit, UnitTerm.power, and UnitTerm.prefix.

00117                              {
00118         StringBuffer s = new StringBuffer();
00119         s.append(this.prefix.toString());
00120         s.append(this.baseunit.toString());
00122         if (this.power != 1) {
00123             s.append("^" + this.power);
00124         }
00125         return s.toString();
00126     }

boolean equals ( Object  obj  ) 

Definition at line 129 of file UnitTerm.java.

References UnitTerm.baseunit, UnitTerm.power, and UnitTerm.prefix.

00129                                       {
00130         if (this == obj) return true;
00131         if (! (obj instanceof UnitTerm)) return false;
00132         UnitTerm test = (UnitTerm) obj;
00133         if (! test.baseunit.equals(this.baseunit) ) return false;
00134         if (! test.prefix.equals(this.prefix) ) return false;
00135         if (! test.power.equals(this.power) ) return false;
00136         return true;
00137     }


Member Data Documentation

Base unit around which this term is constructed.

Definition at line 17 of file UnitTerm.java.

Referenced by UnitTerm.equals(), UnitTerm.fromString(), UnitTerm.toString(), and UnitTerm.UnitTerm().

SI prefix of the conversion factor.

Definition at line 20 of file UnitTerm.java.

Referenced by Unit.canonicalUnit(), UnitTerm.convFactor(), UnitTerm.equals(), UnitTerm.fromString(), UnitTerm.toString(), and UnitTerm.UnitTerm().

Integer power

What power the base unit is raised to in this term.

The overall base unit conversion factor for a term is hence (10**convpower)**power.

Definition at line 24 of file UnitTerm.java.

Referenced by UnitTerm.convFactor(), UnitTerm.equals(), UnitTerm.fromString(), UnitTerm.toString(), and UnitTerm.UnitTerm().

Logger log = Logger.getLogger(UnitTerm.class) [static, package]

Definition at line 27 of file UnitTerm.java.

Referenced by UnitTerm.fromString().


The documentation for this class was generated from the following file:


Generated on Tue Apr 21 15:54:56 2009 for HepData common classes by  doxygen 1.5.5