| 1 | package net.mattmccutchen.measurements; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import net.mattmccutchen.addins.*; |
| 6 | |
| 7 | import com.sun.star.lang.*; |
| 8 | import com.sun.star.registry.*; |
| 9 | |
| 10 | public class MeasurementsAddIn extends AddInBase<AddInHelper> |
| 11 | implements XMeasurementsAddIn { |
| 12 | private static final AddInHelper ah = new AddInHelper( |
| 13 | MeasurementsAddIn.class.getName(), // implementation name |
| 14 | MeasurementsAddIn.class.getName() // specific service name |
| 15 | ); |
| 16 | |
| 17 | public static XSingleServiceFactory __getServiceFactory(String implName, |
| 18 | XMultiServiceFactory multiFactory, XRegistryKey regKey) { |
| 19 | return ah.staticGetServiceFactory(implName, multiFactory, regKey); |
| 20 | } |
| 21 | |
| 22 | public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) { |
| 23 | return ah.staticWriteRegistryServiceInfo(regKey); |
| 24 | } |
| 25 | |
| 26 | private static final List<FunctionInfo> fis |
| 27 | = Arrays.asList(new FunctionInfo[] { |
| 28 | new FunctionInfo("mneg", "mneg", "Negates a measurement.", |
| 29 | Arrays.asList(new ArgumentInfo("m", "Measurement"))), |
| 30 | new FunctionInfo("madd", "madd", "Adds two measurements.", |
| 31 | Arrays.asList(new ArgumentInfo("a", "First term"), |
| 32 | new ArgumentInfo("b", "Second term"))), |
| 33 | new FunctionInfo("msub", "msub", "Subtracts two measurements.", |
| 34 | Arrays.asList(new ArgumentInfo("a", "Positive term"), |
| 35 | new ArgumentInfo("b", "Negative term"))), |
| 36 | new FunctionInfo("mmul", "mmul", "Multiplies two measurements.", |
| 37 | Arrays.asList(new ArgumentInfo("a", "First factor"), |
| 38 | new ArgumentInfo("b", "Second factor"))), |
| 39 | new FunctionInfo("mdiv", "mdiv", "Divides two measurements.", |
| 40 | Arrays.asList(new ArgumentInfo("num", "Numerator"), |
| 41 | new ArgumentInfo("denom", "Denominator"))), |
| 42 | new FunctionInfo("mpowint", "mpowint", "Raises a measurement to an integer power.", |
| 43 | Arrays.asList(new ArgumentInfo("base", "Base"), |
| 44 | new ArgumentInfo("exp", "Exponent"))), |
| 45 | new FunctionInfo("mpow", "mpow", "Raises one measurement to the power of another. Both must be pure numbers.", |
| 46 | Arrays.asList(new ArgumentInfo("base", "Base"), |
| 47 | new ArgumentInfo("exp", "Exponent"))), |
| 48 | new FunctionInfo("mcmp", "mcmp", |
| 49 | "Returns the difference between two measurements, expressed in units of the sum of their uncertainties. " + |
| 50 | "You can compare measurements very flexibly by checking the result against a tolerance.", |
| 51 | Arrays.asList(new ArgumentInfo("a", "First measurement"), |
| 52 | new ArgumentInfo("b", "Second measurement"))), |
| 53 | |
| 54 | new FunctionInfo("mcleanstr", "mcleanstr", |
| 55 | "Formats the measurement to a string without including the code in [...].", |
| 56 | Arrays.asList(new ArgumentInfo("m", "Measurement string"))), |
| 57 | new FunctionInfo("munwrap", "munwrap", |
| 58 | "Unwraps a measurement (which must have no units) into a floating-point number with all available precision, regardless of the measurement's uncertainty.", |
| 59 | Arrays.asList(new ArgumentInfo("m", "Measurement string"))), |
| 60 | new FunctionInfo("mstras", "mstras", |
| 61 | "Formats the measurement to a string in the requested unit.", |
| 62 | Arrays.asList(new ArgumentInfo("m", "Measurement string"), |
| 63 | new ArgumentInfo("u", "Unit string"))), |
| 64 | new FunctionInfo("mcleanstras", "mcleanstras", |
| 65 | "Formats the measurement to a string in the requested unit without including the code in [...].", |
| 66 | Arrays.asList(new ArgumentInfo("m", "Measurement string"), |
| 67 | new ArgumentInfo("u", "Unit string"))), |
| 68 | }); |
| 69 | |
| 70 | // We can be instantiated reflectively!!! |
| 71 | public MeasurementsAddIn() { |
| 72 | super(ah, fis); |
| 73 | } |
| 74 | |
| 75 | // The actual functions!!! |
| 76 | |
| 77 | // Math |
| 78 | public String mneg(String m) { |
| 79 | return Measurement.format(MeasurementMath.neg( |
| 80 | Measurement.parseCode(m)), true); |
| 81 | } |
| 82 | public String madd(String a, String b) { |
| 83 | return Measurement.format(MeasurementMath.add( |
| 84 | Measurement.parseCode(a), Measurement.parseCode(b)), true); |
| 85 | } |
| 86 | public String msub(String a, String b) { |
| 87 | return Measurement.format(MeasurementMath.sub( |
| 88 | Measurement.parseCode(a), Measurement.parseCode(b)), true); |
| 89 | } |
| 90 | public String mmul(String a, String b) { |
| 91 | return Measurement.format(MeasurementMath.mul( |
| 92 | Measurement.parseCode(a), Measurement.parseCode(b)), true); |
| 93 | } |
| 94 | public String mdiv(String a, String b) { |
| 95 | return Measurement.format(MeasurementMath.div( |
| 96 | Measurement.parseCode(a), Measurement.parseCode(b)), true); |
| 97 | } |
| 98 | public String mpowint(String a, String bstr) { |
| 99 | int b = Integer.parseInt(bstr); |
| 100 | return Measurement.format(MeasurementMath.powint( |
| 101 | Measurement.parseCode(a), b), true); |
| 102 | } |
| 103 | public String mpow(String a, String b) { |
| 104 | return Measurement.format(MeasurementMath.pow( |
| 105 | Measurement.parseCode(a), Measurement.parseCode(b)), true); |
| 106 | } |
| 107 | public double mcmp(String a, String b) { |
| 108 | return MeasurementMath.cmp( |
| 109 | Measurement.parseCode(a), Measurement.parseCode(b)); |
| 110 | } |
| 111 | |
| 112 | // Utility |
| 113 | public String mcleanstr(String mstr) { |
| 114 | Measurement m = Measurement.parseCode(mstr); |
| 115 | return Measurement.format(m, false); |
| 116 | } |
| 117 | public double munwrap(String mstr) { |
| 118 | Measurement m = Measurement.parseCode(mstr); |
| 119 | if (m == null || !MeasurementMath.isPureNumber(m)) |
| 120 | return Double.NaN; |
| 121 | return m.number; |
| 122 | } |
| 123 | private String mstras1(String mstr, String ustr, boolean withCode) { |
| 124 | Measurement m = Measurement.parseCode(mstr); |
| 125 | if (m == null) |
| 126 | return Measurement.errorIndicator; |
| 127 | Unit u; |
| 128 | try { |
| 129 | u = Unit.parseUnitString(ustr); |
| 130 | } catch (Exception e) { |
| 131 | return Measurement.errorIndicator; |
| 132 | } |
| 133 | return Measurement.formatInUnit(m, u, withCode); |
| 134 | } |
| 135 | public String mstras(String mstr, String ustr) { |
| 136 | return mstras1(mstr, ustr, true); |
| 137 | } |
| 138 | public String mcleanstras(String mstr, String ustr) { |
| 139 | return mstras1(mstr, ustr, false); |
| 140 | } |
| 141 | } |