Merge old findMotor() and findMotors() methods in ThrustCurveMotorSetDatabase.java
Turns out the motor digest hash function can have collisions. Since the digest is stored in the .ork file we don't want to rewrite it as that will cause *lots* of spurious "thrust curves don't match" warnings, and I really don't want to get into looking up by some sort of new digest if it exists else old digest. Since some motor description fields are also stored in the .ork file, I'm effectively using these as a key to resolve collisions. Now that I need to pass them in to the findMotor() method, I'm merging it with findMotors() (which searches based on motor descriptions) New combined method returns all motors that match digest and description; If there are none returns all motors that match digest; If there are none returns all motors that match description (may be empty) Passing a digest and no other description emulates the behavior of the old findMotor() (but it may return more than one motor). Passing description elements and no digest duplicates the behavior of the old findMotors()
This commit is contained in:
parent
db64a85a3a
commit
495ef34970
@ -17,10 +17,7 @@ public interface MotorDatabase {
|
||||
* @param length the length, or NaN.
|
||||
* @return a list of all the matching motors.
|
||||
*/
|
||||
public List<? extends Motor> findMotors(Motor.Type type,
|
||||
public List<? extends Motor> findMotors(String digest, Motor.Type type,
|
||||
String manufacturer, String designation, double diameter,
|
||||
double length);
|
||||
|
||||
public Motor findMotor(String digest);
|
||||
|
||||
}
|
||||
|
@ -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 <sampo.niskanen@iki.fi>
|
||||
*/
|
||||
public class ThrustCurveMotorSetDatabase implements MotorDatabase {
|
||||
private static final Logger log = LoggerFactory.getLogger(ThrustCurveMotorSetDatabase.class);
|
||||
|
||||
private final List<ThrustCurveMotorSet> motorSets = new ArrayList<ThrustCurveMotorSet>();
|
||||
|
||||
@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<ThrustCurveMotor> findMotors(Motor.Type type, String manufacturer, String designation,
|
||||
public List<ThrustCurveMotor> findMotors(String digest, Motor.Type type, String manufacturer, String designation,
|
||||
double diameter, double length) {
|
||||
ArrayList<ThrustCurveMotor> results = new ArrayList<ThrustCurveMotor>();
|
||||
|
||||
ArrayList<ThrustCurveMotor> fullMatches = new ArrayList<ThrustCurveMotor>();
|
||||
ArrayList<ThrustCurveMotor> digestMatches = new ArrayList<ThrustCurveMotor>();
|
||||
ArrayList<ThrustCurveMotor> descriptionMatches = new ArrayList<ThrustCurveMotor>();
|
||||
|
||||
// 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -54,14 +54,7 @@ public class DatabaseMotorFinder implements MotorFinder {
|
||||
|
||||
List<? extends Motor> motors;
|
||||
|
||||
{
|
||||
Motor m = Application.getMotorSetDatabase().findMotor(digest);
|
||||
if (m != null) {
|
||||
motors = Collections.<Motor> 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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user