diff --git a/core/src/net/sf/openrocket/database/motor/MotorDatabase.java b/core/src/net/sf/openrocket/database/motor/MotorDatabase.java index 231027104..9f7dca35e 100644 --- a/core/src/net/sf/openrocket/database/motor/MotorDatabase.java +++ b/core/src/net/sf/openrocket/database/motor/MotorDatabase.java @@ -17,10 +17,7 @@ public interface MotorDatabase { * @param length the length, or NaN. * @return a list of all the matching motors. */ - public List findMotors(Motor.Type type, + public List findMotors(String digest, Motor.Type type, String manufacturer, String designation, double diameter, double length); - - public Motor findMotor(String digest); - } diff --git a/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSetDatabase.java b/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSetDatabase.java index 5c3d4b7a4..d04aea975 100644 --- a/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSetDatabase.java +++ b/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSetDatabase.java @@ -4,6 +4,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.sf.openrocket.motor.Motor; import net.sf.openrocket.motor.ThrustCurveMotor; @@ -14,51 +17,59 @@ import net.sf.openrocket.motor.ThrustCurveMotor; * @author Sampo Niskanen */ public class ThrustCurveMotorSetDatabase implements MotorDatabase { + private static final Logger log = LoggerFactory.getLogger(ThrustCurveMotorSetDatabase.class); private final List motorSets = new ArrayList(); @Override - public ThrustCurveMotor findMotor(String digest) { - if (digest == null) { - return null; - } - for (ThrustCurveMotorSet set : motorSets) { - for (ThrustCurveMotor m : set.getMotors()) { - if (digest.equals(m.getDigest())) { - return m; - } - } - } - - return null; - - } - - @Override - public List findMotors(Motor.Type type, String manufacturer, String designation, + public List findMotors(String digest, Motor.Type type, String manufacturer, String designation, double diameter, double length) { - ArrayList results = new ArrayList(); - + ArrayList fullMatches = new ArrayList(); + ArrayList digestMatches = new ArrayList(); + ArrayList descriptionMatches = new ArrayList(); + + // Apply filters to see if we can find any motors that match the given criteria. We'll return + // the most restrictive nonempty list we find, or empty list if no matches at all for (ThrustCurveMotorSet set : motorSets) { for (ThrustCurveMotor m : set.getMotors()) { - boolean match = true; + boolean matchDescription = true; + boolean matchDigest = true; + + // unlike the description, digest must be present in search criteria to get a match + if (digest == null || digest != m.getDigest()) + matchDigest = false; + + // match description if (type != null && type != set.getType()) - match = false; + matchDescription = false; else if (manufacturer != null && !m.getManufacturer().matches(manufacturer)) - match = false; + matchDescription = false; else if (designation != null && !designation.equalsIgnoreCase(m.getDesignation())) - match = false; + matchDescription = false; else if (!Double.isNaN(diameter) && (Math.abs(diameter - m.getDiameter()) > 0.005)) - match = false; + matchDescription = false; else if (!Double.isNaN(length) && (Math.abs(length - m.getLength()) > 0.005)) - match = false; - - if (match) - results.add(m); + matchDescription = false; + + if (matchDigest) + digestMatches.add(m); + + if (matchDescription) + descriptionMatches.add(m); + + if (matchDigest && matchDescription) + fullMatches.add(m); } } - return results; + if (!fullMatches.isEmpty()) + return fullMatches; + + if (!digestMatches.isEmpty()) + return digestMatches; + + return descriptionMatches; + } diff --git a/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java b/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java index a92e9f8e1..36e83698d 100644 --- a/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java +++ b/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java @@ -3,6 +3,9 @@ package net.sf.openrocket.file; import java.util.Collections; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.sf.openrocket.aerodynamics.Warning; import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.motor.Motor; @@ -16,6 +19,7 @@ import net.sf.openrocket.startup.Application; * @author Sampo Niskanen */ public class DatabaseMotorFinder implements MotorFinder { + private static final Logger log = LoggerFactory.getLogger(DatabaseMotorFinder.class); /** * Do something when a missing motor is found. @@ -40,6 +44,8 @@ public class DatabaseMotorFinder implements MotorFinder { @Override public Motor findMotor(Type type, String manufacturer, String designation, double diameter, double length, String digest, WarningSet warnings) { + + log.debug("type " + type + ", manufacturer " + manufacturer + ", designation " + designation + ", diameter " + diameter + ", length " + length + ", digest " + digest + ", warnings " + warnings); if (designation == null) { warnings.add(Warning.fromString("No motor specified, ignoring.")); @@ -48,14 +54,7 @@ public class DatabaseMotorFinder implements MotorFinder { List motors; - { - Motor m = Application.getMotorSetDatabase().findMotor(digest); - if (m != null) { - motors = Collections. singletonList(m); - } else { - motors = Application.getMotorSetDatabase().findMotors(type, manufacturer, designation, diameter, length); - } - } + motors = Application.getMotorSetDatabase().findMotors(digest, type, manufacturer, designation, diameter, length); // No motors if (motors.size() == 0) { @@ -65,6 +64,9 @@ public class DatabaseMotorFinder implements MotorFinder { // One motor if (motors.size() == 1) { Motor m = motors.get(0); + + log.debug("motor is " + m.getDesignation()); + if (digest != null && !digest.equals(m.getDigest())) { String str = "Motor with designation '" + designation + "'"; if (manufacturer != null)