motor loading refactoring
[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         @Override
20         public Motor findMotor(Type type, String manufacturer, String designation, double diameter, double length, String digest, WarningSet warnings) {
21                 
22                 if (designation == null) {
23                         warnings.add(Warning.fromString("No motor specified, ignoring."));
24                         return null;
25                 }
26                 
27                 List<? extends Motor> motors = Application.getMotorSetDatabase().findMotors(type, manufacturer, designation, diameter, length);
28                 
29                 // No motors
30                 if (motors.size() == 0) {
31                         Warning.MissingMotor mmw = new Warning.MissingMotor();
32                         mmw.setDesignation(designation);
33                         mmw.setDigest(digest);
34                         mmw.setDiameter(diameter);
35                         mmw.setLength(length);
36                         mmw.setManufacturer(manufacturer);
37                         mmw.setType(type);
38                         warnings.add(mmw);
39                         return null;
40                 }
41                 
42                 // One motor
43                 if (motors.size() == 1) {
44                         Motor m = motors.get(0);
45                         if (digest != null && !digest.equals(m.getDigest())) {
46                                 String str = "Motor with designation '" + designation + "'";
47                                 if (manufacturer != null)
48                                         str += " for manufacturer '" + manufacturer + "'";
49                                 str += " has differing thrust curve than the original.";
50                                 warnings.add(str);
51                         }
52                         return m;
53                 }
54                 
55                 // Multiple motors, check digest for which one to use
56                 if (digest != null) {
57                         
58                         // Check for motor with correct digest
59                         for (Motor m : motors) {
60                                 if (digest.equals(m.getDigest())) {
61                                         return m;
62                                 }
63                         }
64                         String str = "Motor with designation '" + designation + "'";
65                         if (manufacturer != null)
66                                 str += " for manufacturer '" + manufacturer + "'";
67                         str += " has differing thrust curve than the original.";
68                         warnings.add(str);
69                         
70                 } else {
71                         
72                         String str = "Multiple motors with designation '" + designation + "'";
73                         if (manufacturer != null)
74                                 str += " for manufacturer '" + manufacturer + "'";
75                         str += " found, one chosen arbitrarily.";
76                         warnings.add(str);
77                         
78                 }
79                 return motors.get(0);
80         }
81         
82 }