geodetic computations
[debian/openrocket] / src / net / sf / openrocket / unit / UnitGroup.java
1 package net.sf.openrocket.unit;
2
3 import static net.sf.openrocket.util.Chars.*;
4 import static net.sf.openrocket.util.MathUtil.pow2;
5
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 import net.sf.openrocket.rocketcomponent.Configuration;
14 import net.sf.openrocket.rocketcomponent.Rocket;
15
16
17 /**
18  * A group of units (eg. length, mass etc.).  Contains a list of different units of a same
19  * quantity.
20  * 
21  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22  */
23
24 public class UnitGroup {
25         
26         public static final UnitGroup UNITS_NONE;
27         
28         public static final UnitGroup UNITS_MOTOR_DIMENSIONS;
29         public static final UnitGroup UNITS_LENGTH;
30         public static final UnitGroup UNITS_DISTANCE;
31         
32         public static final UnitGroup UNITS_AREA;
33         public static final UnitGroup UNITS_STABILITY;
34         /**
35          * This unit group contains only the caliber unit that never scales the originating "SI" value.
36          * It can be used in cases where the originating value is already in calibers to obtains the correct unit.
37          */
38         public static final UnitGroup UNITS_STABILITY_CALIBERS;
39         public static final UnitGroup UNITS_VELOCITY;
40         public static final UnitGroup UNITS_ACCELERATION;
41         public static final UnitGroup UNITS_MASS;
42         public static final UnitGroup UNITS_INERTIA;
43         public static final UnitGroup UNITS_ANGLE;
44         public static final UnitGroup UNITS_DENSITY_BULK;
45         public static final UnitGroup UNITS_DENSITY_SURFACE;
46         public static final UnitGroup UNITS_DENSITY_LINE;
47         public static final UnitGroup UNITS_FORCE;
48         public static final UnitGroup UNITS_IMPULSE;
49         
50         /** Time in the order of less than a second (time step etc). */
51         public static final UnitGroup UNITS_TIME_STEP;
52         
53         /** Time in the order of seconds (motor delay etc). */
54         public static final UnitGroup UNITS_SHORT_TIME;
55         
56         /** Time in the order of the flight time of a rocket. */
57         public static final UnitGroup UNITS_FLIGHT_TIME;
58         public static final UnitGroup UNITS_ROLL;
59         public static final UnitGroup UNITS_TEMPERATURE;
60         public static final UnitGroup UNITS_PRESSURE;
61         public static final UnitGroup UNITS_RELATIVE;
62         public static final UnitGroup UNITS_ROUGHNESS;
63         
64         public static final UnitGroup UNITS_COEFFICIENT;
65         
66         //      public static final UnitGroup UNITS_FREQUENCY;
67         
68
69         public static final Map<String, UnitGroup> UNITS;
70         
71
72         /*
73          * Note:  Units may not use HTML tags.
74          * 
75          * The scaling value "X" is obtained by "one of this unit is X of SI units"
76          * Type into Google for example:  "1 in^2 in m^2"
77          */
78         static {
79                 UNITS_NONE = new UnitGroup();
80                 UNITS_NONE.addUnit(Unit.NOUNIT2);
81                 
82                 UNITS_LENGTH = new UnitGroup();
83                 UNITS_LENGTH.addUnit(new GeneralUnit(0.001, "mm"));
84                 UNITS_LENGTH.addUnit(new GeneralUnit(0.01, "cm"));
85                 UNITS_LENGTH.addUnit(new GeneralUnit(1, "m"));
86                 UNITS_LENGTH.addUnit(new GeneralUnit(0.0254, "in"));
87                 UNITS_LENGTH.addUnit(new GeneralUnit(0.3048, "ft"));
88                 UNITS_LENGTH.setDefaultUnit(1);
89                 
90                 UNITS_MOTOR_DIMENSIONS = new UnitGroup();
91                 UNITS_MOTOR_DIMENSIONS.addUnit(new GeneralUnit(0.001, "mm"));
92                 UNITS_MOTOR_DIMENSIONS.addUnit(new GeneralUnit(0.01, "cm"));
93                 UNITS_MOTOR_DIMENSIONS.addUnit(new GeneralUnit(0.0254, "in"));
94                 UNITS_MOTOR_DIMENSIONS.setDefaultUnit(0);
95                 
96                 UNITS_DISTANCE = new UnitGroup();
97                 UNITS_DISTANCE.addUnit(new GeneralUnit(1, "m"));
98                 UNITS_DISTANCE.addUnit(new GeneralUnit(1000, "km"));
99                 UNITS_DISTANCE.addUnit(new GeneralUnit(0.3048, "ft"));
100                 UNITS_DISTANCE.addUnit(new GeneralUnit(0.9144, "yd"));
101                 UNITS_DISTANCE.addUnit(new GeneralUnit(1609.344, "mi"));
102                 UNITS_DISTANCE.addUnit(new GeneralUnit(1852, "nmi"));
103                 
104                 UNITS_AREA = new UnitGroup();
105                 UNITS_AREA.addUnit(new GeneralUnit(pow2(0.001), "mm" + SQUARED));
106                 UNITS_AREA.addUnit(new GeneralUnit(pow2(0.01), "cm" + SQUARED));
107                 UNITS_AREA.addUnit(new GeneralUnit(1, "m" + SQUARED));
108                 UNITS_AREA.addUnit(new GeneralUnit(pow2(0.0254), "in" + SQUARED));
109                 UNITS_AREA.addUnit(new GeneralUnit(pow2(0.3048), "ft" + SQUARED));
110                 UNITS_AREA.setDefaultUnit(1);
111                 
112
113                 UNITS_STABILITY = new UnitGroup();
114                 UNITS_STABILITY.addUnit(new GeneralUnit(0.001, "mm"));
115                 UNITS_STABILITY.addUnit(new GeneralUnit(0.01, "cm"));
116                 UNITS_STABILITY.addUnit(new GeneralUnit(0.0254, "in"));
117                 UNITS_STABILITY.addUnit(new CaliberUnit((Rocket) null));
118                 UNITS_STABILITY.setDefaultUnit(3);
119                 
120                 UNITS_STABILITY_CALIBERS = new UnitGroup();
121                 UNITS_STABILITY_CALIBERS.addUnit(new GeneralUnit(1, "cal"));
122                 
123
124                 UNITS_VELOCITY = new UnitGroup();
125                 UNITS_VELOCITY.addUnit(new GeneralUnit(1, "m/s"));
126                 UNITS_VELOCITY.addUnit(new GeneralUnit(1 / 3.6, "km/h"));
127                 UNITS_VELOCITY.addUnit(new GeneralUnit(0.3048, "ft/s"));
128                 UNITS_VELOCITY.addUnit(new GeneralUnit(0.44704, "mph"));
129                 
130                 UNITS_ACCELERATION = new UnitGroup();
131                 UNITS_ACCELERATION.addUnit(new GeneralUnit(1, "m/s" + SQUARED));
132                 UNITS_ACCELERATION.addUnit(new GeneralUnit(0.3048, "ft/s" + SQUARED));
133                 
134                 UNITS_MASS = new UnitGroup();
135                 UNITS_MASS.addUnit(new GeneralUnit(0.001, "g"));
136                 UNITS_MASS.addUnit(new GeneralUnit(1, "kg"));
137                 UNITS_MASS.addUnit(new GeneralUnit(0.0283495231, "oz"));
138                 UNITS_MASS.addUnit(new GeneralUnit(0.45359237, "lb"));
139                 
140                 UNITS_INERTIA = new UnitGroup();
141                 UNITS_INERTIA.addUnit(new GeneralUnit(0.0001, "kg" + DOT + "cm" + SQUARED));
142                 UNITS_INERTIA.addUnit(new GeneralUnit(1, "kg" + DOT + "m" + SQUARED));
143                 UNITS_INERTIA.addUnit(new GeneralUnit(1.82899783e-5, "oz" + DOT + "in" + SQUARED));
144                 UNITS_INERTIA.addUnit(new GeneralUnit(0.000292639653, "lb" + DOT + "in" + SQUARED));
145                 UNITS_INERTIA.addUnit(new GeneralUnit(0.0421401101, "lb" + DOT + "ft" + SQUARED));
146                 UNITS_INERTIA.addUnit(new GeneralUnit(1.35581795, "lbf" + DOT + "ft" + DOT + "s" + SQUARED));
147                 UNITS_INERTIA.setDefaultUnit(1);
148                 
149                 UNITS_ANGLE = new UnitGroup();
150                 UNITS_ANGLE.addUnit(new DegreeUnit());
151                 UNITS_ANGLE.addUnit(new FixedPrecisionUnit("rad", 0.01));
152                 UNITS_ANGLE.addUnit(new GeneralUnit(1.0/3437.74677078, "arcmin"));
153                 
154                 UNITS_DENSITY_BULK = new UnitGroup();
155                 UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1000, "g/cm" + CUBED));
156                 UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1, "kg/m" + CUBED));
157                 UNITS_DENSITY_BULK.addUnit(new GeneralUnit(1729.99404, "oz/in" + CUBED));
158                 UNITS_DENSITY_BULK.addUnit(new GeneralUnit(16.0184634, "lb/ft" + CUBED));
159                 
160                 UNITS_DENSITY_SURFACE = new UnitGroup();
161                 UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(10, "g/cm" + SQUARED));
162                 UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(0.001, "g/m" + SQUARED));
163                 UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(1, "kg/m" + SQUARED));
164                 UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(43.9418487, "oz/in" + SQUARED));
165                 UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(0.305151727, "oz/ft" + SQUARED));
166                 UNITS_DENSITY_SURFACE.addUnit(new GeneralUnit(4.88242764, "lb/ft" + SQUARED));
167                 UNITS_DENSITY_SURFACE.setDefaultUnit(1);
168                 
169                 UNITS_DENSITY_LINE = new UnitGroup();
170                 UNITS_DENSITY_LINE.addUnit(new GeneralUnit(0.001, "g/m"));
171                 UNITS_DENSITY_LINE.addUnit(new GeneralUnit(1, "kg/m"));
172                 UNITS_DENSITY_LINE.addUnit(new GeneralUnit(0.0930102465, "oz/ft"));
173                 
174                 UNITS_FORCE = new UnitGroup();
175                 UNITS_FORCE.addUnit(new GeneralUnit(1, "N"));
176                 UNITS_FORCE.addUnit(new GeneralUnit(4.44822162, "lbf"));
177                 UNITS_FORCE.addUnit(new GeneralUnit(9.80665, "kgf"));
178                 
179                 UNITS_IMPULSE = new UnitGroup();
180                 UNITS_IMPULSE.addUnit(new GeneralUnit(1, "Ns"));
181                 UNITS_IMPULSE.addUnit(new GeneralUnit(4.44822162, "lbf" + DOT + "s"));
182                 
183                 UNITS_TIME_STEP = new UnitGroup();
184                 UNITS_TIME_STEP.addUnit(new FixedPrecisionUnit("ms", 1, 0.001));
185                 UNITS_TIME_STEP.addUnit(new FixedPrecisionUnit("s", 0.01));
186                 UNITS_TIME_STEP.setDefaultUnit(1);
187                 
188                 UNITS_SHORT_TIME = new UnitGroup();
189                 UNITS_SHORT_TIME.addUnit(new GeneralUnit(1, "s"));
190                 
191                 UNITS_FLIGHT_TIME = new UnitGroup();
192                 UNITS_FLIGHT_TIME.addUnit(new GeneralUnit(1, "s"));
193                 UNITS_FLIGHT_TIME.addUnit(new GeneralUnit(60, "min"));
194                 
195                 UNITS_ROLL = new UnitGroup();
196                 UNITS_ROLL.addUnit(new GeneralUnit(1, "rad/s"));
197                 UNITS_ROLL.addUnit(new GeneralUnit(2 * Math.PI, "r/s"));
198                 UNITS_ROLL.addUnit(new GeneralUnit(2 * Math.PI / 60, "rpm"));
199                 UNITS_ROLL.setDefaultUnit(1);
200                 
201                 UNITS_TEMPERATURE = new UnitGroup();
202                 UNITS_TEMPERATURE.addUnit(new FixedPrecisionUnit("K", 1));
203                 UNITS_TEMPERATURE.addUnit(new TemperatureUnit(1, 273.15, DEGREE + "C"));
204                 UNITS_TEMPERATURE.addUnit(new TemperatureUnit(5.0 / 9.0, 459.67, DEGREE + "F"));
205                 UNITS_TEMPERATURE.setDefaultUnit(1);
206                 
207                 UNITS_PRESSURE = new UnitGroup();
208                 UNITS_PRESSURE.addUnit(new FixedPrecisionUnit("mbar", 1, 1.0e2));
209                 UNITS_PRESSURE.addUnit(new FixedPrecisionUnit("bar", 0.001, 1.0e5));
210                 UNITS_PRESSURE.addUnit(new FixedPrecisionUnit("atm", 0.001, 1.01325e5));
211                 UNITS_PRESSURE.addUnit(new GeneralUnit(101325.0 / 760.0, "mmHg"));
212                 UNITS_PRESSURE.addUnit(new GeneralUnit(3386.389, "inHg"));
213                 UNITS_PRESSURE.addUnit(new GeneralUnit(6894.75729, "psi"));
214                 UNITS_PRESSURE.addUnit(new GeneralUnit(1, "Pa"));
215                 
216                 UNITS_RELATIVE = new UnitGroup();
217                 UNITS_RELATIVE.addUnit(new FixedPrecisionUnit("" + ZWSP, 0.01, 1.0));
218                 UNITS_RELATIVE.addUnit(new GeneralUnit(0.01, "%"));
219                 UNITS_RELATIVE.addUnit(new FixedPrecisionUnit("" + PERMILLE, 1, 0.001));
220                 //              UNITS_RELATIVE.addUnit(new FixedPrecisionUnit("" + ZWSP, 0.01, 1.0));
221                 //              UNITS_RELATIVE.addUnit(new FixedPrecisionUnit("%", 1, 0.01));
222                 //              UNITS_RELATIVE.addUnit(new FixedPrecisionUnit("" + PERMILLE, 1, 0.001));
223                 UNITS_RELATIVE.setDefaultUnit(1);
224                 
225
226                 UNITS_ROUGHNESS = new UnitGroup();
227                 UNITS_ROUGHNESS.addUnit(new GeneralUnit(0.000001, MICRO + "m"));
228                 UNITS_ROUGHNESS.addUnit(new GeneralUnit(0.0000254, "mil"));
229                 
230
231                 UNITS_COEFFICIENT = new UnitGroup();
232                 UNITS_COEFFICIENT.addUnit(new FixedPrecisionUnit("" + ZWSP, 0.01)); // zero-width space
233                 
234
235                 // This is not used by OpenRocket, and not extensively tested:
236                 //              UNITS_FREQUENCY = new UnitGroup();
237                 //              UNITS_FREQUENCY.addUnit(new GeneralUnit(1, "s"));
238                 //              UNITS_FREQUENCY.addUnit(new GeneralUnit(0.001, "ms"));
239                 //              UNITS_FREQUENCY.addUnit(new GeneralUnit(0.000001, MICRO + "s"));
240                 //              UNITS_FREQUENCY.addUnit(new FrequencyUnit(1, "Hz"));
241                 //              UNITS_FREQUENCY.addUnit(new FrequencyUnit(1000, "kHz"));
242                 //              UNITS_FREQUENCY.setDefaultUnit(3);
243                 
244
245                 HashMap<String, UnitGroup> map = new HashMap<String, UnitGroup>();
246                 map.put("NONE", UNITS_NONE);
247                 map.put("LENGTH", UNITS_LENGTH);
248                 map.put("MOTOR_DIMENSIONS", UNITS_MOTOR_DIMENSIONS);
249                 map.put("DISTANCE", UNITS_DISTANCE);
250                 map.put("VELOCITY", UNITS_VELOCITY);
251                 map.put("ACCELERATION", UNITS_ACCELERATION);
252                 map.put("AREA", UNITS_AREA);
253                 map.put("STABILITY", UNITS_STABILITY);
254                 map.put("MASS", UNITS_MASS);
255                 map.put("INERTIA", UNITS_INERTIA);
256                 map.put("ANGLE", UNITS_ANGLE);
257                 map.put("DENSITY_BULK", UNITS_DENSITY_BULK);
258                 map.put("DENSITY_SURFACE", UNITS_DENSITY_SURFACE);
259                 map.put("DENSITY_LINE", UNITS_DENSITY_LINE);
260                 map.put("FORCE", UNITS_FORCE);
261                 map.put("IMPULSE", UNITS_IMPULSE);
262                 map.put("TIME_STEP", UNITS_TIME_STEP);
263                 map.put("SHORT_TIME", UNITS_SHORT_TIME);
264                 map.put("FLIGHT_TIME", UNITS_FLIGHT_TIME);
265                 map.put("ROLL", UNITS_ROLL);
266                 map.put("TEMPERATURE", UNITS_TEMPERATURE);
267                 map.put("PRESSURE", UNITS_PRESSURE);
268                 map.put("RELATIVE", UNITS_RELATIVE);
269                 map.put("ROUGHNESS", UNITS_ROUGHNESS);
270                 map.put("COEFFICIENT", UNITS_COEFFICIENT);
271                 
272                 UNITS = Collections.unmodifiableMap(map);
273         }
274         
275         public static void setDefaultMetricUnits() {
276                 UNITS_LENGTH.setDefaultUnit("cm");
277                 UNITS_MOTOR_DIMENSIONS.setDefaultUnit("mm");
278                 UNITS_DISTANCE.setDefaultUnit("m");
279                 UNITS_AREA.setDefaultUnit("cm" + SQUARED);
280                 UNITS_STABILITY.setDefaultUnit("cal");
281                 UNITS_VELOCITY.setDefaultUnit("m/s");
282                 UNITS_ACCELERATION.setDefaultUnit("m/s" + SQUARED);
283                 UNITS_MASS.setDefaultUnit("g");
284                 UNITS_INERTIA.setDefaultUnit("kg" + DOT + "m" + SQUARED);
285                 UNITS_ANGLE.setDefaultUnit("" + DEGREE);
286                 UNITS_DENSITY_BULK.setDefaultUnit("g/cm" + CUBED);
287                 UNITS_DENSITY_SURFACE.setDefaultUnit("g/m" + SQUARED);
288                 UNITS_DENSITY_LINE.setDefaultUnit("g/m");
289                 UNITS_FORCE.setDefaultUnit("N");
290                 UNITS_IMPULSE.setDefaultUnit("Ns");
291                 UNITS_TIME_STEP.setDefaultUnit("s");
292                 UNITS_FLIGHT_TIME.setDefaultUnit("s");
293                 UNITS_ROLL.setDefaultUnit("r/s");
294                 UNITS_TEMPERATURE.setDefaultUnit(DEGREE + "C");
295                 UNITS_PRESSURE.setDefaultUnit("mbar");
296                 UNITS_RELATIVE.setDefaultUnit("%");
297                 UNITS_ROUGHNESS.setDefaultUnit(MICRO + "m");
298         }
299         
300         public static void setDefaultImperialUnits() {
301                 UNITS_LENGTH.setDefaultUnit("in");
302                 UNITS_MOTOR_DIMENSIONS.setDefaultUnit("in");
303                 UNITS_DISTANCE.setDefaultUnit("ft");
304                 UNITS_AREA.setDefaultUnit("in" + SQUARED);
305                 UNITS_STABILITY.setDefaultUnit("cal");
306                 UNITS_VELOCITY.setDefaultUnit("ft/s");
307                 UNITS_ACCELERATION.setDefaultUnit("ft/s" + SQUARED);
308                 UNITS_MASS.setDefaultUnit("oz");
309                 UNITS_INERTIA.setDefaultUnit("lb" + DOT + "ft" + SQUARED);
310                 UNITS_ANGLE.setDefaultUnit("" + DEGREE);
311                 UNITS_DENSITY_BULK.setDefaultUnit("oz/in" + CUBED);
312                 UNITS_DENSITY_SURFACE.setDefaultUnit("oz/ft" + SQUARED);
313                 UNITS_DENSITY_LINE.setDefaultUnit("oz/ft");
314                 UNITS_FORCE.setDefaultUnit("N");
315                 UNITS_IMPULSE.setDefaultUnit("Ns");
316                 UNITS_TIME_STEP.setDefaultUnit("s");
317                 UNITS_FLIGHT_TIME.setDefaultUnit("s");
318                 UNITS_ROLL.setDefaultUnit("r/s");
319                 UNITS_TEMPERATURE.setDefaultUnit(DEGREE + "F");
320                 UNITS_PRESSURE.setDefaultUnit("mbar");
321                 UNITS_RELATIVE.setDefaultUnit("%");
322                 UNITS_ROUGHNESS.setDefaultUnit("mil");
323         }
324         
325         
326         /**
327          * Return a UnitGroup for stability units based on the rocket.
328          * 
329          * @param rocket        the rocket from which to calculate the caliber
330          * @return                      the unit group
331          */
332         public static UnitGroup stabilityUnits(Rocket rocket) {
333                 return new StabilityUnitGroup(rocket);
334         }
335         
336         
337         /**
338          * Return a UnitGroup for stability units based on the rocket configuration.
339          * 
340          * @param config        the rocket configuration from which to calculate the caliber
341          * @return                      the unit group
342          */
343         public static UnitGroup stabilityUnits(Configuration config) {
344                 return new StabilityUnitGroup(config);
345         }
346         
347         
348         /**
349          * Return a UnitGroup for stability units based on a constant caliber.
350          * 
351          * @param reference     the constant reference length
352          * @return                      the unit group
353          */
354         public static UnitGroup stabilityUnits(double reference) {
355                 return new StabilityUnitGroup(reference);
356         }
357         
358         
359         //////////////////////////////////////////////////////
360         
361
362         protected ArrayList<Unit> units = new ArrayList<Unit>();
363         protected int defaultUnit = 0;
364         
365         public int getUnitCount() {
366                 return units.size();
367         }
368         
369         public Unit getDefaultUnit() {
370                 return units.get(defaultUnit);
371         }
372         
373         public int getDefaultUnitIndex() {
374                 return defaultUnit;
375         }
376         
377         public void setDefaultUnit(int n) {
378                 if (n < 0 || n >= units.size()) {
379                         throw new IllegalArgumentException("index out of range: " + n);
380                 }
381                 defaultUnit = n;
382         }
383         
384         
385
386         /**
387          * Find a unit by approximate unit name.  Only letters and (ordinary) numbers are
388          * considered in the matching.  This method is mainly means for testing, allowing
389          * a simple means to obtain a particular unit.
390          * 
391          * @param str   the unit name.
392          * @return              the corresponding unit, or <code>null</code> if not found.
393          */
394         public Unit findApproximate(String str) {
395                 str = str.replaceAll("\\W", "").trim();
396                 for (Unit u : units) {
397                         String name = u.getUnit().replaceAll("\\W", "").trim();
398                         if (str.equalsIgnoreCase(name))
399                                 return u;
400                 }
401                 return null;
402         }
403         
404         /**
405          * Set the default unit based on the unit name.  Throws an exception if a
406          * unit with the provided name is not available.
407          * 
408          * @param   name        the unit name.
409          * @throws  IllegalArgumentException    if the corresponding unit is not found in the group.
410          */
411         public void setDefaultUnit(String name) throws IllegalArgumentException {
412                 for (int i = 0; i < units.size(); i++) {
413                         if (units.get(i).getUnit().equals(name)) {
414                                 setDefaultUnit(i);
415                                 return;
416                         }
417                 }
418                 throw new IllegalArgumentException("name=" + name);
419         }
420         
421         
422         public Unit getUnit(int n) {
423                 return units.get(n);
424         }
425         
426         public int getUnitIndex(Unit u) {
427                 return units.indexOf(u);
428         }
429         
430         private void addUnit(Unit u) {
431                 units.add(u);
432         }
433         
434         public boolean contains(Unit u) {
435                 return units.contains(u);
436         }
437         
438         public Unit[] getUnits() {
439                 return units.toArray(new Unit[0]);
440         }
441         
442         
443         /**
444          * Return the value formatted by the default unit of this group.
445          * It is the same as calling <code>getDefaultUnit().toString(value)</code>.
446          * 
447          * @param value         the SI value to format.
448          * @return                      the formatted string.
449          * @see                         Unit#toString(double)
450          */
451         public String toString(double value) {
452                 return this.getDefaultUnit().toString(value);
453         }
454         
455         
456         /**
457          * Return the value formatted by the default unit of this group including the unit.
458          * It is the same as calling <code>getDefaultUnit().toStringUnit(value)</code>.
459          * 
460          * @param value         the SI value to format.
461          * @return                      the formatted string.
462          * @see                         Unit#toStringUnit(double)
463          */
464         public String toStringUnit(double value) {
465                 return this.getDefaultUnit().toStringUnit(value);
466         }
467         
468         
469
470
471
472         /**
473          * Creates a new Value object with the specified value and the default unit of this group.
474          * 
475          * @param value the value to set.
476          * @return              a new Value object.
477          */
478         public Value toValue(double value) {
479                 return this.getDefaultUnit().toValue(value);
480         }
481         
482         
483
484
485         private static final Pattern STRING_PATTERN = Pattern.compile("^\\s*([0-9.,-]+)(.*?)$");
486         
487         /**
488          * Converts a string into an SI value.  If the string has one of the units in this
489          * group appended to it, that unit will be used in conversion.  Otherwise the default
490          * unit will be used.  If an unknown unit is specified or the value does not parse
491          * with <code>Double.parseDouble</code> then a <code>NumberFormatException</code> 
492          * is thrown.
493          * <p>
494          * This method is applicable only for simple units without e.g. powers.
495          * 
496          * @param str   the string to parse.
497          * @return              the SI value.
498          * @throws NumberFormatException   if the string cannot be parsed.
499          */
500         public double fromString(String str) {
501                 Matcher matcher = STRING_PATTERN.matcher(str);
502                 
503                 if (!matcher.matches()) {
504                         throw new NumberFormatException("string did not match required pattern");
505                 }
506                 
507                 double value = Double.parseDouble(matcher.group(1));
508                 String unit = matcher.group(2).trim();
509                 
510                 if (unit.equals("")) {
511                         value = this.getDefaultUnit().fromUnit(value);
512                 } else {
513                         int i;
514                         for (i = 0; i < units.size(); i++) {
515                                 Unit u = units.get(i);
516                                 if (unit.equalsIgnoreCase(u.getUnit())) {
517                                         value = u.fromUnit(value);
518                                         break;
519                                 }
520                         }
521                         if (i >= units.size()) {
522                                 throw new NumberFormatException("unknown unit " + unit);
523                         }
524                 }
525                 
526                 return value;
527         }
528         
529         
530         ///////////////////////////
531         
532
533         /**
534          * A private class that switches the CaliberUnit to a rocket-specific CaliberUnit.
535          * All other methods are passed through to UNITS_STABILITY.
536          */
537         private static class StabilityUnitGroup extends UnitGroup {
538                 
539                 public StabilityUnitGroup(double ref) {
540                         this(new CaliberUnit(ref));
541                 }
542                 
543                 public StabilityUnitGroup(Rocket rocket) {
544                         this(new CaliberUnit(rocket));
545                 }
546                 
547                 public StabilityUnitGroup(Configuration config) {
548                         this(new CaliberUnit(config));
549                 }
550                 
551                 private StabilityUnitGroup(CaliberUnit caliberUnit) {
552                         this.units.addAll(UnitGroup.UNITS_STABILITY.units);
553                         this.defaultUnit = UnitGroup.UNITS_STABILITY.defaultUnit;
554                         for (int i = 0; i < units.size(); i++) {
555                                 if (units.get(i) instanceof CaliberUnit) {
556                                         units.set(i, caliberUnit);
557                                 }
558                         }
559                 }
560                 
561                 
562                 @Override
563                 public void setDefaultUnit(int n) {
564                         super.setDefaultUnit(n);
565                         UNITS_STABILITY.setDefaultUnit(n);
566                 }
567         }
568 }