Refactor FlightDataType and FlightDataBranch to interfaces

This commit is contained in:
SiboVG 2024-08-14 01:00:10 +02:00
parent bbff83e015
commit 410a30ffdc
4 changed files with 92 additions and 46 deletions

View File

@ -0,0 +1,71 @@
package info.openrocket.core.simulation;
import info.openrocket.core.util.Monitorable;
import java.util.List;
/**
* A branch of data / collection of data points for a specific type of data.
* @param <T> the type of data in this branch
*/
public interface DataBranch<T extends DataType> extends Monitorable {
/**
* Return an array of values for the specified variable type.
*
* @param type the variable type.
* @return a list of the variable values, or <code>null</code> if
* the variable type hasn't been added to this branch.
*/
List<Double> get(T type);
/**
* Return the value of the specified type at the specified index.
* @param type the variable type
* @param index the data index of the value
* @return the value at the specified index
*/
Double getByIndex(T type, int index);
/**
* Return the last value of the specified type in the branch, or NaN if the type is
* unavailable.
*
* @param type the parameter type.
* @return the last value in this branch, or NaN.
*/
double getLast(T type);
/**
* Return the minimum value of the specified type in the branch, or NaN if the type
* is unavailable.
*
* @param type the parameter type.
* @return the minimum value in this branch, or NaN.
*/
double getMinimum(T type);
/**
* Return the maximum value of the specified type in the branch, or NaN if the type
* is unavailable.
*
* @param type the parameter type.
* @return the maximum value in this branch, or NaN.
*/
double getMaximum(T type);
/**
* Return the number of data points in this branch.
*/
int getLength();
/**
* Return the variable types included in this branch. The types are sorted in their
* natural order.
*/
T[] getTypes();
/**
* Return the branch name.
*/
String getName();
}

View File

@ -0,0 +1,9 @@
package info.openrocket.core.simulation;
import info.openrocket.core.util.UnitValue;
/**
* A type of data that can be stored in a {@link DataBranch}.
*/
public interface DataType extends UnitValue {
}

View File

@ -29,7 +29,7 @@ import info.openrocket.core.util.Mutable;
*
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/
public class FlightDataBranch implements Monitorable {
public class FlightDataBranch implements DataBranch<FlightDataType> {
/** The name of this flight data branch. */
private final String name;
@ -228,26 +228,19 @@ public class FlightDataBranch implements Monitorable {
}
}
/**
* Return the branch name.
*/
@Override
public String getName() {
return name;
}
/**
* Return the variable types included in this branch. The types are sorted in their
* natural order.
*/
@Override
public FlightDataType[] getTypes() {
FlightDataType[] array = values.keySet().toArray(new FlightDataType[0]);
Arrays.sort(array);
return array;
}
/**
* Return the number of data points in this branch.
*/
@Override
public int getLength() {
for (ArrayList<Double> doubles : values.values()) {
return doubles.size();
@ -255,13 +248,7 @@ public class FlightDataBranch implements Monitorable {
return 0;
}
/**
* Return an array of values for the specified variable type.
*
* @param type the variable type.
* @return a list of the variable values, or <code>null</code> if
* the variable type hasn't been added to this branch.
*/
@Override
public List<Double> get(FlightDataType type) {
ArrayList<Double> list = values.get(type);
if (list == null)
@ -269,12 +256,7 @@ public class FlightDataBranch implements Monitorable {
return list.clone();
}
/**
* Return the value of the specified type at the specified index.
* @param type the variable type
* @param index the data index of the value
* @return the value at the specified index
*/
@Override
public Double getByIndex(FlightDataType type, int index) {
if (index < 0 || index >= getLength()) {
throw new IllegalArgumentException("Index out of bounds");
@ -286,13 +268,7 @@ public class FlightDataBranch implements Monitorable {
return list.get(index);
}
/**
* Return the last value of the specified type in the branch, or NaN if the type is
* unavailable.
*
* @param type the parameter type.
* @return the last value in this branch, or NaN.
*/
@Override
public double getLast(FlightDataType type) {
ArrayList<Double> list = values.get(type);
if (list == null || list.isEmpty())
@ -300,13 +276,7 @@ public class FlightDataBranch implements Monitorable {
return list.get(list.size() - 1);
}
/**
* Return the minimum value of the specified type in the branch, or NaN if the type
* is unavailable.
*
* @param type the parameter type.
* @return the minimum value in this branch, or NaN.
*/
@Override
public double getMinimum(FlightDataType type) {
Double v = minValues.get(type);
if (v == null)
@ -314,13 +284,7 @@ public class FlightDataBranch implements Monitorable {
return v;
}
/**
* Return the maximum value of the specified type in the branch, or NaN if the type
* is unavailable.
*
* @param type the parameter type.
* @return the maximum value in this branch, or NaN.
*/
@Override
public double getMaximum(FlightDataType type) {
Double v = maxValues.get(type);
if (v == null)

View File

@ -34,7 +34,7 @@ import info.openrocket.core.util.StringUtils;
*
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
*/
public class FlightDataType implements Comparable<FlightDataType>, Groupable<FlightDataTypeGroup>, UnitValue {
public class FlightDataType implements Comparable<FlightDataType>, Groupable<FlightDataTypeGroup>, DataType {
private static final Translator trans = Application.getTranslator();
private static final Logger log = LoggerFactory.getLogger(FlightDataType.class);
@ -490,10 +490,12 @@ public class FlightDataType implements Comparable<FlightDataType>, Groupable<Fli
return symbol;
}
@Override
public UnitGroup getUnitGroup() {
return units;
}
@Override
public FlightDataTypeGroup getGroup() {
return group;
}