package net.sf.openrocket.database;
import java.text.Collator;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.IdentityHashMap;
import net.sf.openrocket.motor.DesignationComparator;
import net.sf.openrocket.motor.Manufacturer;
-import net.sf.openrocket.motor.MotorDigest;
import net.sf.openrocket.motor.Motor;
-import net.sf.openrocket.motor.ThrustCurveMotor;
import net.sf.openrocket.motor.Motor.Type;
+import net.sf.openrocket.motor.MotorDigest;
+import net.sf.openrocket.motor.ThrustCurveMotor;
+import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.MathUtil;
public class ThrustCurveMotorSet implements Comparable<ThrustCurveMotorSet> {
-
+
// Comparators:
private static final Collator COLLATOR = Collator.getInstance(Locale.US);
static {
}
private static final DesignationComparator DESIGNATION_COMPARATOR = new DesignationComparator();
private static final ThrustCurveMotorComparator comparator = new ThrustCurveMotorComparator();
-
-
+
+
private final ArrayList<ThrustCurveMotor> motors = new ArrayList<ThrustCurveMotor>();
private final Map<ThrustCurveMotor, String> digestMap =
- new IdentityHashMap<ThrustCurveMotor, String>();
+ new IdentityHashMap<ThrustCurveMotor, String>();
private final List<Double> delays = new ArrayList<Double>();
private Motor.Type type = Motor.Type.UNKNOWN;
-
+
public void addMotor(ThrustCurveMotor motor) {
// Check for first insertion
// Verify that the motor can be added
if (!matches(motor)) {
throw new IllegalArgumentException("Motor does not match the set:" +
- " manufacturer="+manufacturer +
- " designation="+designation +
+ " manufacturer=" + manufacturer +
+ " designation=" + designation +
" diameter=" + diameter +
" length=" + length +
" set_size=" + motors.size() +
// Update the type if now known
if (type == Motor.Type.UNKNOWN) {
type = motor.getMotorType();
+ // Add "Plugged" option if hybrid
+ if (type == Motor.Type.HYBRID) {
+ if (!delays.contains(Motor.PLUGGED)) {
+ delays.add(Motor.PLUGGED);
+ }
+ }
}
// Change the simplified designation if necessary
}
// Add the standard delays
- for (double d: motor.getStandardDelays()) {
+ for (double d : motor.getStandardDelays()) {
d = Math.rint(d);
- delays.add(d);
+ if (!delays.contains(d)) {
+ delays.add(d);
+ }
}
Collections.sort(delays);
-
+
// Check whether to add as new motor or overwrite existing
final String digest = MotorDigest.digestMotor(motor);
for (int index = 0; index < motors.size(); index++) {
Motor m = motors.get(index);
-
+
if (digest.equals(digestMap.get(m)) &&
motor.getDesignation().equals(m.getDesignation())) {
-
+
// Match found, check which one to keep (or both) based on comment
- String newCmt = motor.getDescription().replaceAll("\\s+"," ").trim();
+ String newCmt = motor.getDescription().replaceAll("\\s+", " ").trim();
String oldCmt = m.getDescription().replaceAll("\\s+", " ").trim();
if (newCmt.length() == 0 || newCmt.equals(oldCmt)) {
public boolean matches(ThrustCurveMotor m) {
if (motors.isEmpty())
return true;
-
+
if (manufacturer != m.getManufacturer())
return false;
if (!MathUtil.equals(length, m.getLength()))
return false;
- if ((type != Type.UNKNOWN) && (m.getMotorType()!= Type.UNKNOWN) &&
+ if ((type != Type.UNKNOWN) && (m.getMotorType() != Type.UNKNOWN) &&
(type != m.getMotorType())) {
return false;
}
return true;
}
-
- @SuppressWarnings("unchecked")
+
public List<ThrustCurveMotor> getMotors() {
- return (List<ThrustCurveMotor>) motors.clone();
+ return motors.clone();
}
+ public int getMotorCount() {
+ return motors.size();
+ }
+
/**
* Return the standard delays applicable to this motor type. This is a union of
public List<Double> getDelays() {
return Collections.unmodifiableList(delays);
}
-
-
+
+
/**
* Return the manufacturer of this motor type.
* @return the manufacturer
public Manufacturer getManufacturer() {
return manufacturer;
}
-
-
+
+
/**
* Return the designation of this motor type. This is either the exact or simplified
* designation, depending on what motors have been added.
public String getDesignation() {
return designation;
}
-
-
+
+
/**
* Return the diameter of this motor type.
* @return the diameter
public double getDiameter() {
return diameter;
}
-
-
+
+
/**
* Return the length of this motor type.
* @return the length
public double getLength() {
return length;
}
-
-
+
+
/**
* Return the type of this motor type. If any of the added motors has a type different
* from UNKNOWN, then that type will be returned.
public Motor.Type getType() {
return type;
}
+
+
-
+ @Override
+ public String toString() {
+ return "ThrustCurveMotorSet[" + manufacturer + " " + designation +
+ ", type=" + type + ", count=" + motors.size() + "]";
+ }
+
+
private static final Pattern SIMPLIFY_PATTERN = Pattern.compile("^[0-9]*[ -]*([A-Z][0-9]+).*");
+
/**
* Simplify a motor designation, if possible. This attempts to reduce the designation
* into a simple letter + number notation for the impulse class and average thrust.
* Comparator for deciding in which order to display matching motors.
*/
private static class ThrustCurveMotorComparator implements Comparator<ThrustCurveMotor> {
-
+
@Override
public int compare(ThrustCurveMotor o1, ThrustCurveMotor o2) {
// 1. Designation
}
}
-
-
+
+
@Override
public int compareTo(ThrustCurveMotorSet other) {
-
+
int value;
// 1. Manufacturer
- value = COLLATOR.compare(this.manufacturer.getDisplayName(),
+ value = COLLATOR.compare(this.manufacturer.getDisplayName(),
other.manufacturer.getDisplayName());
if (value != 0)
return value;
return value;
// 3. Diameter
- value = (int)((this.diameter - other.diameter)*1000000);
+ value = (int) ((this.diameter - other.diameter) * 1000000);
if (value != 0)
return value;
-
+
// 4. Length
- value = (int)((this.length - other.length)*1000000);
+ value = (int) ((this.length - other.length) * 1000000);
return value;
}