1 package net.sf.openrocket.preset;
3 import org.jfree.util.StringUtils;
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;
11 public class TypedKey<T> {
13 private final String name;
14 private final Class<T> type;
15 private final UnitGroup unitGroup;
17 public TypedKey(String name, Class<T> type) {
18 this(name, type, null);
21 public TypedKey(String name, Class<T> type, UnitGroup unitGroup) {
24 this.unitGroup = unitGroup;
28 public String toString() {
29 return "TypedKey [name=" + name + "]";
32 public String getName() {
36 public Class<T> getType() {
40 public UnitGroup getUnitGroup() {
44 public Object parseFromString( String value ) {
45 if ( type.equals(Manufacturer.class)) {
46 Manufacturer m = Manufacturer.getManufacturer(value);
49 if ( type.equals(ComponentPreset.Type.class) ) {
50 ComponentPreset.Type t = ComponentPreset.Type.valueOf(value);
53 if ( type.equals(Boolean.class) ) {
54 return Boolean.parseBoolean(value);
56 if ( type.isAssignableFrom(Double.class) ) {
57 return Double.parseDouble(value);
59 if ( type.equals(String.class ) ) {
62 if ( type.equals(Finish.class) ) {
63 return Finish.valueOf(value);
65 if ( type.equals(Material.class) ) {
66 // FIXME - cannot parse Materials just yet. Need a way to do it without worrying about locale.
69 String translated_value = Application.getTranslator().get(value);
71 material = Databases.findMaterial(Material.Type.BULK, translated_value);
72 if ( material != null ) {
75 material = Databases.findMaterial(Material.Type.LINE, translated_value);
76 if ( material != null ) {
79 material = Databases.findMaterial(Material.Type.SURFACE, translated_value);
80 if ( material != null ) {
83 throw new IllegalArgumentException("Invalid material " + value + " in component preset.");
86 if ( type.equals(Shape.class) ) {
87 //FIXME - ignore case!
88 if ( "ogive".equalsIgnoreCase(value) ) {
91 if ( "cone".equalsIgnoreCase(value) ) {
94 if ( "elliptical".equalsIgnoreCase(value) ) {
95 return Shape.ELLIPSOID;
97 if ( "parabolic".equalsIgnoreCase(value) ) {
98 return Shape.PARABOLIC;
100 if ( "sears-haack".equalsIgnoreCase(value) ) {
103 if ( "power-series".equalsIgnoreCase(value) ) {
106 throw new IllegalArgumentException("Invalid shape " + value + " in component preset.");
108 throw new IllegalArgumentException("Inavlid type " + type.getName() + " for component preset parameter " + name);
112 public int hashCode() {
113 final int prime = 31;
115 result = prime * result + ((name == null) ? 0 : name.hashCode());
116 result = prime * result + ((type == null) ? 0 : type.hashCode());
121 public boolean equals(Object obj) {
126 if (getClass() != obj.getClass())
128 TypedKey other = (TypedKey) obj;
130 if (other.name != null)
132 } else if (!name.equals(other.name))
135 if (other.type != null)
137 } else if (!type.equals(other.type))