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} );
96 TypedKey<?>[] displayedColumns;
98 Type( TypedKey<?>[] displayedColumns) {
99 this.displayedColumns = displayedColumns;
102 public List<Type> getCompatibleTypes() {
103 return compatibleTypeMap.get(Type.this);
106 public TypedKey<?>[] getDisplayedColumns() {
107 return displayedColumns;
110 private static Map<Type,List<Type>> compatibleTypeMap = new HashMap<Type,List<Type>>();
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));
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);
140 public final static Map<String, TypedKey<?>> keyMap = new HashMap<String, TypedKey<?>>();
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);
162 public final static List<TypedKey<?>> orderedKeyList = Arrays.<TypedKey<?>>asList(
171 FORE_SHOULDER_DIAMETER,
172 FORE_SHOULDER_LENGTH,
182 // package scope constructor to encourage use of factory.
187 * Convenience method to retrieve the Type of this ComponentPreset.
191 public Type getType() {
192 return properties.get(TYPE);
196 * Convenience method to retrieve the Manufacturer of this ComponentPreset.
199 public Manufacturer getManufacturer() {
200 return properties.get(MANUFACTURER);
204 * Convenience method to retrieve the PartNo of this ComponentPreset.
207 public String getPartNo() {
208 return properties.get(PARTNO);
211 public String getDigest() {
215 public boolean has(Object key) {
216 return properties.containsKey(key);
220 * Package scope so the ComponentPresetFactory can call it.
223 void putAll(TypedPropertyMap other) {
227 properties.putAll(other);
231 * Package scope so the ComponentPresetFactory can call it.
235 <T> void put( TypedKey<T> key, T value ) {
236 properties.put(key, value);
239 public <T> T get(TypedKey<T> key) {
240 T value = properties.get(key);
242 throw new BugException("Preset did not contain key " + key + " " + properties.toString());
247 public boolean isFavorite() {
251 public void setFavorite(boolean favorite) {
252 this.favorite = favorite;
256 public int compareTo(ComponentPreset p2) {
257 int manuCompare = this.getManufacturer().getSimpleName().compareTo(p2.getManufacturer().getSimpleName());
258 if ( manuCompare != 0 )
261 int partNoCompare = this.getPartNo().compareTo(p2.getPartNo());
262 return partNoCompare;
266 public String toString() {
267 return get(MANUFACTURER).toString() + " " + get(PARTNO);
270 public String preferenceKey() {
271 return get(MANUFACTURER).toString() + "|" + get(PARTNO);
275 * Package scope so the factory can call it.
277 void computeDigest() {
280 ByteArrayOutputStream bos = new ByteArrayOutputStream();
281 DataOutputStream os = new DataOutputStream(bos);
283 List<TypedKey<?>> keys = new ArrayList<TypedKey<?>>( properties.keySet());
285 Collections.sort(keys, new Comparator<TypedKey<?>>() {
287 public int compare( TypedKey<?> a, TypedKey<?> b ) {
288 return a.getName().compareTo(b.getName());
292 for ( TypedKey<?> key : keys ) {
294 Object value = properties.get(key);
296 os.writeBytes(key.getName());
298 if ( key.getType() == Double.class ) {
299 Double d = (Double) value;
301 } else if (key.getType() == String.class ) {
302 String s = (String) value;
304 } else if (key.getType() == Manufacturer.class ) {
305 String s = ((Manufacturer)value).getSimpleName();
307 } else if ( key.getType() == Finish.class ) {
308 String s = ((Finish)value).name();
310 } else if ( key.getType() == Type.class ) {
311 String s = ((Type)value).name();
313 } else if ( key.getType() == Boolean.class ) {
314 Boolean b = (Boolean) value;
316 } else if ( key.getType() == Material.class ) {
317 double d = ((Material)value).getDensity();
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();
327 MessageDigest md5 = MessageDigest.getInstance("MD5");
328 digest = TextUtil.hexString(md5.digest( bos.toByteArray() ));
330 catch ( Exception e ) {
331 throw new BugException(e);