create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / rocksim / RocksimDensityType.java
1 /*
2  * RocksimDensityType.java
3  */
4 package net.sf.openrocket.file.rocksim;
5
6 import net.sf.openrocket.material.Material;
7
8 /**
9  * Models the nose cone shape of a rocket.  Maps from Rocksim's notion to OpenRocket's.
10  */
11 public enum RocksimDensityType {
12     ROCKSIM_BULK   (0, RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_BULK_DENSITY),
13     ROCKSIM_SURFACE(1, RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_SURFACE_DENSITY),
14     ROCKSIM_LINE   (2, RocksimCommonConstants.ROCKSIM_TO_OPENROCKET_LINE_DENSITY);
15
16     /** The Rocksim enumeration value. Sent in XML. */
17     private final int ordinal;
18
19     /** The corresponding OpenRocket shape. */
20     private final double conversion;
21
22     /**
23      * Constructor.
24      *
25      * @param idx            the Rocksim shape code
26      * @param theConversion  the numerical conversion ratio to OpenRocket
27      */
28     private RocksimDensityType(int idx, double theConversion) {
29         ordinal = idx;
30         conversion = theConversion;
31     }
32
33     /**
34      * Get the OpenRocket shape that corresponds to the Rocksim value.
35      *
36      * @return a conversion
37      */
38     public double asOpenRocket() {
39         return conversion;
40     }
41
42     /**
43      * Lookup an instance of this enum based upon the Rocksim code.
44      *
45      * @param rocksimDensityType  the Rocksim code (from XML)
46      * @return an instance of this enum
47      */
48     public static RocksimDensityType fromCode(int rocksimDensityType) {
49         RocksimDensityType[] values = values();
50         for (RocksimDensityType value : values) {
51             if (value.ordinal == rocksimDensityType) {
52                 return value;
53             }
54         }
55         return ROCKSIM_BULK; //Default
56     }
57
58     /**
59      * Get the ordinal code.
60      *
61      * @param type  the OR type
62      *
63      * @return  the Rocksim XML value
64      */
65     public static int toCode(Material.Type type) {
66         if (type.equals(Material.Type.BULK)) {
67             return ROCKSIM_BULK.ordinal;
68         }
69         if (type.equals(Material.Type.LINE)) {
70             return ROCKSIM_LINE.ordinal;
71         }
72         if (type.equals(Material.Type.SURFACE)) {
73             return ROCKSIM_SURFACE.ordinal;
74         }
75         return ROCKSIM_BULK.ordinal;
76     }
77 }
78