diff --git a/CHANGELOG.md b/CHANGELOG.md index b431ade12..bedb73618 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added test for invalid input data in `CsvRawGridSource` [#1021](https://github.com/ie3-institute/PowerSystemDataModel/issues/1021) - Added `CsvThermalGridSource` [#1009](https://github.com/ie3-institute/PowerSystemDataModel/issues/1009) - Enhance documentation for CSV timeseries [#825](https://github.com/ie3-institute/PowerSystemDataModel/issues/825) +- Enhancing `VoltageLevel` with `equals` method [#1063](https://github.com/ie3-institute/PowerSystemDataModel/issues/1063) ### Fixed - Fixed Couchbase integration tests that randomly failed [#755](https://github.com/ie3-institute/PowerSystemDataModel/issues/755) diff --git a/src/main/java/edu/ie3/datamodel/models/voltagelevels/CommonVoltageLevel.java b/src/main/java/edu/ie3/datamodel/models/voltagelevels/CommonVoltageLevel.java index d120a64ee..895dd38ae 100644 --- a/src/main/java/edu/ie3/datamodel/models/voltagelevels/CommonVoltageLevel.java +++ b/src/main/java/edu/ie3/datamodel/models/voltagelevels/CommonVoltageLevel.java @@ -7,10 +7,7 @@ import edu.ie3.datamodel.exceptions.VoltageLevelException; import edu.ie3.util.interval.RightOpenInterval; -import java.util.Collections; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; +import java.util.*; import javax.measure.quantity.ElectricPotential; import tech.units.indriya.ComparableQuantity; @@ -79,6 +76,21 @@ public boolean covers(String id, ComparableQuantity vRated) return idCovered; /* voltage covered is always true, otherwise the exception would have been thrown. */ } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + CommonVoltageLevel that = (CommonVoltageLevel) o; + return this.voltageRange.equals(that.voltageRange); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), synonymousIds, voltageRange); + } + @Override public String toString() { return "CommonVoltageLevel{" diff --git a/src/main/java/edu/ie3/datamodel/models/voltagelevels/VoltageLevel.java b/src/main/java/edu/ie3/datamodel/models/voltagelevels/VoltageLevel.java index 7865d4da8..8a94fc534 100644 --- a/src/main/java/edu/ie3/datamodel/models/voltagelevels/VoltageLevel.java +++ b/src/main/java/edu/ie3/datamodel/models/voltagelevels/VoltageLevel.java @@ -6,6 +6,7 @@ package edu.ie3.datamodel.models.voltagelevels; import java.io.Serializable; +import java.util.Objects; import javax.measure.quantity.ElectricPotential; import tech.units.indriya.ComparableQuantity; @@ -47,6 +48,20 @@ public ComparableQuantity getNominalVoltage() { return nominalVoltage; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + VoltageLevel that = (VoltageLevel) o; + return this.nominalVoltage.isEquivalentTo(that.nominalVoltage); + } + + @Override + public int hashCode() { + return Objects.hash(id, nominalVoltage); + } + @Override public String toString() { return "VoltageLevel{" + "id='" + id + '\'' + ", nominalVoltage=" + nominalVoltage + '}'; diff --git a/src/test/groovy/edu/ie3/datamodel/models/CommonVoltageLevelTest.groovy b/src/test/groovy/edu/ie3/datamodel/models/CommonVoltageLevelTest.groovy index 041f12627..3ea843864 100644 --- a/src/test/groovy/edu/ie3/datamodel/models/CommonVoltageLevelTest.groovy +++ b/src/test/groovy/edu/ie3/datamodel/models/CommonVoltageLevelTest.groovy @@ -10,6 +10,8 @@ import static tech.units.indriya.unit.Units.VOLT import edu.ie3.datamodel.exceptions.VoltageLevelException import edu.ie3.datamodel.models.voltagelevels.CommonVoltageLevel +import edu.ie3.datamodel.models.voltagelevels.GermanVoltageLevelUtils +import edu.ie3.datamodel.models.voltagelevels.VoltageLevel import edu.ie3.util.interval.RightOpenInterval import spock.lang.Shared import spock.lang.Specification @@ -57,4 +59,24 @@ class CommonVoltageLevelTest extends Specification { VoltageLevelException ex = thrown() ex.message == "The provided id \"HS\" and rated voltage \"500 V\" could possibly meet the voltage level \"Niederspannung\" (Interval [0.0 kV, 10 kV)), but are inconsistent." } + + def "A common voltage level should test the equality correctly"() { + given: + def mv = GermanVoltageLevelUtils.MV_10KV + + expect: + mv == new CommonVoltageLevel( + "MS", + Quantities.getQuantity(10d, KILOVOLT), + new HashSet<>(Arrays.asList("mv_10kV")), + new RightOpenInterval<>(Quantities.getQuantity(10d, KILOVOLT), Quantities.getQuantity(20d, KILOVOLT))) + + + mv != new CommonVoltageLevel("MS", + Quantities.getQuantity(10d, KILOVOLT), + new HashSet<>(Arrays.asList("mv")), + new RightOpenInterval<>(Quantities.getQuantity(10d, KILOVOLT), Quantities.getQuantity(19.9, KILOVOLT))) + + mv != new VoltageLevel("mv", Quantities.getQuantity(10d, KILOVOLT)) + } }