Fix cloning and equality of wind models

This commit is contained in:
SiboVG 2024-09-21 06:53:43 +01:00
parent 4a3b9efe76
commit db08a4ffc2
2 changed files with 32 additions and 9 deletions

View File

@ -6,6 +6,7 @@ import java.util.EventObject;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;
import info.openrocket.core.util.ChangeSource;
import info.openrocket.core.util.Coordinate;
@ -131,12 +132,21 @@ public class MultiLevelPinkNoiseWindModel implements WindModel {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MultiLevelPinkNoiseWindModel that = (MultiLevelPinkNoiseWindModel) o;
return levels.equals(that.levels);
// Compare the levels list
if (levels.size() != that.levels.size()) return false;
for (int i = 0; i < levels.size(); i++) {
if (!levels.get(i).equals(that.levels.get(i))) return false;
}
// If we implement any additional fields in the future, we should compare them here
return true;
}
@Override
public int hashCode() {
return levels.hashCode();
return Objects.hash(levels);
}
public static class LevelWindModel implements Cloneable, ChangeSource {
@ -203,11 +213,17 @@ public class MultiLevelPinkNoiseWindModel implements WindModel {
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
LevelWindModel that = (LevelWindModel) obj;
return Double.compare(that.altitude, altitude) == 0 && model.equals(that.model);
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LevelWindModel that = (LevelWindModel) o;
return Double.compare(that.altitude, altitude) == 0 &&
model.equals(that.model);
}
@Override
public int hashCode() {
return Objects.hash(altitude, model);
}
@Override

View File

@ -81,8 +81,8 @@ public class SimulationOptions implements ChangeSource, Cloneable, SimulationOpt
private List<EventListener> listeners = new ArrayList<>();
private WindModelType windModelType = WindModelType.AVERAGE;
private final PinkNoiseWindModel averageWindModel;
private final MultiLevelPinkNoiseWindModel multiLevelPinkNoiseWindModel;
private PinkNoiseWindModel averageWindModel;
private MultiLevelPinkNoiseWindModel multiLevelPinkNoiseWindModel;
public SimulationOptions() {
averageWindModel = new PinkNoiseWindModel(randomSeed);
@ -338,7 +338,14 @@ public class SimulationOptions implements ChangeSource, Cloneable, SimulationOpt
public SimulationOptions clone() {
try {
SimulationOptions copy = (SimulationOptions) super.clone();
// Deep clone the wind models
copy.averageWindModel = this.averageWindModel.clone();
copy.multiLevelPinkNoiseWindModel = this.multiLevelPinkNoiseWindModel.clone();
// Create a new list for listeners
copy.listeners = new ArrayList<>();
return copy;
} catch (CloneNotSupportedException e) {
throw new BugException(e);