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