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