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