Skip to content

Commit

Permalink
Clean all deprecated methods and remame element to property
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
geofjamg committed Nov 8, 2024
1 parent bd56b88 commit 4d5ff02
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public void afterRemoval(String identifiable) {
// empty default implementation
}

@Override
public void onUpdate(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
// empty default implementation
}

@Override
public void onUpdate(Identifiable<?> identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
// empty default implementation
Expand All @@ -57,4 +52,34 @@ public void onExtensionBeforeRemoval(Extension<?> extension) {
public void onExtensionAfterRemoval(Identifiable<?> identifiable, String extensionName) {
// empty default implementation
}

@Override
public void onPropertyAdded(Identifiable<?> identifiable, String attribute, Object newValue) {
// empty default implementation
}

@Override
public void onPropertyReplaced(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
// empty default implementation
}

@Override
public void onPropertyRemoved(Identifiable<?> identifiable, String attribute, Object oldValue) {
// empty default implementation
}

@Override
public void onVariantCreated(String sourceVariantId, String targetVariantId) {
// empty default implementation
}

@Override
public void onVariantOverwritten(String sourceVariantId, String targetVariantId) {
// empty default implementation
}

@Override
public void onVariantRemoved(String variantId) {
// empty default implementation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public void afterRemoval(String id) {
events.add(new RemovalNetworkEvent(id, true));
}

@Override
public void onUpdate(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
events.add(new UpdateNetworkEvent(identifiable.getId(), attribute, null, oldValue, newValue));
}

@Override
public void onUpdate(Identifiable<?> identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
events.add(new UpdateNetworkEvent(identifiable.getId(), attribute, variantId, oldValue, newValue));
Expand All @@ -74,17 +69,17 @@ public void onExtensionUpdate(Extension<?> extension, String attribute, String v
}

@Override
public void onElementAdded(Identifiable<?> identifiable, String attribute, Object newValue) {
public void onPropertyAdded(Identifiable<?> identifiable, String attribute, Object newValue) {
events.add(new PropertyUpdateNetworkEvent(identifiable.getId(), attribute, PropertyUpdateNetworkEvent.PropertyUpdateType.ADDED, null, newValue));
}

@Override
public void onElementReplaced(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
public void onPropertyReplaced(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
events.add(new PropertyUpdateNetworkEvent(identifiable.getId(), attribute, PropertyUpdateNetworkEvent.PropertyUpdateType.REPLACED, oldValue, newValue));
}

@Override
public void onElementRemoved(Identifiable<?> identifiable, String attribute, Object oldValue) {
public void onPropertyRemoved(Identifiable<?> identifiable, String attribute, Object oldValue) {
events.add(new PropertyUpdateNetworkEvent(identifiable.getId(), attribute, PropertyUpdateNetworkEvent.PropertyUpdateType.REMOVED, oldValue, null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public interface NetworkListener {

void afterRemoval(String id);

void onUpdate(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue);

void onUpdate(Identifiable<?> identifiable, String attribute, String variantId, Object oldValue, Object newValue);

void onExtensionCreation(Extension<?> extension);
Expand All @@ -32,27 +30,15 @@ public interface NetworkListener {

void onExtensionUpdate(Extension<?> extendable, String attribute, String variantId, Object oldValue, Object newValue);

default void onElementAdded(Identifiable<?> identifiable, String attribute, Object newValue) {
// empty default implementation
}
void onPropertyAdded(Identifiable<?> identifiable, String attribute, Object newValue);

default void onElementReplaced(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
// empty default implementation
}
void onPropertyReplaced(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue);

default void onElementRemoved(Identifiable<?> identifiable, String attribute, Object oldValue) {
// empty default implementation
}
void onPropertyRemoved(Identifiable<?> identifiable, String attribute, Object oldValue);

default void onVariantCreated(String sourceVariantId, String targetVariantId) {
// empty default implementation
}
void onVariantCreated(String sourceVariantId, String targetVariantId);

default void onVariantOverwritten(String sourceVariantId, String targetVariantId) {
// empty default implementation
}
void onVariantOverwritten(String sourceVariantId, String targetVariantId);

default void onVariantRemoved(String variantId) {
// empty default implementation
}
void onVariantRemoved(String variantId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ public String getProperty(String key, String defaultValue) {
public String setProperty(String key, String value) {
String oldValue = (String) properties.put(key, value);
if (Objects.isNull(oldValue)) {
getNetwork().getListeners().notifyElementAdded(this, () -> getPropertyStringForNotification(key), value);
getNetwork().getListeners().notifyPropertyAdded(this, () -> getPropertyStringForNotification(key), value);
} else {
getNetwork().getListeners().notifyElementReplaced(this, () -> getPropertyStringForNotification(key), oldValue, value);
getNetwork().getListeners().notifyPropertyReplaced(this, () -> getPropertyStringForNotification(key), oldValue, value);
}
return oldValue;
}
Expand All @@ -210,7 +210,7 @@ public String setProperty(String key, String value) {
public boolean removeProperty(String key) {
Object oldValue = properties.remove(key);
if (oldValue != null) {
getNetwork().getListeners().notifyElementRemoved(this, () -> getPropertyStringForNotification(key), oldValue);
getNetwork().getListeners().notifyPropertyRemoved(this, () -> getPropertyStringForNotification(key), oldValue);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,13 @@ void remove(NetworkListener listener) {

void notifyUpdate(Identifiable<?> identifiable, Supplier<String> attribute, Object oldValue, Object newValue) {
if (!listeners.isEmpty() && !Objects.equals(oldValue, newValue)) {
notifyUpdateListeners(identifiable, attribute.get(), oldValue, newValue);
notifyUpdateListeners(identifiable, attribute.get(), null, oldValue, newValue);
}
}

void notifyUpdate(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
if (!listeners.isEmpty() && !Objects.equals(oldValue, newValue)) {
notifyUpdateListeners(identifiable, attribute, oldValue, newValue);
}
}

private void notifyUpdateListeners(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
for (NetworkListener listener : listeners) {
try {
listener.onUpdate(identifiable, attribute, oldValue, newValue);
} catch (Exception t) {
LOGGER.error(t.toString(), t);
}
notifyUpdateListeners(identifiable, attribute, null, oldValue, newValue);
}
}

Expand Down Expand Up @@ -155,48 +145,48 @@ void notifyAfterRemoval(String id) {
}
}

void notifyElementAdded(Identifiable<?> identifiable, Supplier<String> attribute, Object newValue) {
void notifyPropertyAdded(Identifiable<?> identifiable, Supplier<String> attribute, Object newValue) {
if (!listeners.isEmpty()) {
notifyElementAdded(identifiable, attribute.get(), newValue);
notifyPropertyAdded(identifiable, attribute.get(), newValue);
}
}

void notifyElementAdded(Identifiable<?> identifiable, String attribute, Object newValue) {
void notifyPropertyAdded(Identifiable<?> identifiable, String attribute, Object newValue) {
for (NetworkListener listener : listeners) {
try {
listener.onElementAdded(identifiable, attribute, newValue);
listener.onPropertyAdded(identifiable, attribute, newValue);
} catch (Exception t) {
LOGGER.error(t.toString(), t);
}
}
}

void notifyElementReplaced(Identifiable<?> identifiable, Supplier<String> attribute, Object oldValue, Object newValue) {
void notifyPropertyReplaced(Identifiable<?> identifiable, Supplier<String> attribute, Object oldValue, Object newValue) {
if (!listeners.isEmpty() && !Objects.equals(oldValue, newValue)) {
notifyElementReplaced(identifiable, attribute.get(), oldValue, newValue);
notifyPropertyReplaced(identifiable, attribute.get(), oldValue, newValue);
}
}

void notifyElementReplaced(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
void notifyPropertyReplaced(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
for (NetworkListener listener : listeners) {
try {
listener.onElementReplaced(identifiable, attribute, oldValue, newValue);
listener.onPropertyReplaced(identifiable, attribute, oldValue, newValue);
} catch (Exception t) {
LOGGER.error(t.toString(), t);
}
}
}

void notifyElementRemoved(Identifiable<?> identifiable, Supplier<String> attribute, Object oldValue) {
void notifyPropertyRemoved(Identifiable<?> identifiable, Supplier<String> attribute, Object oldValue) {
if (!listeners.isEmpty()) {
notifyElementRemoved(identifiable, attribute.get(), oldValue);
notifyPropertyRemoved(identifiable, attribute.get(), oldValue);
}
}

void notifyElementRemoved(Identifiable<?> identifiable, String attribute, Object oldValue) {
void notifyPropertyRemoved(Identifiable<?> identifiable, String attribute, Object oldValue) {
for (NetworkListener listener : listeners) {
try {
listener.onElementRemoved(identifiable, attribute, oldValue);
listener.onPropertyRemoved(identifiable, attribute, oldValue);
} catch (Exception t) {
LOGGER.error(t.toString(), t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ public void edgeAdded(int e, SwitchImpl aSwitch) {
switches.put(aSwitch.getId(), e);
network.getListeners().notifyCreation(aSwitch);
} else {
network.getListeners().notifyElementAdded(voltageLevel, INTERNAL_CONNECTION, null);
network.getListeners().notifyPropertyAdded(voltageLevel, INTERNAL_CONNECTION, null);
}
invalidateCache();
}
Expand All @@ -552,7 +552,7 @@ public void edgeRemoved(int e, SwitchImpl aSwitch) {
switches.remove(switchId);
network.getListeners().notifyAfterRemoval(switchId);
} else {
network.getListeners().notifyElementRemoved(voltageLevel, INTERNAL_CONNECTION, null);
network.getListeners().notifyPropertyRemoved(voltageLevel, INTERNAL_CONNECTION, null);
}
}

Expand All @@ -569,7 +569,7 @@ public void allEdgesRemoved(Collection<SwitchImpl> aSwitches) {
if (ss != null) {
network.getIndex().remove(ss);
} else {
network.getListeners().notifyElementRemoved(voltageLevel, INTERNAL_CONNECTION, null);
network.getListeners().notifyPropertyRemoved(voltageLevel, INTERNAL_CONNECTION, null);
}
});
switches.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ public Substation addGeographicalTag(String tag) {
if (tag == null) {
throw new ValidationException(this, "geographical tag is null");
}
Set<String> oldGeographicalTags = new LinkedHashSet<>(geographicalTags);
if (geographicalTags.add(tag)) {
getNetwork().getListeners().notifyElementAdded(this, "geographicalTags", tag);
getNetwork().getListeners().notifyUpdate(this, "geographicalTags", oldGeographicalTags, geographicalTags);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void customOperationalLimitsGroupTest() {
boolean[] updated = new boolean[1];
network.addListener(new DefaultNetworkListener() {
@Override
public void onUpdate(Identifiable<?> identifiable, String attribute, Object oldValue, Object newValue) {
public void onUpdate(Identifiable<?> identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
assertEquals("NHV1", identifiable.getId());
assertEquals("limits_CURRENT", attribute);
assertNull(((OperationalLimitsGroupImpl.OperationalLimitsInfo) oldValue).value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,8 @@ public void testMove2Nb() {
public void testChangesNotification() {
// Changes listener
NetworkListener exceptionListener = mock(DefaultNetworkListener.class);
doThrow(new UnsupportedOperationException()).when(exceptionListener).onUpdate(any(), anyString(), any(), any());
doThrow(new UnsupportedOperationException()).when(exceptionListener).onUpdate(any(), any(), anyString(), any(),
any());
doThrow(new UnsupportedOperationException()).when(exceptionListener).onUpdate(any(), anyString(), anyString(), any(), any());
doThrow(new UnsupportedOperationException()).when(exceptionListener).onUpdate(any(), any(), anyString(), any(), any());

NetworkListener mockedListener = mock(DefaultNetworkListener.class);
// Add observer changes to current network
Expand Down Expand Up @@ -369,12 +368,12 @@ public void testChangesNotification() {

// Change values that not depend on the variant
acLine.setR(1.5);
verify(mockedListener, times(1)).onUpdate(acLine, "r", 1.0, 1.5);
verify(mockedListener, times(1)).onUpdate(acLine, "r", null, 1.0, 1.5);

// Simulate exception for onUpdate calls
doThrow(new PowsyblException())
.when(mockedListener)
.onUpdate(any(Line.class), anyString(), anyDouble(), anyDouble());
.onUpdate(any(Line.class), anyString(), anyString(), anyDouble(), anyDouble());
// Change P1 value
try {
acLine.getTerminal1().setP(1.1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void setNameTest() {
assertTrue(load.getOptionalName().isEmpty());
load.setName("FOO");
assertEquals("FOO", load.getOptionalName().orElseThrow());
Mockito.verify(mockedListener, Mockito.times(1)).onUpdate(load, "name", null, "FOO");
Mockito.verify(mockedListener, Mockito.times(1)).onUpdate(load, "name", null, null, "FOO");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void nodeBreakerTest() {
MutableObject<Object> obj = new MutableObject<>();
network.addListener(new DefaultNetworkListener() {
@Override
public void onUpdate(Identifiable identifiable, String attribute, Object oldValue, Object newValue) {
public void onUpdate(Identifiable identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
obj.setValue(newValue);
}
});
Expand All @@ -47,7 +47,7 @@ public void busBreakerTest() {
MutableObject<Object> obj = new MutableObject<>();
network.addListener(new DefaultNetworkListener() {
@Override
public void onUpdate(Identifiable identifiable, String attribute, Object oldValue, Object newValue) {
public void onUpdate(Identifiable identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
obj.setValue(newValue);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ public void testNetwork1() {

// Changes listener
NetworkListener exceptionListener = mock(DefaultNetworkListener.class);
doThrow(new UnsupportedOperationException()).when(exceptionListener).onElementAdded(any(), anyString(), any());
doThrow(new UnsupportedOperationException()).when(exceptionListener).onElementReplaced(any(), anyString(),
doThrow(new UnsupportedOperationException()).when(exceptionListener).onPropertyAdded(any(), anyString(), any());
doThrow(new UnsupportedOperationException()).when(exceptionListener).onPropertyReplaced(any(), anyString(),
any(), any());
NetworkListener mockedListener = mock(DefaultNetworkListener.class);

Expand Down Expand Up @@ -206,12 +206,12 @@ public void testNetwork1() {

// Check notification done
verify(mockedListener, times(1))
.onElementAdded(busCalc, "properties[" + key + "]", value);
.onPropertyAdded(busCalc, "properties[" + key + "]", value);
// Check no notification on same property
String value2 = "ValueTest2";
busCalc.setProperty(key, value2);
verify(mockedListener, times(1))
.onElementReplaced(busCalc, "properties[" + key + "]", value, value2);
.onPropertyReplaced(busCalc, "properties[" + key + "]", value, value2);
// Check no notification on same property
busCalc.setProperty(key, value2);
verifyNoMoreInteractions(mockedListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void baseTests() {

// Create mocked network listeners
NetworkListener exceptionListener = mock(DefaultNetworkListener.class);
doThrow(new UnsupportedOperationException()).when(exceptionListener).onElementAdded(any(), anyString(), any());
doThrow(new UnsupportedOperationException()).when(exceptionListener).onPropertyAdded(any(), anyString(), any());
NetworkListener mockedListener = mock(DefaultNetworkListener.class);
// Test without listeners registered
substation.addGeographicalTag("no listeners");
Expand All @@ -71,7 +71,7 @@ public void baseTests() {
substation.addGeographicalTag("test");
// Check notification done
verify(mockedListener, times(1))
.onElementAdded(any(Substation.class), anyString(), anyString());
.onUpdate(any(Substation.class), anyString(), isNull(), any(), any());
// Remove observer
network.removeListener(mockedListener);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void baseTestsPhaseTapChanger() {
currentStep.setB(5.0);
currentStep.setAlpha(6.0);
currentStep.setRho(7.0);
verify(mockedListener, times(6)).onUpdate(any(Identifiable.class), anyString(), any(), any());
verify(mockedListener, times(6)).onUpdate(any(Identifiable.class), anyString(), nullable(String.class), any(), any());
// Remove observer
network.removeListener(mockedListener);
// Cancel modification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void testTieLineAdder() {
danglingLine2.setX(x + 1);
danglingLine2.setG(hl2g1 + hl1g2 + 2);
danglingLine2.setB(hl2b1 + hl2b2 + 2);
verify(mockedListener, times(8)).onUpdate(any(DanglingLine.class), anyString(), any(), any());
verify(mockedListener, times(8)).onUpdate(any(DanglingLine.class), anyString(), nullable(String.class), any(), any());
// Remove observer
network.removeListener(mockedListener);
// Cancel changes on dangling lines
Expand Down

0 comments on commit 4d5ff02

Please sign in to comment.