1 package net.sf.openrocket.preset;
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;
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;
24 * A model for a preset component.
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.
29 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
31 // FIXME - Implement clone.
32 public class ComponentPreset implements Comparable<ComponentPreset> {
34 private final TypedPropertyMap properties = new TypedPropertyMap();
36 private boolean favorite = false;
37 private String digest = "";
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} ),
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} ),
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} ),
66 TUBE_COUPLER( new TypedKey<?>[] {
67 ComponentPreset.MANUFACTURER,
68 ComponentPreset.PARTNO,
69 ComponentPreset.OUTER_DIAMETER,
70 ComponentPreset.INNER_DIAMETER,
71 ComponentPreset.LENGTH} ),
73 BULK_HEAD( new TypedKey<?>[] {
74 ComponentPreset.MANUFACTURER,
75 ComponentPreset.PARTNO,
76 ComponentPreset.DESCRIPTION,
77 ComponentPreset.OUTER_DIAMETER,
78 ComponentPreset.LENGTH} ),
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} ),
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} );
97 Type[] compatibleTypes;
98 TypedKey<?>[] displayedColumns;
100 Type( TypedKey<?>[] displayedColumns) {
101 compatibleTypes = new Type[1];
102 compatibleTypes[0] = this;
103 this.displayedColumns = displayedColumns;
106 Type( Type[] t, TypedKey<?>[] displayedColumns ) {
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];
114 this.displayedColumns = displayedColumns;
117 public Type[] getCompatibleTypes() {
118 return compatibleTypes;
121 public TypedKey<?>[] getDisplayedColumns() {
122 return displayedColumns;
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);
146 public final static Map<String, TypedKey<?>> keyMap = new HashMap<String, TypedKey<?>>();
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);
168 public final static List<TypedKey<?>> orderedKeyList = Arrays.<TypedKey<?>>asList(
177 FORE_SHOULDER_DIAMETER,
178 FORE_SHOULDER_LENGTH,
188 // package scope constructor to encourage use of factory.
193 * Convenience method to retrieve the Type of this ComponentPreset.
197 public Type getType() {
198 return properties.get(TYPE);
202 * Convenience method to retrieve the Manufacturer of this ComponentPreset.
205 public Manufacturer getManufacturer() {
206 return properties.get(MANUFACTURER);
210 * Convenience method to retrieve the PartNo of this ComponentPreset.
213 public String getPartNo() {
214 return properties.get(PARTNO);
217 public String getDigest() {
221 public boolean has(Object key) {
222 return properties.containsKey(key);
226 * Package scope so the ComponentPresetFactory can call it.
229 void putAll(TypedPropertyMap other) {
233 properties.putAll(other);
237 * Package scope so the ComponentPresetFactory can call it.
241 <T> void put( TypedKey<T> key, T value ) {
242 properties.put(key, value);
245 public <T> T get(TypedKey<T> key) {
246 T value = properties.get(key);
248 throw new BugException("Preset did not contain key " + key + " " + properties.toString());
253 public boolean isFavorite() {
257 public void setFavorite(boolean favorite) {
258 this.favorite = favorite;
262 public int compareTo(ComponentPreset p2) {
263 int manuCompare = this.getManufacturer().getSimpleName().compareTo(p2.getManufacturer().getSimpleName());
264 if ( manuCompare != 0 )
267 int partNoCompare = this.getPartNo().compareTo(p2.getPartNo());
268 return partNoCompare;
272 public String toString() {
273 return get(MANUFACTURER).toString() + " " + get(PARTNO);
276 public String preferenceKey() {
277 return get(MANUFACTURER).toString() + "|" + get(PARTNO);
281 * Package scope so the factory can call it.
283 void computeDigest() {
286 ByteArrayOutputStream bos = new ByteArrayOutputStream();
287 DataOutputStream os = new DataOutputStream(bos);
289 List<TypedKey<?>> keys = new ArrayList<TypedKey<?>>( properties.keySet());
291 Collections.sort(keys, new Comparator<TypedKey<?>>() {
293 public int compare( TypedKey<?> a, TypedKey<?> b ) {
294 return a.getName().compareTo(b.getName());
298 for ( TypedKey<?> key : keys ) {
300 Object value = properties.get(key);
302 os.writeBytes(key.getName());
304 if ( key.getType() == Double.class ) {
305 Double d = (Double) value;
307 } else if (key.getType() == String.class ) {
308 String s = (String) value;
310 } else if (key.getType() == Manufacturer.class ) {
311 String s = ((Manufacturer)value).getSimpleName();
313 } else if ( key.getType() == Finish.class ) {
314 String s = ((Finish)value).name();
316 } else if ( key.getType() == Type.class ) {
317 String s = ((Type)value).name();
319 } else if ( key.getType() == Boolean.class ) {
320 Boolean b = (Boolean) value;
322 } else if ( key.getType() == Material.class ) {
323 double d = ((Material)value).getDensity();
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();
333 MessageDigest md5 = MessageDigest.getInstance("MD5");
334 digest = TextUtil.hexString(md5.digest( bos.toByteArray() ));
336 catch ( Exception e ) {
337 throw new BugException(e);