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> * @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. */ /** The name of this flight data branch. */
private final String name; private final String name;
@ -228,26 +228,19 @@ public class FlightDataBranch implements Monitorable {
} }
} }
/** @Override
* Return the branch name.
*/
public String getName() { public String getName() {
return name; return name;
} }
/** @Override
* Return the variable types included in this branch. The types are sorted in their
* natural order.
*/
public FlightDataType[] getTypes() { public FlightDataType[] getTypes() {
FlightDataType[] array = values.keySet().toArray(new FlightDataType[0]); FlightDataType[] array = values.keySet().toArray(new FlightDataType[0]);
Arrays.sort(array); Arrays.sort(array);
return array; return array;
} }
/** @Override
* Return the number of data points in this branch.
*/
public int getLength() { public int getLength() {
for (ArrayList<Double> doubles : values.values()) { for (ArrayList<Double> doubles : values.values()) {
return doubles.size(); return doubles.size();
@ -255,13 +248,7 @@ public class FlightDataBranch implements Monitorable {
return 0; return 0;
} }
/** @Override
* 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.
*/
public List<Double> get(FlightDataType type) { public List<Double> get(FlightDataType type) {
ArrayList<Double> list = values.get(type); ArrayList<Double> list = values.get(type);
if (list == null) if (list == null)
@ -269,12 +256,7 @@ public class FlightDataBranch implements Monitorable {
return list.clone(); return list.clone();
} }
/** @Override
* 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
*/
public Double getByIndex(FlightDataType type, int index) { public Double getByIndex(FlightDataType type, int index) {
if (index < 0 || index >= getLength()) { if (index < 0 || index >= getLength()) {
throw new IllegalArgumentException("Index out of bounds"); throw new IllegalArgumentException("Index out of bounds");
@ -286,13 +268,7 @@ public class FlightDataBranch implements Monitorable {
return list.get(index); return list.get(index);
} }
/** @Override
* 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.
*/
public double getLast(FlightDataType type) { public double getLast(FlightDataType type) {
ArrayList<Double> list = values.get(type); ArrayList<Double> list = values.get(type);
if (list == null || list.isEmpty()) if (list == null || list.isEmpty())
@ -300,13 +276,7 @@ public class FlightDataBranch implements Monitorable {
return list.get(list.size() - 1); return list.get(list.size() - 1);
} }
/** @Override
* 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.
*/
public double getMinimum(FlightDataType type) { public double getMinimum(FlightDataType type) {
Double v = minValues.get(type); Double v = minValues.get(type);
if (v == null) if (v == null)
@ -314,13 +284,7 @@ public class FlightDataBranch implements Monitorable {
return v; return v;
} }
/** @Override
* 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.
*/
public double getMaximum(FlightDataType type) { public double getMaximum(FlightDataType type) {
Double v = maxValues.get(type); Double v = maxValues.get(type);
if (v == null) if (v == null)

View File

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