package net.mattmccutchen.measurements; import java.util.*; import net.mattmccutchen.addins.*; import com.sun.star.lang.*; import com.sun.star.registry.*; public class MeasurementsAddIn extends AddInBase implements XMeasurementsAddIn { private static final AddInHelper ah = new AddInHelper( MeasurementsAddIn.class.getName(), // implementation name MeasurementsAddIn.class.getName() // specific service name ); public static XSingleServiceFactory __getServiceFactory(String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey) { return ah.staticGetServiceFactory(implName, multiFactory, regKey); } public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) { return ah.staticWriteRegistryServiceInfo(regKey); } private static final List fis = Arrays.asList(new FunctionInfo[] { new FunctionInfo("mneg", "mneg", "Negates a measurement.", Arrays.asList(new ArgumentInfo("m", "Measurement"))), new FunctionInfo("madd", "madd", "Adds two measurements.", Arrays.asList(new ArgumentInfo("a", "First term"), new ArgumentInfo("b", "Second term"))), new FunctionInfo("msub", "msub", "Subtracts two measurements.", Arrays.asList(new ArgumentInfo("a", "Positive term"), new ArgumentInfo("b", "Negative term"))), new FunctionInfo("mmul", "mmul", "Multiplies two measurements.", Arrays.asList(new ArgumentInfo("a", "First factor"), new ArgumentInfo("b", "Second factor"))), new FunctionInfo("mdiv", "mdiv", "Divides two measurements.", Arrays.asList(new ArgumentInfo("num", "Numerator"), new ArgumentInfo("denom", "Denominator"))), new FunctionInfo("mpowint", "mpowint", "Raises a measurement to an integer power.", Arrays.asList(new ArgumentInfo("base", "Base"), new ArgumentInfo("exp", "Exponent"))), new FunctionInfo("mrootint", "mrootint", "Takes an integer root of a measurement. Does not allow fractional powers of units in the result.", Arrays.asList(new ArgumentInfo("base", "Base"), new ArgumentInfo("root", "Root (e.g., 2 for square root)"))), new FunctionInfo("mpow", "mpow", "Raises one measurement to the power of another. Both must be pure numbers.", Arrays.asList(new ArgumentInfo("base", "Base"), new ArgumentInfo("exp", "Exponent"))), new FunctionInfo("mexp", "mexp", "Raises e (2.718...) to the power of a measurement.", Arrays.asList(new ArgumentInfo("m", "Measurement"))), new FunctionInfo("mln", "mln", "Takes the natural logarithm of a measurement. " + "The measurement must be a pure number, so you may have to rewrite a difference of logarithms as a logarithm of a quotient.", Arrays.asList(new ArgumentInfo("m", "Measurement"))), new FunctionInfo("mcmp", "mcmp", "Returns the difference between two measurements, expressed in units of the sum of their uncertainties. " + "You can compare measurements very flexibly by checking the result against a tolerance.", Arrays.asList(new ArgumentInfo("a", "First measurement"), new ArgumentInfo("b", "Second measurement"))), new FunctionInfo("mcleanstr", "mcleanstr", "Formats the measurement to a string without including the code in [...].", Arrays.asList(new ArgumentInfo("m", "Measurement string"))), new FunctionInfo("munwrap", "munwrap", "Unwraps a measurement (which must have no units) into a floating-point number with all available precision, regardless of the measurement's uncertainty.", Arrays.asList(new ArgumentInfo("m", "Measurement string"))), new FunctionInfo("mstras", "mstras", "Formats the measurement to a string in the requested unit.", Arrays.asList(new ArgumentInfo("m", "Measurement string"), new ArgumentInfo("u", "Unit string"))), new FunctionInfo("mcleanstras", "mcleanstras", "Formats the measurement to a string in the requested unit without including the code in [...].", Arrays.asList(new ArgumentInfo("m", "Measurement string"), new ArgumentInfo("u", "Unit string"))), }); // We can be instantiated reflectively!!! public MeasurementsAddIn() { super(ah, fis); } // The actual functions!!! // Math public String mneg(String m) { return Measurement.format(MeasurementMath.neg( Measurement.parseCode(m)), true); } public String madd(String a, String b) { return Measurement.format(MeasurementMath.add( Measurement.parseCode(a), Measurement.parseCode(b)), true); } public String msub(String a, String b) { return Measurement.format(MeasurementMath.sub( Measurement.parseCode(a), Measurement.parseCode(b)), true); } public String mmul(String a, String b) { return Measurement.format(MeasurementMath.mul( Measurement.parseCode(a), Measurement.parseCode(b)), true); } public String mdiv(String a, String b) { return Measurement.format(MeasurementMath.div( Measurement.parseCode(a), Measurement.parseCode(b)), true); } public String mpowint(String a, String bstr) { int b = Integer.parseInt(bstr); return Measurement.format(MeasurementMath.powint( Measurement.parseCode(a), b), true); } public String mrootint(String a, String bstr) { int b = Integer.parseInt(bstr); return Measurement.format(MeasurementMath.rootint( Measurement.parseCode(a), b), true); } public String mpow(String a, String b) { return Measurement.format(MeasurementMath.pow( Measurement.parseCode(a), Measurement.parseCode(b)), true); } public String mexp(String m) { return Measurement.format(MeasurementMath.exp( Measurement.parseCode(m)), true); } public String mln(String m) { return Measurement.format(MeasurementMath.ln( Measurement.parseCode(m)), true); } public double mcmp(String a, String b) { return MeasurementMath.cmp( Measurement.parseCode(a), Measurement.parseCode(b)); } // Utility public String mcleanstr(String mstr) { Measurement m = Measurement.parseCode(mstr); return Measurement.format(m, false); } public double munwrap(String mstr) { Measurement m = Measurement.parseCode(mstr); if (m == null || !MeasurementMath.isPureNumber(m)) return Double.NaN; return m.number; } private String mstras1(String mstr, String ustr, boolean withCode) { Measurement m = Measurement.parseCode(mstr); if (m == null) return Measurement.errorIndicator; Unit u; try { u = Unit.parseUnitString(ustr); } catch (Exception e) { return Measurement.errorIndicator; } return Measurement.formatInUnit(m, u, withCode); } public String mstras(String mstr, String ustr) { return mstras1(mstr, ustr, true); } public String mcleanstras(String mstr, String ustr) { return mstras1(mstr, ustr, false); } }