Initial commit
[debian/openrocket] / src / net / sf / openrocket / database / Databases.java
1 package net.sf.openrocket.database;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6
7 import net.sf.openrocket.file.MotorLoader;
8 import net.sf.openrocket.material.Material;
9 import net.sf.openrocket.rocketcomponent.Motor;
10 import net.sf.openrocket.util.MathUtil;
11
12
13 /**
14  * A class that contains single instances of {@link Database} for specific purposes.
15  * 
16  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
17  */
18 public class Databases {
19
20         /* Static implementations of specific databases: */
21         /**
22          * The motor database.
23          */
24         public static final Database<Motor> MOTOR = new Database<Motor>(new MotorLoader());
25         
26         
27         /**
28          * A database of bulk materials (with bulk densities).
29          */
30         public static final Database<Material> BULK_MATERIAL = new Database<Material>();
31         /**
32          * A database of surface materials (with surface densities).
33          */
34         public static final Database<Material> SURFACE_MATERIAL = new Database<Material>();
35         /**
36          * A database of linear material (with length densities).
37          */
38         public static final Database<Material> LINE_MATERIAL = new Database<Material>();
39         
40         
41
42         // TODO: HIGH: loading the thrust curves and other databases
43         static {
44                 
45                 try {
46                         MOTOR.loadJarDirectory("datafiles/thrustcurves/", ".*\\.[eE][nN][gG]$");
47                 } catch (IOException e) {
48                         System.out.println("Could not read thrust curves from JAR: "+e.getMessage());
49                         
50                         try {
51                                 MOTOR.loadDirectory(new File("datafiles/thrustcurves/"),".*\\.[eE][nN][gG]$");
52                         } catch (IOException e1) {
53                                 System.out.println("Could not read thrust curves from directory either.");
54                                 throw new RuntimeException(e1);
55                         }
56                 }
57         }
58         
59         // TODO: HIGH: Move materials into data files
60         static {
61                 
62                 BULK_MATERIAL.add(new Material.Bulk("Acrylic",          1190));
63                 BULK_MATERIAL.add(new Material.Bulk("Balsa",             170));
64                 BULK_MATERIAL.add(new Material.Bulk("Birch",             670));
65                 BULK_MATERIAL.add(new Material.Bulk("Cardboard",         680));
66                 BULK_MATERIAL.add(new Material.Bulk("Carbon fiber",     1780));
67                 BULK_MATERIAL.add(new Material.Bulk("Cork",                      240));
68                 BULK_MATERIAL.add(new Material.Bulk("Fiberglass",       1850));
69                 BULK_MATERIAL.add(new Material.Bulk("Kraft phenolic",950));
70                 BULK_MATERIAL.add(new Material.Bulk("Maple",             755));
71                 BULK_MATERIAL.add(new Material.Bulk("Paper (office)",820));
72                 BULK_MATERIAL.add(new Material.Bulk("Pine",                      530));
73                 BULK_MATERIAL.add(new Material.Bulk("Plywood (birch)",630));
74                 BULK_MATERIAL.add(new Material.Bulk("Polycarbonate (Lexan)",1200));
75                 BULK_MATERIAL.add(new Material.Bulk("Polystyrene",  1050));
76                 BULK_MATERIAL.add(new Material.Bulk("PVC",                      1390));
77                 BULK_MATERIAL.add(new Material.Bulk("Spruce",            450));
78                 BULK_MATERIAL.add(new Material.Bulk("Quantum tubing",1050));
79                 
80                 SURFACE_MATERIAL.add(new Material.Surface("Ripstop nylon",                      0.067));
81                 SURFACE_MATERIAL.add(new Material.Surface("Mylar",                                      0.021));
82                 SURFACE_MATERIAL.add(new Material.Surface("Polyethylene (thin)",        0.015));
83                 SURFACE_MATERIAL.add(new Material.Surface("Polyethylene (heavy)",       0.040));
84                 SURFACE_MATERIAL.add(new Material.Surface("Silk",                                       0.060));
85                 SURFACE_MATERIAL.add(new Material.Surface("Paper (office)",                     0.080));
86                 SURFACE_MATERIAL.add(new Material.Surface("Cellophane",                         0.018));
87                 SURFACE_MATERIAL.add(new Material.Surface("Cr\u00eape paper",           0.025));
88                 
89                 LINE_MATERIAL.add(new Material.Line("Thread (heavy-duty)",                              0.0003));
90                 LINE_MATERIAL.add(new Material.Line("Elastic cord (round 2mm, 1/16 in)",0.0018));
91                 LINE_MATERIAL.add(new Material.Line("Elastic cord (flat  6mm, 1/4 in)", 0.0043));
92                 LINE_MATERIAL.add(new Material.Line("Elastic cord (flat 12mm, 1/2 in)", 0.008));
93                 LINE_MATERIAL.add(new Material.Line("Elastic cord (flat 19mm, 3/4 in)", 0.0012));
94                 LINE_MATERIAL.add(new Material.Line("Elastic cord (flat 25mm, 1 in)",   0.0016));
95                 LINE_MATERIAL.add(new Material.Line("Braided nylon (2 mm, 1/16 in)",    0.001));
96                 LINE_MATERIAL.add(new Material.Line("Braided nylon (3 mm, 1/8 in)",     0.0035));
97                 LINE_MATERIAL.add(new Material.Line("Tubular nylon (11 mm, 7/16 in)",   0.013));
98                 LINE_MATERIAL.add(new Material.Line("Tubular nylon (14 mm, 9/16 in)",   0.016));
99                 LINE_MATERIAL.add(new Material.Line("Tubular nylon (25 mm, 1 in)",              0.029));
100         }
101         
102         
103         /**
104          * Find a material from the database with the specified type and name.  Returns
105          * <code>null</code> if the specified material could not be found.
106          * 
107          * @param type  the material type.
108          * @param name  the material name in the database.
109          * @return              the material, or <code>null</code> if not found.
110          */
111         public static Material findMaterial(Material.Type type, String name) {
112                 Database<Material> db;
113                 switch (type) {
114                 case BULK:
115                         db = BULK_MATERIAL;
116                         break;
117                 case SURFACE:
118                         db = SURFACE_MATERIAL;
119                         break;
120                 case LINE:
121                         db = LINE_MATERIAL;
122                         break;
123                 default:
124                         throw new IllegalArgumentException("Illegal material type: "+type);
125                 }
126                 
127                 for (Material m: db) {
128                         if (m.getName().equalsIgnoreCase(name)) {
129                                 return m;
130                         }
131                 }
132                 return null;
133         }
134         
135         
136         /**
137          * Find a material from the database or return a new material if the specified
138          * material with the specified density is not found.
139          * 
140          * @param type          the material type.
141          * @param name          the material name.
142          * @param density       the density of the material.
143          * @return                      the material object from the database or a new material.
144          */
145         public static Material findMaterial(Material.Type type, String name, double density) {
146                 Database<Material> db;
147                 switch (type) {
148                 case BULK:
149                         db = BULK_MATERIAL;
150                         break;
151                 case SURFACE:
152                         db = SURFACE_MATERIAL;
153                         break;
154                 case LINE:
155                         db = LINE_MATERIAL;
156                         break;
157                 default:
158                         throw new IllegalArgumentException("Illegal material type: "+type);
159                 }
160
161                 for (Material m: db) {
162                         if (m.getName().equalsIgnoreCase(name) && MathUtil.equals(m.getDensity(), density)) {
163                                 return m;
164                         }
165                 }
166                 return Material.newMaterial(type, name, density);
167         }       
168         
169         
170
171         /**
172          * Return all motor in the database matching a search criteria.  Any search criteria that
173          * is null or NaN is ignored.
174          * 
175          * @param type                  the motor type, or null.
176          * @param manufacturer  the manufacturer, or null.
177          * @param designation   the designation, or null.
178          * @param diameter              the diameter, or NaN.
179          * @param length                the length, or NaN.
180          * @return                              an array of all the matching motors.
181          */
182         public static Motor[] findMotors(Motor.Type type, String manufacturer, String designation, double diameter, double length) {
183                 ArrayList<Motor> results = new ArrayList<Motor>();
184                 
185                 for (Motor m: MOTOR) {
186                         boolean match = true;
187                         if (type != null  &&  type != m.getMotorType())
188                                 match = false;
189                         else if (manufacturer != null  &&  !manufacturer.equalsIgnoreCase(m.getManufacturer()))
190                                 match = false;
191                         else if (designation != null  &&  !designation.equalsIgnoreCase(m.getDesignation()))
192                                 match = false;
193                         else if (!Double.isNaN(diameter)  &&  (Math.abs(diameter - m.getDiameter()) > 0.0015))
194                                 match = false;
195                         else if (!Double.isNaN(length) && (Math.abs(length - m.getLength()) > 0.0015))
196                                 match = false;
197                         
198                         if (match)
199                                 results.add(m);
200                 }
201                 
202                 return results.toArray(new Motor[0]);
203         }
204
205 }