create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / rocksim / RocksimNoseConeCode.java
1 /*
2  * RocksimNoseConeCode.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import net.sf.openrocket.rocketcomponent.Transition;
7
8 import java.util.HashSet;
9 import java.util.Set;
10
11 /**
12  * Models the nose cone shape of a rocket.  Maps from Rocksim's notion to OpenRocket's.
13  */
14 public enum RocksimNoseConeCode {
15     CONICAL(0, Transition.Shape.CONICAL, "Conic", "Cone"),
16     OGIVE(1, Transition.Shape.OGIVE),
17     PARABOLIC(2, Transition.Shape.ELLIPSOID),  //Rocksim' PARABOLIC most closely resembles an ELLIPSOID in OpenRocket
18     ELLIPTICAL(3, Transition.Shape.ELLIPSOID),
19     POWER_SERIES(4, Transition.Shape.POWER),
20     PARABOLIC_SERIES(5, Transition.Shape.PARABOLIC),
21     HAACK(6, Transition.Shape.HAACK);
22
23     /**
24      * The Rocksim enumeration value. Sent in XML.
25      */
26     private final int ordinal;
27
28     /**
29      * The corresponding OpenRocket shape.
30      */
31     private final Transition.Shape shape;
32
33     /**
34      * Names of the shape that are sometimes found in NCDATA.CSV
35      */
36     private Set<String> shapeNames = new HashSet<String>();
37
38     /**
39      * Constructor.
40      *
41      * @param idx           the Rocksim shape code
42      * @param aShape        the corresponding OpenRocket shape
43      * @param theShapeNames an array of alternate names
44      */
45     private RocksimNoseConeCode(int idx, Transition.Shape aShape, String... theShapeNames) {
46         ordinal = idx;
47         shape = aShape;
48         shapeNames.add(this.name().toLowerCase());
49         if (theShapeNames != null) {
50             for (String theShapeName : theShapeNames) {
51                 shapeNames.add(theShapeName.toLowerCase());
52             }
53         }
54     }
55
56     /**
57      * Get the OpenRocket shape that corresponds to the Rocksim shape.
58      *
59      * @return a shape
60      */
61     public Transition.Shape asOpenRocket() {
62         return shape;
63     }
64
65     /**
66      * Lookup an instance of this enum based upon the Rocksim code.
67      *
68      * @param rocksimShapeCode the Rocksim code (from XML)
69      * @return an instance of this enum
70      */
71     public static RocksimNoseConeCode fromCode(int rocksimShapeCode) {
72         RocksimNoseConeCode[] values = values();
73         for (RocksimNoseConeCode value : values) {
74             if (value.ordinal == rocksimShapeCode) {
75                 return value;
76             }
77         }
78         return PARABOLIC; //Default
79     }
80
81     /**
82      * Lookup an ordinal value for the Rocksim code.
83      *
84      * @param type the OR Shape
85      * @return the Rocksim code
86      */
87     public static int toCode(Transition.Shape type) {
88         RocksimNoseConeCode[] values = values();
89         for (RocksimNoseConeCode value : values) {
90             if (value.shape.equals(type)) {
91                 if (value.ordinal == 2) {
92                     return 3;
93                 }
94                 return value.ordinal;
95             }
96         }
97         return ELLIPTICAL.ordinal; //Default
98     }
99
100     /**
101      * Given the name of a shape, map it into an instance of this enum.
102      *
103      * @param theName the name of the shape; case does not matter
104      * @return the corresponding enum instance; defaults to PARABOLIC if not found.
105      */
106     public static RocksimNoseConeCode fromShapeName(String theName) {
107         RocksimNoseConeCode[] values = values();
108         for (RocksimNoseConeCode value : values) {
109             if (value.shapeNames.contains(theName.toLowerCase())) {
110                 return value;
111             }
112         }
113         return PARABOLIC; //Default
114     }
115
116     /**
117      * Convenience method that determines if the parameter is an integer that refers to a shape code, or the name
118      * of the shape itself.  This basically combines fromCode and fromShapeName into one method.
119      *
120      * @param nameOrOrdinalString the shape number or shape name
121      * @return an instance of this enum; defaults to PARABOLIC if not found
122      */
123     public static RocksimNoseConeCode fromShapeNameOrCode(String nameOrOrdinalString) {
124         try {
125             return fromCode(Integer.parseInt(nameOrOrdinalString));
126         }
127         catch (NumberFormatException nfe) {
128             return fromShapeName(nameOrOrdinalString);
129         }
130     }
131 }