Skip to content

Enhancing VoltageLevel with equals method. #1064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -79,6 +76,21 @@ public boolean covers(String id, ComparableQuantity<ElectricPotential> 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{"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -47,6 +48,20 @@ public ComparableQuantity<ElectricPotential> 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 + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}