2 * RocksimNoseConeCode.java
4 package net.sf.openrocket.file.rocksim;
6 import net.sf.openrocket.rocketcomponent.Transition;
8 import java.util.HashSet;
12 * Models the nose cone shape of a rocket. Maps from Rocksim's notion to OpenRocket's.
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);
24 * The Rocksim enumeration value. Sent in XML.
26 private final int ordinal;
29 * The corresponding OpenRocket shape.
31 private final Transition.Shape shape;
34 * Names of the shape that are sometimes found in NCDATA.CSV
36 private Set<String> shapeNames = new HashSet<String>();
41 * @param idx the Rocksim shape code
42 * @param aShape the corresponding OpenRocket shape
43 * @param theShapeNames an array of alternate names
45 private RocksimNoseConeCode(int idx, Transition.Shape aShape, String... theShapeNames) {
48 shapeNames.add(this.name().toLowerCase());
49 if (theShapeNames != null) {
50 for (String theShapeName : theShapeNames) {
51 shapeNames.add(theShapeName.toLowerCase());
57 * Get the OpenRocket shape that corresponds to the Rocksim shape.
61 public Transition.Shape asOpenRocket() {
66 * Lookup an instance of this enum based upon the Rocksim code.
68 * @param rocksimShapeCode the Rocksim code (from XML)
69 * @return an instance of this enum
71 public static RocksimNoseConeCode fromCode(int rocksimShapeCode) {
72 RocksimNoseConeCode[] values = values();
73 for (RocksimNoseConeCode value : values) {
74 if (value.ordinal == rocksimShapeCode) {
78 return PARABOLIC; //Default
82 * Lookup an ordinal value for the Rocksim code.
84 * @param type the OR Shape
85 * @return the Rocksim code
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) {
97 return ELLIPTICAL.ordinal; //Default
101 * Given the name of a shape, map it into an instance of this enum.
103 * @param theName the name of the shape; case does not matter
104 * @return the corresponding enum instance; defaults to PARABOLIC if not found.
106 public static RocksimNoseConeCode fromShapeName(String theName) {
107 RocksimNoseConeCode[] values = values();
108 for (RocksimNoseConeCode value : values) {
109 if (value.shapeNames.contains(theName.toLowerCase())) {
113 return PARABOLIC; //Default
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.
120 * @param nameOrOrdinalString the shape number or shape name
121 * @return an instance of this enum; defaults to PARABOLIC if not found
123 public static RocksimNoseConeCode fromShapeNameOrCode(String nameOrOrdinalString) {
125 return fromCode(Integer.parseInt(nameOrOrdinalString));
127 catch (NumberFormatException nfe) {
128 return fromShapeName(nameOrOrdinalString);