Commit | Line | Data |
---|---|---|
3f5430db MM |
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"))), | |
f650dafb | 45 | new FunctionInfo("mrootint", "mrootint", "Takes an integer root of a measurement. Does not allow fractional powers of units in the result.", |
3a3b5f3c | 46 | Arrays.asList(new ArgumentInfo("base", "Base"), |
f650dafb | 47 | new ArgumentInfo("root", "Root (e.g., 2 for square root)"))), |
3f5430db MM |
48 | new FunctionInfo("mpow", "mpow", "Raises one measurement to the power of another. Both must be pure numbers.", |
49 | Arrays.asList(new ArgumentInfo("base", "Base"), | |
50 | new ArgumentInfo("exp", "Exponent"))), | |
3a3b5f3c MM |
51 | new FunctionInfo("mexp", "mexp", "Raises e (2.718...) to the power of a measurement.", |
52 | Arrays.asList(new ArgumentInfo("m", "Measurement"))), | |
53 | new FunctionInfo("mln", "mln", "Takes the natural logarithm of a measurement. " + | |
54 | "The measurement must be a pure number, so you may have to rewrite a difference of logarithms as a logarithm of a quotient.", | |
55 | Arrays.asList(new ArgumentInfo("m", "Measurement"))), | |
3f5430db MM |
56 | new FunctionInfo("mcmp", "mcmp", |
57 | "Returns the difference between two measurements, expressed in units of the sum of their uncertainties. " + | |
58 | "You can compare measurements very flexibly by checking the result against a tolerance.", | |
59 | Arrays.asList(new ArgumentInfo("a", "First measurement"), | |
60 | new ArgumentInfo("b", "Second measurement"))), | |
61 | ||
62 | new FunctionInfo("mcleanstr", "mcleanstr", | |
63 | "Formats the measurement to a string without including the code in [...].", | |
64 | Arrays.asList(new ArgumentInfo("m", "Measurement string"))), | |
65 | new FunctionInfo("munwrap", "munwrap", | |
66 | "Unwraps a measurement (which must have no units) into a floating-point number with all available precision, regardless of the measurement's uncertainty.", | |
67 | Arrays.asList(new ArgumentInfo("m", "Measurement string"))), | |
68 | new FunctionInfo("mstras", "mstras", | |
69 | "Formats the measurement to a string in the requested unit.", | |
70 | Arrays.asList(new ArgumentInfo("m", "Measurement string"), | |
71 | new ArgumentInfo("u", "Unit string"))), | |
72 | new FunctionInfo("mcleanstras", "mcleanstras", | |
73 | "Formats the measurement to a string in the requested unit without including the code in [...].", | |
74 | Arrays.asList(new ArgumentInfo("m", "Measurement string"), | |
75 | new ArgumentInfo("u", "Unit string"))), | |
76 | }); | |
77 | ||
78 | // We can be instantiated reflectively!!! | |
79 | public MeasurementsAddIn() { | |
80 | super(ah, fis); | |
81 | } | |
82 | ||
83 | // The actual functions!!! | |
84 | ||
85 | // Math | |
86 | public String mneg(String m) { | |
87 | return Measurement.format(MeasurementMath.neg( | |
88 | Measurement.parseCode(m)), true); | |
89 | } | |
90 | public String madd(String a, String b) { | |
91 | return Measurement.format(MeasurementMath.add( | |
92 | Measurement.parseCode(a), Measurement.parseCode(b)), true); | |
93 | } | |
94 | public String msub(String a, String b) { | |
95 | return Measurement.format(MeasurementMath.sub( | |
96 | Measurement.parseCode(a), Measurement.parseCode(b)), true); | |
97 | } | |
98 | public String mmul(String a, String b) { | |
99 | return Measurement.format(MeasurementMath.mul( | |
100 | Measurement.parseCode(a), Measurement.parseCode(b)), true); | |
101 | } | |
102 | public String mdiv(String a, String b) { | |
103 | return Measurement.format(MeasurementMath.div( | |
104 | Measurement.parseCode(a), Measurement.parseCode(b)), true); | |
105 | } | |
106 | public String mpowint(String a, String bstr) { | |
107 | int b = Integer.parseInt(bstr); | |
108 | return Measurement.format(MeasurementMath.powint( | |
109 | Measurement.parseCode(a), b), true); | |
110 | } | |
3a3b5f3c MM |
111 | public String mrootint(String a, String bstr) { |
112 | int b = Integer.parseInt(bstr); | |
113 | return Measurement.format(MeasurementMath.rootint( | |
114 | Measurement.parseCode(a), b), true); | |
115 | } | |
3f5430db MM |
116 | public String mpow(String a, String b) { |
117 | return Measurement.format(MeasurementMath.pow( | |
118 | Measurement.parseCode(a), Measurement.parseCode(b)), true); | |
119 | } | |
3a3b5f3c MM |
120 | public String mexp(String m) { |
121 | return Measurement.format(MeasurementMath.exp( | |
122 | Measurement.parseCode(m)), true); | |
123 | } | |
124 | public String mln(String m) { | |
125 | return Measurement.format(MeasurementMath.ln( | |
126 | Measurement.parseCode(m)), true); | |
127 | } | |
3f5430db MM |
128 | public double mcmp(String a, String b) { |
129 | return MeasurementMath.cmp( | |
130 | Measurement.parseCode(a), Measurement.parseCode(b)); | |
131 | } | |
132 | ||
133 | // Utility | |
134 | public String mcleanstr(String mstr) { | |
135 | Measurement m = Measurement.parseCode(mstr); | |
136 | return Measurement.format(m, false); | |
137 | } | |
138 | public double munwrap(String mstr) { | |
139 | Measurement m = Measurement.parseCode(mstr); | |
140 | if (m == null || !MeasurementMath.isPureNumber(m)) | |
141 | return Double.NaN; | |
142 | return m.number; | |
143 | } | |
144 | private String mstras1(String mstr, String ustr, boolean withCode) { | |
145 | Measurement m = Measurement.parseCode(mstr); | |
146 | if (m == null) | |
147 | return Measurement.errorIndicator; | |
148 | Unit u; | |
149 | try { | |
150 | u = Unit.parseUnitString(ustr); | |
151 | } catch (Exception e) { | |
152 | return Measurement.errorIndicator; | |
153 | } | |
154 | return Measurement.formatInUnit(m, u, withCode); | |
155 | } | |
156 | public String mstras(String mstr, String ustr) { | |
157 | return mstras1(mstr, ustr, true); | |
158 | } | |
159 | public String mcleanstras(String mstr, String ustr) { | |
160 | return mstras1(mstr, ustr, false); | |
161 | } | |
162 | } |