create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / DatabaseMotorFinder.java
1 package net.sf.openrocket.file;
2
3 import java.util.List;
4
5 import net.sf.openrocket.aerodynamics.Warning;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.motor.Motor;
8 import net.sf.openrocket.motor.Motor.Type;
9 import net.sf.openrocket.startup.Application;
10
11 /**
12  * A MotorFinder implementation that searches the thrust curve motor database
13  * for a motor.
14  * 
15  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
16  */
17 public class DatabaseMotorFinder implements MotorFinder {
18         
19         /**
20          * Do something when a missing motor is found.
21          * 
22          * This implementation adds a Warning.MissingMotor to the warning set and returns null.
23          * 
24          * Override this function to change the behavior.
25          * 
26          * @return The Motor which will be put in the Rocket.
27          */
28         protected Motor handleMissingMotor(Type type, String manufacturer, String designation, double diameter, double length, String digest, WarningSet warnings) {
29                 Warning.MissingMotor mmw = new Warning.MissingMotor();
30                 mmw.setDesignation(designation);
31                 mmw.setDigest(digest);
32                 mmw.setDiameter(diameter);
33                 mmw.setLength(length);
34                 mmw.setManufacturer(manufacturer);
35                 mmw.setType(type);
36                 warnings.add(mmw);
37                 return null;
38         }
39         
40         @Override
41         public Motor findMotor(Type type, String manufacturer, String designation, double diameter, double length, String digest, WarningSet warnings) {
42                 
43                 if (designation == null) {
44                         warnings.add(Warning.fromString("No motor specified, ignoring."));
45                         return null;
46                 }
47                 
48                 List<? extends Motor> motors = Application.getMotorSetDatabase().findMotors(type, manufacturer, designation, diameter, length);
49                 
50                 // No motors
51                 if (motors.size() == 0) {
52                         return handleMissingMotor(type, manufacturer, designation, diameter, length, digest, warnings);
53                 }               
54                 
55                 // One motor
56                 if (motors.size() == 1) {
57                         Motor m = motors.get(0);
58                         if (digest != null && !digest.equals(m.getDigest())) {
59                                 String str = "Motor with designation '" + designation + "'";
60                                 if (manufacturer != null)
61                                         str += " for manufacturer '" + manufacturer + "'";
62                                 str += " has differing thrust curve than the original.";
63                                 warnings.add(str);
64                         }
65                         return m;
66                 }
67                 
68                 // Multiple motors, check digest for which one to use
69                 if (digest != null) {
70                         
71                         // Check for motor with correct digest
72                         for (Motor m : motors) {
73                                 if (digest.equals(m.getDigest())) {
74                                         return m;
75                                 }
76                         }
77                         String str = "Motor with designation '" + designation + "'";
78                         if (manufacturer != null)
79                                 str += " for manufacturer '" + manufacturer + "'";
80                         str += " has differing thrust curve than the original.";
81                         warnings.add(str);
82                         
83                 } else {
84                         
85                         String str = "Multiple motors with designation '" + designation + "'";
86                         if (manufacturer != null)
87                                 str += " for manufacturer '" + manufacturer + "'";
88                         str += " found, one chosen arbitrarily.";
89                         warnings.add(str);
90                         
91                 }
92                 return motors.get(0);
93         }
94         
95 }