Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / file / rocksim / RocksimNoseConeCode.java
index cd580ea1e82835c13e4b341575f8ac695c7107fd..8d24e24447ec284b86eb5596c3f65e87c9fb8d74 100644 (file)
@@ -5,38 +5,57 @@ package net.sf.openrocket.file.rocksim;
 
 import net.sf.openrocket.rocketcomponent.Transition;
 
+import java.util.HashSet;
+import java.util.Set;
+
 /**
  * Models the nose cone shape of a rocket.  Maps from Rocksim's notion to OpenRocket's.
  */
 public enum RocksimNoseConeCode {
-    CONICAL         (0, Transition.Shape.CONICAL),
-    OGIVE           (1, Transition.Shape.OGIVE),
-    PARABOLIC       (2, Transition.Shape.ELLIPSOID),  //Rocksim' PARABOLIC most closely resembles an ELLIPSOID in OpenRocket
-    ELLIPTICAL      (3, Transition.Shape.ELLIPSOID),
-    POWER_SERIES    (4, Transition.Shape.POWER),
+    CONICAL(0, Transition.Shape.CONICAL, "Conic", "Cone"),
+    OGIVE(1, Transition.Shape.OGIVE),
+    PARABOLIC(2, Transition.Shape.ELLIPSOID),  //Rocksim' PARABOLIC most closely resembles an ELLIPSOID in OpenRocket
+    ELLIPTICAL(3, Transition.Shape.ELLIPSOID),
+    POWER_SERIES(4, Transition.Shape.POWER),
     PARABOLIC_SERIES(5, Transition.Shape.PARABOLIC),
-    HAACK           (6, Transition.Shape.HAACK);
+    HAACK(6, Transition.Shape.HAACK);
 
-    /** The Rocksim enumeration value. Sent in XML. */
+    /**
+     * The Rocksim enumeration value. Sent in XML.
+     */
     private final int ordinal;
-    
-    /** The corresponding OpenRocket shape. */
+
+    /**
+     * The corresponding OpenRocket shape.
+     */
     private final Transition.Shape shape;
 
+    /**
+     * Names of the shape that are sometimes found in NCDATA.CSV
+     */
+    private Set<String> shapeNames = new HashSet<String>();
+
     /**
      * Constructor.
-     * 
-     * @param idx    the Rocksim shape code
-     * @param aShape the corresponding OpenRocket shape
+     *
+     * @param idx           the Rocksim shape code
+     * @param aShape        the corresponding OpenRocket shape
+     * @param theShapeNames an array of alternate names
      */
-    private RocksimNoseConeCode(int idx, Transition.Shape aShape) {
+    private RocksimNoseConeCode(int idx, Transition.Shape aShape, String... theShapeNames) {
         ordinal = idx;
         shape = aShape;
+        shapeNames.add(this.name().toLowerCase());
+        if (theShapeNames != null) {
+            for (String theShapeName : theShapeNames) {
+                shapeNames.add(theShapeName.toLowerCase());
+            }
+        }
     }
 
     /**
      * Get the OpenRocket shape that corresponds to the Rocksim shape.
-     * 
+     *
      * @return a shape
      */
     public Transition.Shape asOpenRocket() {
@@ -45,8 +64,8 @@ public enum RocksimNoseConeCode {
 
     /**
      * Lookup an instance of this enum based upon the Rocksim code.
-     * 
-     * @param rocksimShapeCode  the Rocksim code (from XML)
+     *
+     * @param rocksimShapeCode the Rocksim code (from XML)
      * @return an instance of this enum
      */
     public static RocksimNoseConeCode fromCode(int rocksimShapeCode) {
@@ -61,9 +80,8 @@ public enum RocksimNoseConeCode {
 
     /**
      * Lookup an ordinal value for the Rocksim code.
-     * 
-     * @param type  the OR Shape
-     *              
+     *
+     * @param type the OR Shape
      * @return the Rocksim code
      */
     public static int toCode(Transition.Shape type) {
@@ -78,4 +96,36 @@ public enum RocksimNoseConeCode {
         }
         return ELLIPTICAL.ordinal; //Default
     }
+
+    /**
+     * Given the name of a shape, map it into an instance of this enum.
+     *
+     * @param theName the name of the shape; case does not matter
+     * @return the corresponding enum instance; defaults to PARABOLIC if not found.
+     */
+    public static RocksimNoseConeCode fromShapeName(String theName) {
+        RocksimNoseConeCode[] values = values();
+        for (RocksimNoseConeCode value : values) {
+            if (value.shapeNames.contains(theName.toLowerCase())) {
+                return value;
+            }
+        }
+        return PARABOLIC; //Default
+    }
+
+    /**
+     * Convenience method that determines if the parameter is an integer that refers to a shape code, or the name
+     * of the shape itself.  This basically combines fromCode and fromShapeName into one method.
+     *
+     * @param nameOrOrdinalString the shape number or shape name
+     * @return an instance of this enum; defaults to PARABOLIC if not found
+     */
+    public static RocksimNoseConeCode fromShapeNameOrCode(String nameOrOrdinalString) {
+        try {
+            return fromCode(Integer.parseInt(nameOrOrdinalString));
+        }
+        catch (NumberFormatException nfe) {
+            return fromShapeName(nameOrOrdinalString);
+        }
+    }
 }