- Implemented a DampingMoment simulation listener example
[debian/openrocket] / core / src / net / sf / openrocket / simulation / FlightDataType.java
index 2cbe325aebad733ab067383c27722d83b91d06c4..adb4b51bc88eae89612fce7ed2df8b03833b89b5 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Locale;
 import java.util.Map;
 
 import net.sf.openrocket.l10n.Translator;
+import net.sf.openrocket.logging.LogHelper;
 import net.sf.openrocket.startup.Application;
 import net.sf.openrocket.unit.UnitGroup;
 
@@ -23,15 +24,16 @@ import net.sf.openrocket.unit.UnitGroup;
  */
 public class FlightDataType implements Comparable<FlightDataType> {
        private static final Translator trans = Application.getTranslator();
+       private static final LogHelper log = Application.getLogger();
        
        /** Priority of custom-created variables */
        private static final int DEFAULT_PRIORITY = 999;
        
        /** List of existing types.  MUST BE DEFINED BEFORE ANY TYPES!! */
+       /** NOTE: The String key here is now the symbol */
        private static final Map<String, FlightDataType> EXISTING_TYPES = new HashMap<String, FlightDataType>();
        
        
-       
        //// Time
        public static final FlightDataType TYPE_TIME = newType(trans.get("FlightDataType.TYPE_TIME"), "t", UnitGroup.UNITS_FLIGHT_TIME, 1);
        
@@ -69,6 +71,9 @@ public class FlightDataType implements Comparable<FlightDataType> {
        //// Longitude
        public static final FlightDataType TYPE_LONGITUDE = newType(trans.get("FlightDataType.TYPE_LONGITUDE"), "\u03bb", UnitGroup.UNITS_ANGLE, 37);
        
+       //// Gravity
+       public static final FlightDataType TYPE_GRAVITY = newType(trans.get("FlightDataType.TYPE_GRAVITY"), "g", UnitGroup.UNITS_ACCELERATION, 38);
+       
        //// Angular motion
        //// Angle of attack
        public static final FlightDataType TYPE_AOA = newType(trans.get("FlightDataType.TYPE_AOA"), "\u03b1", UnitGroup.UNITS_ANGLE, 40);
@@ -83,16 +88,18 @@ public class FlightDataType implements Comparable<FlightDataType> {
        //// Stability information
        //// Mass
        public static final FlightDataType TYPE_MASS = newType(trans.get("FlightDataType.TYPE_MASS"), "m", UnitGroup.UNITS_MASS, 50);
+       //// Propellant mass
+       public static final FlightDataType TYPE_PROPELLANT_MASS = newType(trans.get("FlightDataType.TYPE_PROPELLANT_MASS"), "mp", UnitGroup.UNITS_MASS, 51);
        //// Longitudinal moment of inertia
-       public static final FlightDataType TYPE_LONGITUDINAL_INERTIA = newType(trans.get("FlightDataType.TYPE_LONGITUDINAL_INERTIA"), "Il", UnitGroup.UNITS_INERTIA, 51);
+       public static final FlightDataType TYPE_LONGITUDINAL_INERTIA = newType(trans.get("FlightDataType.TYPE_LONGITUDINAL_INERTIA"), "Il", UnitGroup.UNITS_INERTIA, 52);
        //// Rotational moment of inertia
-       public static final FlightDataType TYPE_ROTATIONAL_INERTIA = newType(trans.get("FlightDataType.TYPE_ROTATIONAL_INERTIA"), "Ir", UnitGroup.UNITS_INERTIA, 52);
+       public static final FlightDataType TYPE_ROTATIONAL_INERTIA = newType(trans.get("FlightDataType.TYPE_ROTATIONAL_INERTIA"), "Ir", UnitGroup.UNITS_INERTIA, 53);
        //// CP location
-       public static final FlightDataType TYPE_CP_LOCATION = newType(trans.get("FlightDataType.TYPE_CP_LOCATION"), "Cp", UnitGroup.UNITS_LENGTH, 53);
+       public static final FlightDataType TYPE_CP_LOCATION = newType(trans.get("FlightDataType.TYPE_CP_LOCATION"), "Cp", UnitGroup.UNITS_LENGTH, 54);
        //// CG location
-       public static final FlightDataType TYPE_CG_LOCATION = newType(trans.get("FlightDataType.TYPE_CG_LOCATION"), "Cg", UnitGroup.UNITS_LENGTH, 54);
+       public static final FlightDataType TYPE_CG_LOCATION = newType(trans.get("FlightDataType.TYPE_CG_LOCATION"), "Cg", UnitGroup.UNITS_LENGTH, 55);
        //// Stability margin calibers
-       public static final FlightDataType TYPE_STABILITY = newType(trans.get("FlightDataType.TYPE_STABILITY"), "S", UnitGroup.UNITS_COEFFICIENT, 55);
+       public static final FlightDataType TYPE_STABILITY = newType(trans.get("FlightDataType.TYPE_STABILITY"), "S", UnitGroup.UNITS_COEFFICIENT, 56);
        
        
        //// Characteristic numbers
@@ -167,7 +174,7 @@ public class FlightDataType implements Comparable<FlightDataType> {
        //// Air temperature
        public static final FlightDataType TYPE_AIR_TEMPERATURE = newType(trans.get("FlightDataType.TYPE_AIR_TEMPERATURE"), "T", UnitGroup.UNITS_TEMPERATURE, 111);
        //// Air pressure
-       public static final FlightDataType TYPE_AIR_PRESSURE = newType(trans.get("FlightDataType.TYPE_AIR_PRESSURE"), "p", UnitGroup.UNITS_PRESSURE, 112);
+       public static final FlightDataType TYPE_AIR_PRESSURE = newType(trans.get("FlightDataType.TYPE_AIR_PRESSURE"), "P", UnitGroup.UNITS_PRESSURE, 112);
        //// Speed of sound
        public static final FlightDataType TYPE_SPEED_OF_SOUND = newType(trans.get("FlightDataType.TYPE_SPEED_OF_SOUND"), "Vs", UnitGroup.UNITS_VELOCITY, 113);
        
@@ -192,12 +199,14 @@ public class FlightDataType implements Comparable<FlightDataType> {
                TYPE_VELOCITY_XY, 
                TYPE_ACCELERATION_XY, 
                TYPE_LATITUDE, 
-               TYPE_LONGITUDE, 
+               TYPE_LONGITUDE,
+               TYPE_GRAVITY,
                TYPE_AOA,
                TYPE_ROLL_RATE,
                TYPE_PITCH_RATE,
                TYPE_YAW_RATE,
                TYPE_MASS,
+               TYPE_PROPELLANT_MASS,
                TYPE_LONGITUDINAL_INERTIA,
                TYPE_ROTATIONAL_INERTIA,
                TYPE_CP_LOCATION,
@@ -235,36 +244,91 @@ public class FlightDataType implements Comparable<FlightDataType> {
                };
        
        /**
-        * Return a {@link FlightDataType} based on a string description.  This returns known data types
-        * if possible, or a new type otherwise.
+        * Return a {@link FlightDataType} with a given string description, symbol and unitgroup.
+        * This returns an existing data type if the symbol matches that of an existing type. 
+        * 
+        * If the symbol matches but the unit and description information differ, then the old stored datatype
+        * is erased and the updated version based on the given parametes is returned.
+        * The only exception is if the description or unitgroup are undefined (null or empty string). In this case 
+        * we just get these parameters from the existing type when making the new one.
         * 
         * @param s             the string description of the type.
         * @param u             the unit group the new type should belong to if a new group is created.
         * @return              a data type.
         */
+       @SuppressWarnings("null")
        public static synchronized FlightDataType getType(String s, String symbol, UnitGroup u) {
-               // modified to include the unit
-               FlightDataType type = EXISTING_TYPES.get(s.toLowerCase(Locale.ENGLISH));
+
+               // if symbol is null : try finding by name
+               // if unit is null : don't do anything to the unit if found, just return datatype if found and generate an error and an empty unit otherwise
+               int oldPriority = DEFAULT_PRIORITY;
                
-               // added this for backward compatibility. Will update type if symbol undefined
-               //if (type != null && type.getSymbol() != symbol){
-               //      EXISTING_TYPES.remove(type);
-               //      type = null;
-               //}
+               //FlightDataType type = findFromSymbol(symbol);
+               FlightDataType type = EXISTING_TYPES.get(symbol);
                
                if (type != null) {
-                       return type;
+                       // found it from symbol
+                       
+                       // if name was not given (empty string), can use the one we found
+                       if ( s == null || s.isEmpty()){
+                               s = type.getName();
+                       }
+                       if ( u == null ){
+                               u = type.getUnitGroup();
+                       }
+                       
+                       // if something has changed, then we need to remove the old one
+                       // otherwise, just return what we found
+                       if ( !u.equals(type.getUnitGroup()) )
+                          {
+                               oldPriority = type.priority;
+                               EXISTING_TYPES.remove(type);
+                               log.info("Unitgroup of type "+type.getName() + 
+                                                ", has changed from "+type.getUnitGroup().toString() + 
+                                                " to "+u.toString() +
+                                                ". Removing old version.");
+                       }
+                       else if (!s.equals(type.getName())) {
+                               oldPriority = type.priority;
+                               EXISTING_TYPES.remove(type);
+                               log.info("Name of type "+type.getName()+", has changed to "+s+". Removing old version.");
+                       }
+                       else{
+                               return type;
+                       }
                }
-               type = newType(s, symbol, u, DEFAULT_PRIORITY);
-               return type;
+               
+               if (u == null){
+                       u = UnitGroup.UNITS_NONE;
+                       log.error("Made a new flightdatatype, but did not know what units to use.");
+               }
+               
+               // make a new one
+               type = newType(s, symbol, u, oldPriority);
+               return type;    
        }
        
+       /*
+        * Get the flightdatatype from existing types based on the symbol.
+        */
+       /*
+       private static FlightDataType findFromSymbol(String symbol){
+               for (FlightDataType t : EXISTING_TYPES.values()){
+                       if (t.getSymbol().equals(symbol)){
+                               return t;
+                       }
+               }
+               return null;
+       }
+       */
+       
        /**
         * Used while initializing the class.
         */
        private static synchronized FlightDataType newType(String s, String symbol, UnitGroup u, int priority) {
                FlightDataType type = new FlightDataType(s, symbol, u, priority);
-               EXISTING_TYPES.put(s.toLowerCase(Locale.ENGLISH), type);
+               //EXISTING_TYPES.put(s.toLowerCase(Locale.ENGLISH), type);
+               EXISTING_TYPES.put(symbol, type);
                return type;
        }