When creating a new user material from an existing material (ie, key), make certain...
[debian/openrocket] / core / src / net / sf / openrocket / database / Databases.java
1 package net.sf.openrocket.database;
2
3 import net.sf.openrocket.logging.LogHelper;
4 import net.sf.openrocket.material.Material;
5 import net.sf.openrocket.material.MaterialStorage;
6 import net.sf.openrocket.startup.Application;
7 import net.sf.openrocket.util.MathUtil;
8
9
10 /**
11  * A class that contains single instances of {@link Database} for specific purposes.
12  * 
13  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14  */
15 public class Databases {
16         private static final LogHelper log = Application.getLogger();
17
18         /* Static implementations of specific databases: */
19
20         /**
21          * A database of bulk materials (with bulk densities).
22          */
23         public static final Database<Material> BULK_MATERIAL = new Database<Material>();
24         /**
25          * A database of surface materials (with surface densities).
26          */
27         public static final Database<Material> SURFACE_MATERIAL = new Database<Material>();
28         /**
29          * A database of linear material (with length densities).
30          */
31         public static final Database<Material> LINE_MATERIAL = new Database<Material>();
32
33
34
35         static {
36
37                 // Add default materials
38                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Acrylic", 1190));
39                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Aluminum", 2700));
40                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Balsa", 170));
41                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Basswood", 500));
42                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Birch", 670));
43                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Brass", 8600));
44                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Cardboard", 680));
45                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Carbonfiber", 1780));
46                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Cork", 240));
47                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"DepronXPS", 40));
48                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Fiberglass", 1850));
49                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Kraftphenolic", 950));
50                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Maple", 755));
51                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Paperoffice", 820));
52                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Pine", 530));
53                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Plywoodbirch", 630));
54                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"PolycarbonateLexan", 1200));
55                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Polystyrene", 1050));
56                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"PVC", 1390));
57                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Spruce", 450));
58                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Steel", 7850));
59                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"StyrofoamgenericEPS", 20));
60                 //              BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Styrofoam (Blue foam, XPS)", 32));
61                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"StyrofoamBluefoamXPS", 32));
62                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Titanium", 4500));
63                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"Quantumtubing", 1050));
64                 BULK_MATERIAL.add(Material.newSystemMaterial(Material.Type.BULK,"BlueTube", 1300));
65
66                 SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Ripstopnylon", 0.067));
67                 SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Mylar", 0.021));
68                 SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Polyethylenethin", 0.015));
69                 SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Polyethyleneheavy", 0.040));
70                 SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Silk", 0.060));
71                 SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Paperoffice", 0.080));
72                 SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Cellophane", 0.018));
73                 SURFACE_MATERIAL.add(Material.newSystemMaterial(Material.Type.SURFACE,"Crepepaper", 0.025));
74
75                 //// Thread (heavy-duty)
76                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Threadheavy-duty", 0.0003));
77                 //// Elastic cord (round 2mm, 1/16 in)
78                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Elasticcordround2mm", 0.0018));
79                 //// Elastic cord (flat  6mm, 1/4 in)
80                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Elasticcordflat6mm", 0.0043));
81                 //// Elastic cord (flat 12mm, 1/2 in)
82                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Elasticcordflat12mm", 0.008));
83                 //// Elastic cord (flat 19mm, 3/4 in)
84                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Elasticcordflat19mm", 0.0012));
85                 //// Elastic cord (flat 25mm, 1 in)
86                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Elasticcordflat25mm", 0.0016));
87                 //// Braided nylon (2 mm, 1/16 in)
88                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Braidednylon2mm", 0.001));
89                 //// Braided nylon (3 mm, 1/8 in)
90                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Braidednylon3mm", 0.0035));
91                 //// Tubular nylon (11 mm, 7/16 in)
92                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Tubularnylon11mm", 0.013));
93                 //// Tubular nylon (14 mm, 9/16 in)
94                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Tubularnylon14mm", 0.016));
95                 //// Tubular nylon (25 mm, 1 in)
96                 LINE_MATERIAL.add(Material.newSystemMaterial(Material.Type.LINE,"Tubularnylon25mm", 0.029));
97
98
99                 // Add user-defined materials
100                 for (Material m : Application.getPreferences().getUserMaterials()) {
101                         switch (m.getType()) {
102                         case LINE:
103                                 LINE_MATERIAL.add(m);
104                                 break;
105
106                         case SURFACE:
107                                 SURFACE_MATERIAL.add(m);
108                                 break;
109
110                         case BULK:
111                                 BULK_MATERIAL.add(m);
112                                 break;
113
114                         default:
115                                 log.warn("ERROR: Unknown material type " + m);
116                         }
117                 }
118
119                 // Add database storage listener
120                 MaterialStorage listener = new MaterialStorage();
121                 LINE_MATERIAL.addDatabaseListener(listener);
122                 SURFACE_MATERIAL.addDatabaseListener(listener);
123                 BULK_MATERIAL.addDatabaseListener(listener);
124         }
125
126
127         /*
128          * Used just for ensuring initialization of the class.
129          */
130         public static void fakeMethod() {
131
132         }
133
134
135         /**
136          * Find a material from the database with the specified type and name.  Returns
137          * <code>null</code> if the specified material could not be found.
138          * 
139          * @param type  the material type.
140          * @param name  the material name in the database.
141          * @return              the material, or <code>null</code> if not found.
142          */
143         public static Material findMaterial(Material.Type type, String name) {
144                 Database<Material> db;
145                 switch (type) {
146                 case BULK:
147                         db = BULK_MATERIAL;
148                         break;
149                 case SURFACE:
150                         db = SURFACE_MATERIAL;
151                         break;
152                 case LINE:
153                         db = LINE_MATERIAL;
154                         break;
155                 default:
156                         throw new IllegalArgumentException("Illegal material type: " + type);
157                 }
158
159                 for (Material m : db) {
160                         if (m.getName().equalsIgnoreCase(name)) {
161                                 return m;
162                         }
163                 }
164                 return null;
165         }
166
167
168         /**
169          * Find a material from the database or return a new user defined material if the specified
170          * material with the specified density is not found.
171          * 
172          * @param type                  the material type.
173          * @param name                  the material name.
174          * @param density               the density of the material.
175          * @return                              the material object from the database or a new material.
176          */
177         public static Material findMaterial(Material.Type type, String key, String name, double density) {
178                 Database<Material> db;
179                 switch (type) {
180                 case BULK:
181                         db = BULK_MATERIAL;
182                         break;
183                 case SURFACE:
184                         db = SURFACE_MATERIAL;
185                         break;
186                 case LINE:
187                         db = LINE_MATERIAL;
188                         break;
189                 default:
190                         throw new IllegalArgumentException("Illegal material type: " + type);
191                 }
192
193                 Material bestMatch = null;
194
195                 // Alter the search mechanism to handle older specifications.
196                 // If a key is specified, then we match on key, if one is found.
197                 // Otherwise we return the material which matches on name.
198                 // this requires us to loop through the entire db at least once to look for the key.
199                 for (Material m : db) {
200                         // perfect match based on key.
201                         if ( key != null && m.getKey().equals(key) ) {
202                                 // Exact match
203                                 if ( MathUtil.equals(m.getDensity(), density) ) {
204                                         return m;
205                                 }
206                                 // Custom material with standard name
207                                 return Material.newUserMaterialWithKey(type, key, m.getName(), density);
208                         }
209                         if (m.getName().equalsIgnoreCase(name) && MathUtil.equals(m.getDensity(), density)) {
210                                 bestMatch = m;
211                         }
212                 }
213                 if ( bestMatch != null ) {
214                         return bestMatch;
215                 }
216                 return Material.newUserMaterial(type, name, density);
217         }
218
219
220 }