adds documentation and refactoring for ThrustCurveMotorSet file

This commit is contained in:
Luiz Victor Linhares Rocha 2016-10-24 15:29:32 -02:00
parent 3fc4c3799c
commit 37402f9b95

View File

@ -47,32 +47,126 @@ public class ThrustCurveMotorSet implements Comparable<ThrustCurveMotorSet> {
private String caseInfo = null; private String caseInfo = null;
private boolean available = true; private boolean available = true;
/**
* adds a motor into the set,
* uses digest and designation to determinate if a motor is present or not
* @param motor the motor to be added
*/
public void addMotor(ThrustCurveMotor motor) { public void addMotor(ThrustCurveMotor motor) {
// Check for first insertion checkFirstInsertion(motor);
if (motors.isEmpty()) { verifyMotor(motor);
manufacturer = motor.getManufacturer(); updateType(motor);
designation = motor.getDesignation(); checkChangeSimplifiedDesignation(motor);
simplifiedDesignation = simplifyDesignation(designation); addStandardDelays(motor);
diameter = motor.getDiameter(); if(!checkMotorOverwrite(motor)){
length = motor.getLength(); motors.add(motor);
totalImpulse = Math.round((motor.getTotalImpulseEstimate())); digestMap.put(motor, motor.getDigest());
Collections.sort(motors, comparator);
}
}
/**
* checks whether a motor is present, overwriting it if
* @param motor the motor to be checked
* @return if there was an overwrite or not, returns true if all is equals
*/
private boolean checkMotorOverwrite(ThrustCurveMotor motor) {
final String digest = motor.getDigest();
for (int index = 0; index < motors.size(); index++) {
Motor m = motors.get(index);
if (isMotorPresent(motor, digest, m)) {
// Match found, check which one to keep (or both) based on comment
String newCmt = getFormattedDescription(motor);
String oldCmt = getFormattedDescription(m);
if (isNewDescriptionIrrelevant(newCmt, oldCmt)) {
return true;
} else if (oldCmt.length() == 0) {
motors.set(index, motor);
digestMap.put(motor, digest);
return true;
}
// else continue search and add both
}
}
return false;
}
/**
* checks if a motor is in the maps
* @param motor the motor to be checked
* @param digest the digest of the motor
* @param m the current motor being checked with
* @return wheter the motor is or no
*/
private boolean isMotorPresent(ThrustCurveMotor motor, final String digest, Motor m) {
return digest.equals(digestMap.get(m)) &&
motor.getDesignation().equals(m.getDesignation());
}
/**
* get a description from the motor
* @param motor the motor
* @return the description of the motor
*/
private String getFormattedDescription(Motor motor) {
return motor.getDescription().replaceAll("\\s+", " ").trim();
}
/**
* checks if the new commit message is empty or equals to the old commit
* @param newCmt the new commit message
* @param oldCmt the old commit message
* @return whether the new commit is empty or equals to the old commit
*/
private boolean isNewDescriptionIrrelevant(String newCmt, String oldCmt) {
return newCmt.length() == 0 || newCmt.equals(oldCmt);
}
/**
* adds the standard delay if aplicable
* @param motor the motor to be considered
*/
private void addStandardDelays(ThrustCurveMotor motor) {
// Add the standard delays
for (double d : motor.getStandardDelays()) {
d = Math.rint(d);
if (!delays.contains(d)) {
delays.add(d);
}
}
Collections.sort(delays);
}
/**
* checks if simplified designation should be changed with the given motor
* @param motor the motor to be checked with
*/
private void checkChangeSimplifiedDesignation(ThrustCurveMotor motor) {
// Change the simplified designation if necessary
if (!designation.equalsIgnoreCase(motor.getDesignation().trim())) {
designation = simplifiedDesignation;
}
if (caseInfo == null) {
caseInfo = motor.getCaseInfo(); caseInfo = motor.getCaseInfo();
available = motor.isAvailable(); }
} }
// Verify that the motor can be added
if (!matches(motor)) {
throw new IllegalArgumentException("Motor does not match the set:" +
" manufacturer=" + manufacturer +
" designation=" + designation +
" diameter=" + diameter +
" length=" + length +
" set_size=" + motors.size() +
" motor=" + motor);
}
/**
* checks if the cached type should be changed with the given motor
* if it's hybrid, delays will be added
* @param motor the motor to be checked with
*/
private void updateType(ThrustCurveMotor motor) {
// Update the type if now known // Update the type if now known
if (type == Motor.Type.UNKNOWN) { if (type == Motor.Type.UNKNOWN) {
type = motor.getMotorType(); type = motor.getMotorType();
@ -83,60 +177,50 @@ public class ThrustCurveMotorSet implements Comparable<ThrustCurveMotorSet> {
} }
} }
} }
// Change the simplified designation if necessary
if (!designation.equalsIgnoreCase(motor.getDesignation().trim())) {
designation = simplifiedDesignation;
} }
if (caseInfo == null) {
/**
* verifies if a motor is valid for this set
* @param motor the motor to be checked
*/
private void verifyMotor(ThrustCurveMotor motor) {
if (!matches(motor)) {
throw new IllegalArgumentException("Motor does not match the set:" +
" manufacturer=" + manufacturer +
" designation=" + designation +
" diameter=" + diameter +
" length=" + length +
" set_size=" + motors.size() +
" motor=" + motor);
}
}
/**
* checks if the given motor is the first one
* the ifrst motor inserted is what will difine the rest of the motors in the set
* @param motor the motor to be checked
*/
private void checkFirstInsertion(ThrustCurveMotor motor) {
if (motors.isEmpty()) {
manufacturer = motor.getManufacturer();
designation = motor.getDesignation();
simplifiedDesignation = simplifyDesignation(designation);
diameter = motor.getDiameter();
length = motor.getLength();
totalImpulse = Math.round((motor.getTotalImpulseEstimate()));
caseInfo = motor.getCaseInfo(); caseInfo = motor.getCaseInfo();
} available = motor.isAvailable();
// Add the standard delays
for (double d : motor.getStandardDelays()) {
d = Math.rint(d);
if (!delays.contains(d)) {
delays.add(d);
}
}
Collections.sort(delays);
// Check whether to add as new motor or overwrite existing
final String digest = motor.getDigest();
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 oldCmt = m.getDescription().replaceAll("\\s+", " ").trim();
if (newCmt.length() == 0 || newCmt.equals(oldCmt)) {
// Do not replace and do not add
return;
} else if (oldCmt.length() == 0) {
// Replace existing motor
motors.set(index, motor);
digestMap.put(motor, digest);
return;
}
// else continue search and add both
} }
} }
// Motor not present, add it /**
motors.add(motor); * Checks if a motor can be added with the set
digestMap.put(motor, digest); * A set contains motors of same manufacturer, diameter, length and type
Collections.sort(motors, comparator); * @param m the motor to be checked with
* @return if the motor passed the test or not
} */
public boolean matches(ThrustCurveMotor m) { public boolean matches(ThrustCurveMotor m) {
if (motors.isEmpty()) if (motors.isEmpty())
return true; return true;
@ -164,12 +248,19 @@ public class ThrustCurveMotorSet implements Comparable<ThrustCurveMotorSet> {
return true; return true;
} }
/**
* returns a new list with the stored motors
* @return list
*/
public List<ThrustCurveMotor> getMotors() { public List<ThrustCurveMotor> getMotors() {
return motors.clone(); return motors.clone();
} }
/**
*
* @return number of motor in the set
*/
public int getMotorCount() { public int getMotorCount() {
return motors.size(); return motors.size();
} }
@ -239,12 +330,18 @@ public class ThrustCurveMotorSet implements Comparable<ThrustCurveMotorSet> {
return totalImpulse; return totalImpulse;
} }
/**
* returns the case info of the motor
* @return the motor's case information
*/
public String getCaseInfo() { public String getCaseInfo() {
return caseInfo; return caseInfo;
} }
/**
* checks if the motor is available for other calculations
* @return if the motor is available
*/
public boolean isAvailable() { public boolean isAvailable() {
return available; return available;
} }