00001 package cedar.hepdata.util;
00002
00003 import java.util.regex.Pattern;
00004 import java.util.regex.Matcher;
00005
00006 import org.apache.log4j.*;
00007
00008
00015 public class UnitTerm {
00017 public BaseUnit baseunit;
00018
00020 public SiPrefix prefix;
00021
00024 public Integer power;
00025
00026
00027 static Logger log = Logger.getLogger(UnitTerm.class);
00028
00030
00032 public UnitTerm(BaseUnit baseunit, SiPrefix prefix, Integer power) {
00033 this.baseunit = baseunit;
00034 this.prefix = prefix;
00035 this.power = power;
00036 }
00037
00039 public UnitTerm(UnitTerm otherterm) {
00040 this.baseunit = otherterm.baseunit;
00041 this.prefix = otherterm.prefix;
00042 this.power = otherterm.power;
00043 }
00044
00046 public UnitTerm(String termstr) throws HDUnitException {
00047 fromString(termstr);
00048 }
00049
00051
00053 public UnitTerm fromString(String termstr) throws HDUnitException {
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(/?)"
00063 + "([YZEPTGMkmunpfazy]?)"
00064 + baseunitMatches
00065 + "(|[-+]\\d+|[E\\^][-+]?\\d+|\\*\\*[-+]?\\d+)\\Z";
00066 log.debug(regex);
00067 Pattern p = Pattern.compile(regex);
00068 Matcher m = p.matcher(termstr);
00069 if (m.matches()) {
00070
00071 baseunit = bu;
00072
00073 final String prefixstr = m.group(2);
00074 for (SiPrefix testprefix : SiPrefix.values()) {
00075
00076 if (testprefix.toString().equals(prefixstr)) {
00077 prefix = testprefix;
00078 break;
00079 }
00080 }
00081
00082
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
00093 if (m.group(1).length() != 0) {
00094 power = -power;
00095 }
00096
00097
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 }
00108
00112 public Double convFactor() {
00113 return Math.pow(10.0, new Double(prefix.powerOf10() * power));
00114 }
00115
00117 public String toString() {
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 }
00127
00128
00129 public boolean equals(Object obj) {
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 }
00138 }