First cut at making component presets work for nose cones.
[debian/openrocket] / core / src / net / sf / openrocket / preset / TypedKey.java
1 package net.sf.openrocket.preset;
2
3 import org.jfree.util.StringUtils;
4
5 import net.sf.openrocket.material.Material;
6 import net.sf.openrocket.motor.Manufacturer;
7 import net.sf.openrocket.rocketcomponent.ExternalComponent.Finish;
8 import net.sf.openrocket.rocketcomponent.Transition.Shape;
9 import net.sf.openrocket.unit.UnitGroup;
10
11 public class TypedKey<T> {
12
13         private final String name;
14         private final Class<T> type;
15         private final UnitGroup unitGroup;
16         
17         public TypedKey(String name, Class<T> type) {
18                 this(name, type, null);
19         }
20         
21         public TypedKey(String name, Class<T> type, UnitGroup unitGroup) {
22                 this.name = name;
23                 this.type = type;
24                 this.unitGroup = unitGroup;
25         }
26
27         @Override
28         public String toString() {
29                 return "TypedKey [name=" + name + "]";
30         }
31
32         public String getName() {
33                 return name;
34         }
35
36         public Class<T> getType() {
37                 return type;
38         }
39
40         public UnitGroup getUnitGroup() {
41                 return unitGroup;
42         }
43
44         public Object parseFromString( String value ) {
45                 if ( type.equals(Manufacturer.class)) {
46                         Manufacturer m = Manufacturer.getManufacturer(value);
47                         return m;
48                 }
49                 if ( type.equals(ComponentPreset.Type.class) ) {
50                         ComponentPreset.Type t = ComponentPreset.Type.valueOf(value);
51                         return t;
52                 }
53                 if ( type.equals(Boolean.class) ) {
54                         return Boolean.parseBoolean(value);
55                 }
56                 if ( type.isAssignableFrom(Double.class) ) {
57                         return Double.parseDouble(value);
58                 }
59                 if ( type.equals(String.class ) ) {
60                         return value;
61                 }
62                 if ( type.equals(Finish.class) ) {
63                         return Finish.valueOf(value);
64                 }
65                 if ( type.equals(Material.class) ) {
66                         // FIXME - cannot parse Materials just yet.  Need a way to do it without worrying about locale.
67                         return null;
68                         /*
69                         String translated_value = Application.getTranslator().get(value);
70                         Material material;
71                         material = Databases.findMaterial(Material.Type.BULK, translated_value);
72                         if ( material != null ) {
73                                 return material;
74                         }
75                         material = Databases.findMaterial(Material.Type.LINE, translated_value);
76                         if ( material != null ) {
77                                 return material;
78                         }
79                         material = Databases.findMaterial(Material.Type.SURFACE, translated_value);
80                         if ( material != null ) {
81                                 return material;
82                         }
83                         throw new IllegalArgumentException("Invalid material " + value + " in component preset.");
84                         */
85                 }
86                 if ( type.equals(Shape.class) ) {
87                         //FIXME - ignore case!
88                         if ( "ogive".equalsIgnoreCase(value) ) {
89                                 return Shape.OGIVE;
90                         }
91                         if ( "cone".equalsIgnoreCase(value) ) {
92                                 return Shape.CONICAL;
93                         }
94                         if ( "elliptical".equalsIgnoreCase(value) ) {
95                                 return Shape.ELLIPSOID;
96                         }
97                         if ( "parabolic".equalsIgnoreCase(value) ) {
98                                 return Shape.PARABOLIC;
99                         }
100                         if ( "sears-haack".equalsIgnoreCase(value) ) {
101                                 return Shape.HAACK;
102                         }
103                         if ( "power-series".equalsIgnoreCase(value) ) {
104                                 return Shape.POWER;
105                         }
106                         throw new IllegalArgumentException("Invalid shape " + value + " in component preset.");
107                 }
108                 throw new IllegalArgumentException("Inavlid type " + type.getName() + " for component preset parameter " + name);
109         }
110
111         @Override
112         public int hashCode() {
113                 final int prime = 31;
114                 int result = 1;
115                 result = prime * result + ((name == null) ? 0 : name.hashCode());
116                 result = prime * result + ((type == null) ? 0 : type.hashCode());
117                 return result;
118         }
119
120         @Override
121         public boolean equals(Object obj) {
122                 if (this == obj)
123                         return true;
124                 if (obj == null)
125                         return false;
126                 if (getClass() != obj.getClass())
127                         return false;
128                 TypedKey other = (TypedKey) obj;
129                 if (name == null) {
130                         if (other.name != null)
131                                 return false;
132                 } else if (!name.equals(other.name))
133                         return false;
134                 if (type == null) {
135                         if (other.type != null)
136                                 return false;
137                 } else if (!type.equals(other.type))
138                         return false;
139                 return true;
140         }
141         
142 }