Added centering rings and engine blocks presets
[debian/openrocket] / core / src / net / sf / openrocket / preset / ComponentPreset.java
1 package net.sf.openrocket.preset;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.DataOutputStream;
5 import java.security.MessageDigest;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.Collections;
9 import java.util.Comparator;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import net.sf.openrocket.material.Material;
15 import net.sf.openrocket.motor.Manufacturer;
16 import net.sf.openrocket.rocketcomponent.ExternalComponent.Finish;
17 import net.sf.openrocket.rocketcomponent.Transition.Shape;
18 import net.sf.openrocket.unit.UnitGroup;
19 import net.sf.openrocket.util.BugException;
20 import net.sf.openrocket.util.TextUtil;
21
22
23 /**
24  * A model for a preset component.
25  * <p>
26  * A preset component contains a component class type, manufacturer information,
27  * part information, and a method that returns a prototype of the preset component.
28  * 
29  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
30  */
31 // FIXME - Implement clone.
32 public class ComponentPreset implements Comparable<ComponentPreset> {
33
34         private final TypedPropertyMap properties = new TypedPropertyMap();
35
36         private boolean favorite = false;
37         private String digest = "";
38
39         public enum Type {
40                 BODY_TUBE( new TypedKey<?>[] {
41                                 ComponentPreset.MANUFACTURER,
42                                 ComponentPreset.PARTNO,
43                                 ComponentPreset.DESCRIPTION,
44                                 ComponentPreset.INNER_DIAMETER,
45                                 ComponentPreset.OUTER_DIAMETER,
46                                 ComponentPreset.LENGTH} ),
47                                 
48                 NOSE_CONE( new TypedKey<?>[] {
49                                 ComponentPreset.MANUFACTURER,
50                                 ComponentPreset.PARTNO,
51                                 ComponentPreset.DESCRIPTION,
52                                 ComponentPreset.SHAPE,
53                                 ComponentPreset.OUTER_DIAMETER,
54                                 ComponentPreset.SHOULDER_DIAMETER,
55                                 ComponentPreset.LENGTH} ),
56
57                 TRANSITION( new TypedKey<?>[] {
58                                 ComponentPreset.MANUFACTURER,
59                                 ComponentPreset.PARTNO,
60                                 ComponentPreset.DESCRIPTION,
61                                 ComponentPreset.SHAPE,
62                                 ComponentPreset.FORE_OUTER_DIAMETER,
63                                 ComponentPreset.OUTER_DIAMETER,
64                                 ComponentPreset.LENGTH} ),
65                                 
66                 TUBE_COUPLER( new TypedKey<?>[] {
67                                 ComponentPreset.MANUFACTURER,
68                                 ComponentPreset.PARTNO,
69                                 ComponentPreset.OUTER_DIAMETER,
70                                 ComponentPreset.INNER_DIAMETER,
71                                 ComponentPreset.LENGTH} ),
72                                                 
73                 BULK_HEAD( new TypedKey<?>[] {
74                                 ComponentPreset.MANUFACTURER,
75                                 ComponentPreset.PARTNO,
76                                 ComponentPreset.DESCRIPTION,
77                                 ComponentPreset.OUTER_DIAMETER,
78                                 ComponentPreset.LENGTH} ),
79
80                 CENTERING_RING( new TypedKey<?>[] {
81                                 ComponentPreset.MANUFACTURER,
82                                 ComponentPreset.PARTNO,
83                                 ComponentPreset.DESCRIPTION,
84                                 ComponentPreset.INNER_DIAMETER,
85                                 ComponentPreset.OUTER_DIAMETER,
86                                 ComponentPreset.LENGTH} ),
87
88                 ENGINE_BLOCK( new TypedKey<?>[] {
89                                 ComponentPreset.MANUFACTURER,
90                                 ComponentPreset.PARTNO,
91                                 ComponentPreset.DESCRIPTION,
92                                 ComponentPreset.INNER_DIAMETER,
93                                 ComponentPreset.OUTER_DIAMETER,
94                                 ComponentPreset.LENGTH} );
95
96
97                 Type[] compatibleTypes;
98                 TypedKey<?>[] displayedColumns;
99
100                 Type( TypedKey<?>[] displayedColumns) {
101                         compatibleTypes = new Type[1];
102                         compatibleTypes[0] = this;
103                         this.displayedColumns = displayedColumns;
104                 }
105
106                 Type( Type[] t, TypedKey<?>[] displayedColumns ) {
107
108                         compatibleTypes = new Type[t.length+1];
109                         compatibleTypes[0] = this;
110                         for( int i=0; i<t.length; i++ ) {
111                                 compatibleTypes[i+1] = t[i];
112                         }
113
114                         this.displayedColumns = displayedColumns;
115                 }
116
117                 public Type[] getCompatibleTypes() {
118                         return compatibleTypes;
119                 }
120
121                 public TypedKey<?>[] getDisplayedColumns() {
122                         return displayedColumns;
123                 }
124
125         }
126
127         public final static TypedKey<Manufacturer> MANUFACTURER = new TypedKey<Manufacturer>("Manufacturer", Manufacturer.class);
128         public final static TypedKey<String> PARTNO = new TypedKey<String>("PartNo",String.class);
129         public final static TypedKey<String> DESCRIPTION = new TypedKey<String>("Description", String.class);
130         public final static TypedKey<Type> TYPE = new TypedKey<Type>("Type",Type.class);
131         public final static TypedKey<Double> LENGTH = new TypedKey<Double>("Length", Double.class, UnitGroup.UNITS_LENGTH);
132         public final static TypedKey<Double> INNER_DIAMETER = new TypedKey<Double>("InnerDiameter", Double.class, UnitGroup.UNITS_LENGTH);
133         public final static TypedKey<Double> OUTER_DIAMETER = new TypedKey<Double>("OuterDiameter", Double.class, UnitGroup.UNITS_LENGTH);
134         public final static TypedKey<Double> SHOULDER_LENGTH = new TypedKey<Double>("ShoulderLength", Double.class, UnitGroup.UNITS_LENGTH);
135         public final static TypedKey<Double> SHOULDER_DIAMETER = new TypedKey<Double>("ShoulderDiameter", Double.class, UnitGroup.UNITS_LENGTH);
136         public final static TypedKey<Double> FORE_SHOULDER_LENGTH = new TypedKey<Double>("ForeShoulderLength",Double.class, UnitGroup.UNITS_LENGTH);
137         public final static TypedKey<Double> FORE_SHOULDER_DIAMETER = new TypedKey<Double>("ForeShoulderDiameter",Double.class, UnitGroup.UNITS_LENGTH);
138         public final static TypedKey<Double> FORE_OUTER_DIAMETER = new TypedKey<Double>("ForeOuterDiameter", Double.class, UnitGroup.UNITS_LENGTH);
139         public final static TypedKey<Shape> SHAPE = new TypedKey<Shape>("Shape", Shape.class);
140         public final static TypedKey<Material> MATERIAL = new TypedKey<Material>("Material", Material.class);
141         public final static TypedKey<Finish> FINISH = new TypedKey<Finish>("Finish", Finish.class);
142         public final static TypedKey<Double> THICKNESS = new TypedKey<Double>("Thickness", Double.class, UnitGroup.UNITS_LENGTH);
143         public final static TypedKey<Boolean> FILLED = new TypedKey<Boolean>("Filled", Boolean.class);
144         public final static TypedKey<Double> MASS = new TypedKey<Double>("Mass", Double.class, UnitGroup.UNITS_MASS);
145
146         public final static Map<String, TypedKey<?>> keyMap = new HashMap<String, TypedKey<?>>();
147         static {
148                 keyMap.put(MANUFACTURER.getName(), MANUFACTURER);
149                 keyMap.put(PARTNO.getName(), PARTNO);
150                 keyMap.put(TYPE.getName(), TYPE);
151                 keyMap.put(DESCRIPTION.getName(), DESCRIPTION);
152                 keyMap.put(LENGTH.getName(), LENGTH);
153                 keyMap.put(INNER_DIAMETER.getName(), INNER_DIAMETER);
154                 keyMap.put(OUTER_DIAMETER.getName(), OUTER_DIAMETER);
155                 keyMap.put(SHOULDER_LENGTH.getName(), SHOULDER_LENGTH);
156                 keyMap.put(SHOULDER_DIAMETER.getName(), SHOULDER_DIAMETER);
157                 keyMap.put(FORE_SHOULDER_LENGTH.getName(), FORE_SHOULDER_LENGTH);
158                 keyMap.put(FORE_SHOULDER_DIAMETER.getName(), FORE_SHOULDER_DIAMETER);
159                 keyMap.put(FORE_OUTER_DIAMETER.getName(), FORE_OUTER_DIAMETER);
160                 keyMap.put(SHAPE.getName(), SHAPE);
161                 keyMap.put(MATERIAL.getName(), MATERIAL);
162                 keyMap.put(FINISH.getName(), FINISH);
163                 keyMap.put(THICKNESS.getName(), THICKNESS);
164                 keyMap.put(FILLED.getName(), FILLED);
165                 keyMap.put(MASS.getName(), MASS);
166         }
167
168         public final static List<TypedKey<?>> orderedKeyList = Arrays.<TypedKey<?>>asList(
169                         MANUFACTURER,
170                         PARTNO,
171                         DESCRIPTION,
172                         OUTER_DIAMETER,
173                         INNER_DIAMETER,
174                         LENGTH,
175                         SHOULDER_DIAMETER,
176                         SHOULDER_LENGTH,
177                         FORE_SHOULDER_DIAMETER,
178                         FORE_SHOULDER_LENGTH,
179                         SHAPE,
180                         THICKNESS,
181                         FILLED,
182                         MASS,
183                         FINISH,
184                         MATERIAL
185                         );
186         
187         
188         // package scope constructor to encourage use of factory.
189         ComponentPreset() {
190         }
191
192         /**
193          * Convenience method to retrieve the Type of this ComponentPreset.
194          * 
195          * @return
196          */
197         public Type getType() {
198                 return properties.get(TYPE);
199         }
200
201         /**
202          * Convenience method to retrieve the Manufacturer of this ComponentPreset.
203          * @return
204          */
205         public Manufacturer getManufacturer() {
206                 return properties.get(MANUFACTURER);
207         }
208
209         /**
210          * Convenience method to retrieve the PartNo of this ComponentPreset.
211          * @return
212          */
213         public String getPartNo() {
214                 return properties.get(PARTNO);
215         }
216
217         public String getDigest() {
218                 return digest;
219         }
220
221         public boolean has(Object key) {
222                 return properties.containsKey(key);
223         }
224
225         /**
226          * Package scope so the ComponentPresetFactory can call it.
227          * @param other
228          */
229         void putAll(TypedPropertyMap other) {
230                 if (other == null) {
231                         return;
232                 }
233                 properties.putAll(other);
234         }
235
236         /**
237          * Package scope so the ComponentPresetFactory can call it.
238          * @param key
239          * @param value
240          */
241         <T> void put( TypedKey<T> key, T value ) {
242                 properties.put(key, value);
243         }
244         
245         public <T> T get(TypedKey<T> key) {
246                 T value = properties.get(key);
247                 if (value == null) {
248                         throw new BugException("Preset did not contain key " + key + " " + properties.toString());
249                 }
250                 return (T) value;
251         }
252
253         public boolean isFavorite() {
254                 return favorite;
255         }
256
257         public void setFavorite(boolean favorite) {
258                 this.favorite = favorite;
259         }
260
261         @Override
262         public int compareTo(ComponentPreset p2) {
263                 int manuCompare = this.getManufacturer().getSimpleName().compareTo(p2.getManufacturer().getSimpleName());
264                 if ( manuCompare != 0 )
265                         return manuCompare;
266
267                 int partNoCompare = this.getPartNo().compareTo(p2.getPartNo());
268                 return partNoCompare;
269         }
270
271         @Override
272         public String toString() {
273                 return get(MANUFACTURER).toString() + " " + get(PARTNO);
274         }
275
276         public String preferenceKey() {
277                 return get(MANUFACTURER).toString() + "|" + get(PARTNO);
278         }
279
280         /**
281          * Package scope so the factory can call it.
282          */
283         void computeDigest() {
284
285                 try {
286                         ByteArrayOutputStream bos = new ByteArrayOutputStream();
287                         DataOutputStream os = new DataOutputStream(bos);
288
289                         List<TypedKey<?>> keys = new ArrayList<TypedKey<?>>( properties.keySet());
290
291                         Collections.sort(keys, new Comparator<TypedKey<?>>() {
292                                 @Override
293                                 public int compare( TypedKey<?> a, TypedKey<?> b ) {
294                                         return a.getName().compareTo(b.getName());
295                                 }
296                         });
297
298                         for ( TypedKey<?> key : keys  ) {
299
300                                 Object value = properties.get(key);
301
302                                 os.writeBytes(key.getName());
303
304                                 if ( key.getType() == Double.class ) {
305                                         Double d = (Double) value;
306                                         os.writeDouble(d);
307                                 } else if (key.getType() == String.class ) {
308                                         String s = (String) value;
309                                         os.writeBytes(s);
310                                 } else if (key.getType() == Manufacturer.class ) {
311                                         String s = ((Manufacturer)value).getSimpleName();
312                                         os.writeBytes(s);
313                                 } else if ( key.getType() == Finish.class ) {
314                                         String s = ((Finish)value).name();
315                                         os.writeBytes(s);
316                                 } else if ( key.getType() == Type.class ) {
317                                         String s = ((Type)value).name();
318                                         os.writeBytes(s);
319                                 } else if ( key.getType() == Boolean.class ) {
320                                         Boolean b = (Boolean) value;
321                                         os.writeBoolean(b);
322                                 } else if ( key.getType() == Material.class ) {
323                                         double d = ((Material)value).getDensity();
324                                         os.writeDouble(d);
325                                 } else if ( key.getType() == Shape.class ) {
326                                         // FIXME - this is ugly to use the ordinal but what else?
327                                         int i = ((Shape)value).ordinal();
328                                         os.writeInt(i);
329                                 }
330
331                         }
332
333                         MessageDigest md5 = MessageDigest.getInstance("MD5");
334                         digest = TextUtil.hexString(md5.digest( bos.toByteArray() ));
335                 }
336                 catch ( Exception e ) {
337                         throw new BugException(e);
338                 }
339         }
340
341 }