d10a88b745780c33d8d06f6f692981ae317ac67f
[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                 TypedKey<?>[] displayedColumns;
97
98                 Type( TypedKey<?>[] displayedColumns) {
99                         this.displayedColumns = displayedColumns;
100                 }
101
102                 public List<Type> getCompatibleTypes() {
103                         return compatibleTypeMap.get(Type.this);
104                 }
105
106                 public TypedKey<?>[] getDisplayedColumns() {
107                         return displayedColumns;
108                 }
109                 
110                 private static Map<Type,List<Type>> compatibleTypeMap = new HashMap<Type,List<Type>>();
111
112                 static {
113                         compatibleTypeMap.put( BODY_TUBE, Arrays.asList( BODY_TUBE, TUBE_COUPLER) );
114                         compatibleTypeMap.put( TUBE_COUPLER, Arrays.asList( BODY_TUBE,TUBE_COUPLER) );
115                         compatibleTypeMap.put( CENTERING_RING, Arrays.asList( CENTERING_RING, ENGINE_BLOCK ) );
116                         compatibleTypeMap.put( NOSE_CONE, Arrays.asList( NOSE_CONE, TRANSITION));
117                 }
118                 
119         }
120
121         public final static TypedKey<Manufacturer> MANUFACTURER = new TypedKey<Manufacturer>("Manufacturer", Manufacturer.class);
122         public final static TypedKey<String> PARTNO = new TypedKey<String>("PartNo",String.class);
123         public final static TypedKey<String> DESCRIPTION = new TypedKey<String>("Description", String.class);
124         public final static TypedKey<Type> TYPE = new TypedKey<Type>("Type",Type.class);
125         public final static TypedKey<Double> LENGTH = new TypedKey<Double>("Length", Double.class, UnitGroup.UNITS_LENGTH);
126         public final static TypedKey<Double> INNER_DIAMETER = new TypedKey<Double>("InnerDiameter", Double.class, UnitGroup.UNITS_LENGTH);
127         public final static TypedKey<Double> OUTER_DIAMETER = new TypedKey<Double>("OuterDiameter", Double.class, UnitGroup.UNITS_LENGTH);
128         public final static TypedKey<Double> SHOULDER_LENGTH = new TypedKey<Double>("ShoulderLength", Double.class, UnitGroup.UNITS_LENGTH);
129         public final static TypedKey<Double> SHOULDER_DIAMETER = new TypedKey<Double>("ShoulderDiameter", Double.class, UnitGroup.UNITS_LENGTH);
130         public final static TypedKey<Double> FORE_SHOULDER_LENGTH = new TypedKey<Double>("ForeShoulderLength",Double.class, UnitGroup.UNITS_LENGTH);
131         public final static TypedKey<Double> FORE_SHOULDER_DIAMETER = new TypedKey<Double>("ForeShoulderDiameter",Double.class, UnitGroup.UNITS_LENGTH);
132         public final static TypedKey<Double> FORE_OUTER_DIAMETER = new TypedKey<Double>("ForeOuterDiameter", Double.class, UnitGroup.UNITS_LENGTH);
133         public final static TypedKey<Shape> SHAPE = new TypedKey<Shape>("Shape", Shape.class);
134         public final static TypedKey<Material> MATERIAL = new TypedKey<Material>("Material", Material.class);
135         public final static TypedKey<Finish> FINISH = new TypedKey<Finish>("Finish", Finish.class);
136         public final static TypedKey<Double> THICKNESS = new TypedKey<Double>("Thickness", Double.class, UnitGroup.UNITS_LENGTH);
137         public final static TypedKey<Boolean> FILLED = new TypedKey<Boolean>("Filled", Boolean.class);
138         public final static TypedKey<Double> MASS = new TypedKey<Double>("Mass", Double.class, UnitGroup.UNITS_MASS);
139
140         public final static Map<String, TypedKey<?>> keyMap = new HashMap<String, TypedKey<?>>();
141         static {
142                 keyMap.put(MANUFACTURER.getName(), MANUFACTURER);
143                 keyMap.put(PARTNO.getName(), PARTNO);
144                 keyMap.put(TYPE.getName(), TYPE);
145                 keyMap.put(DESCRIPTION.getName(), DESCRIPTION);
146                 keyMap.put(LENGTH.getName(), LENGTH);
147                 keyMap.put(INNER_DIAMETER.getName(), INNER_DIAMETER);
148                 keyMap.put(OUTER_DIAMETER.getName(), OUTER_DIAMETER);
149                 keyMap.put(SHOULDER_LENGTH.getName(), SHOULDER_LENGTH);
150                 keyMap.put(SHOULDER_DIAMETER.getName(), SHOULDER_DIAMETER);
151                 keyMap.put(FORE_SHOULDER_LENGTH.getName(), FORE_SHOULDER_LENGTH);
152                 keyMap.put(FORE_SHOULDER_DIAMETER.getName(), FORE_SHOULDER_DIAMETER);
153                 keyMap.put(FORE_OUTER_DIAMETER.getName(), FORE_OUTER_DIAMETER);
154                 keyMap.put(SHAPE.getName(), SHAPE);
155                 keyMap.put(MATERIAL.getName(), MATERIAL);
156                 keyMap.put(FINISH.getName(), FINISH);
157                 keyMap.put(THICKNESS.getName(), THICKNESS);
158                 keyMap.put(FILLED.getName(), FILLED);
159                 keyMap.put(MASS.getName(), MASS);
160         }
161
162         public final static List<TypedKey<?>> orderedKeyList = Arrays.<TypedKey<?>>asList(
163                         MANUFACTURER,
164                         PARTNO,
165                         DESCRIPTION,
166                         OUTER_DIAMETER,
167                         INNER_DIAMETER,
168                         LENGTH,
169                         SHOULDER_DIAMETER,
170                         SHOULDER_LENGTH,
171                         FORE_SHOULDER_DIAMETER,
172                         FORE_SHOULDER_LENGTH,
173                         SHAPE,
174                         THICKNESS,
175                         FILLED,
176                         MASS,
177                         FINISH,
178                         MATERIAL
179                         );
180         
181         
182         // package scope constructor to encourage use of factory.
183         ComponentPreset() {
184         }
185
186         /**
187          * Convenience method to retrieve the Type of this ComponentPreset.
188          * 
189          * @return
190          */
191         public Type getType() {
192                 return properties.get(TYPE);
193         }
194
195         /**
196          * Convenience method to retrieve the Manufacturer of this ComponentPreset.
197          * @return
198          */
199         public Manufacturer getManufacturer() {
200                 return properties.get(MANUFACTURER);
201         }
202
203         /**
204          * Convenience method to retrieve the PartNo of this ComponentPreset.
205          * @return
206          */
207         public String getPartNo() {
208                 return properties.get(PARTNO);
209         }
210
211         public String getDigest() {
212                 return digest;
213         }
214
215         public boolean has(Object key) {
216                 return properties.containsKey(key);
217         }
218
219         /**
220          * Package scope so the ComponentPresetFactory can call it.
221          * @param other
222          */
223         void putAll(TypedPropertyMap other) {
224                 if (other == null) {
225                         return;
226                 }
227                 properties.putAll(other);
228         }
229
230         /**
231          * Package scope so the ComponentPresetFactory can call it.
232          * @param key
233          * @param value
234          */
235         <T> void put( TypedKey<T> key, T value ) {
236                 properties.put(key, value);
237         }
238         
239         public <T> T get(TypedKey<T> key) {
240                 T value = properties.get(key);
241                 if (value == null) {
242                         throw new BugException("Preset did not contain key " + key + " " + properties.toString());
243                 }
244                 return (T) value;
245         }
246
247         public boolean isFavorite() {
248                 return favorite;
249         }
250
251         public void setFavorite(boolean favorite) {
252                 this.favorite = favorite;
253         }
254
255         @Override
256         public int compareTo(ComponentPreset p2) {
257                 int manuCompare = this.getManufacturer().getSimpleName().compareTo(p2.getManufacturer().getSimpleName());
258                 if ( manuCompare != 0 )
259                         return manuCompare;
260
261                 int partNoCompare = this.getPartNo().compareTo(p2.getPartNo());
262                 return partNoCompare;
263         }
264
265         @Override
266         public String toString() {
267                 return get(MANUFACTURER).toString() + " " + get(PARTNO);
268         }
269
270         public String preferenceKey() {
271                 return get(MANUFACTURER).toString() + "|" + get(PARTNO);
272         }
273
274         /**
275          * Package scope so the factory can call it.
276          */
277         void computeDigest() {
278
279                 try {
280                         ByteArrayOutputStream bos = new ByteArrayOutputStream();
281                         DataOutputStream os = new DataOutputStream(bos);
282
283                         List<TypedKey<?>> keys = new ArrayList<TypedKey<?>>( properties.keySet());
284
285                         Collections.sort(keys, new Comparator<TypedKey<?>>() {
286                                 @Override
287                                 public int compare( TypedKey<?> a, TypedKey<?> b ) {
288                                         return a.getName().compareTo(b.getName());
289                                 }
290                         });
291
292                         for ( TypedKey<?> key : keys  ) {
293
294                                 Object value = properties.get(key);
295
296                                 os.writeBytes(key.getName());
297
298                                 if ( key.getType() == Double.class ) {
299                                         Double d = (Double) value;
300                                         os.writeDouble(d);
301                                 } else if (key.getType() == String.class ) {
302                                         String s = (String) value;
303                                         os.writeBytes(s);
304                                 } else if (key.getType() == Manufacturer.class ) {
305                                         String s = ((Manufacturer)value).getSimpleName();
306                                         os.writeBytes(s);
307                                 } else if ( key.getType() == Finish.class ) {
308                                         String s = ((Finish)value).name();
309                                         os.writeBytes(s);
310                                 } else if ( key.getType() == Type.class ) {
311                                         String s = ((Type)value).name();
312                                         os.writeBytes(s);
313                                 } else if ( key.getType() == Boolean.class ) {
314                                         Boolean b = (Boolean) value;
315                                         os.writeBoolean(b);
316                                 } else if ( key.getType() == Material.class ) {
317                                         double d = ((Material)value).getDensity();
318                                         os.writeDouble(d);
319                                 } else if ( key.getType() == Shape.class ) {
320                                         // FIXME - this is ugly to use the ordinal but what else?
321                                         int i = ((Shape)value).ordinal();
322                                         os.writeInt(i);
323                                 }
324
325                         }
326
327                         MessageDigest md5 = MessageDigest.getInstance("MD5");
328                         digest = TextUtil.hexString(md5.digest( bos.toByteArray() ));
329                 }
330                 catch ( Exception e ) {
331                         throw new BugException(e);
332                 }
333         }
334
335 }