commit
201de6a6de
@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -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 <sampo.niskanen@iki.fi>
|
||||
*/
|
||||
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<? 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) {
|
||||
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user