0d558944042a4274014276074a89b493ddaccebc
[debian/openrocket] / core / src / net / sf / openrocket / preset / TypedKey.java
1 package net.sf.openrocket.preset;
2
3 import net.sf.openrocket.database.Databases;
4 import net.sf.openrocket.material.Material;
5 import net.sf.openrocket.rocketcomponent.ExternalComponent.Finish;
6 import net.sf.openrocket.startup.Application;
7
8 public class TypedKey<T> {
9
10         private final String name;
11         private final Class<T> type;
12         
13         public TypedKey(String name, Class<T> type) {
14                 this.name = name;
15                 this.type = type;
16         }
17
18         public String getName() {
19                 return name;
20         }
21
22         public Class<T> getType() {
23                 return type;
24         }
25
26         public Object parseFromString( String value ) {
27                 if ( type.equals(Boolean.class) ) {
28                         return Boolean.parseBoolean(value);
29                 }
30                 if ( type.isAssignableFrom(Double.class) ) {
31                         return Double.parseDouble(value);
32                 }
33                 if ( type.equals(String.class ) ) {
34                         return value;
35                 }
36                 if ( type.equals(Finish.class) ) {
37                         return Finish.valueOf(value);
38                 }
39                 if ( type.equals(Material.class) ) {
40                         // need to translate the value first!
41                         String translated_value = Application.getTranslator().get(value);
42                         Material material;
43                         material = Databases.findMaterial(Material.Type.BULK, translated_value);
44                         if ( material != null ) {
45                                 return material;
46                         }
47                         material = Databases.findMaterial(Material.Type.LINE, translated_value);
48                         if ( material != null ) {
49                                 return material;
50                         }
51                         material = Databases.findMaterial(Material.Type.SURFACE, translated_value);
52                         if ( material != null ) {
53                                 return material;
54                         }
55                         throw new IllegalArgumentException("Invalid material " + value + " in component preset.");
56                 }
57                 throw new IllegalArgumentException("Inavlid type " + type.getName() + " for component preset parameter " + name);
58         }
59
60         @Override
61         public int hashCode() {
62                 final int prime = 31;
63                 int result = 1;
64                 result = prime * result + ((name == null) ? 0 : name.hashCode());
65                 result = prime * result + ((type == null) ? 0 : type.hashCode());
66                 return result;
67         }
68
69         @Override
70         public boolean equals(Object obj) {
71                 if (this == obj)
72                         return true;
73                 if (obj == null)
74                         return false;
75                 if (getClass() != obj.getClass())
76                         return false;
77                 TypedKey other = (TypedKey) obj;
78                 if (name == null) {
79                         if (other.name != null)
80                                 return false;
81                 } else if (!name.equals(other.name))
82                         return false;
83                 if (type == null) {
84                         if (other.type != null)
85                                 return false;
86                 } else if (!type.equals(other.type))
87                         return false;
88                 return true;
89         }
90         
91 }