com.powsybl.iidm.serde.AbstractComplexIdentifiableSerDe Maven / Gradle / Ivy
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.serde;
import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.IdentifiableAdder;
import com.powsybl.iidm.serde.util.IidmSerDeUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
/**
* Abstract class for serializing/deserializing equipment that need their sub-elements to be entirely created.
*
* @author Miora Vedelago {@literal }
*/
abstract class AbstractComplexIdentifiableSerDe, A extends IdentifiableAdder, P extends Identifiable> extends AbstractIdentifiableSerDe {
protected abstract void readRootElementAttributes(A adder, P parent, List> toApply, NetworkDeserializerContext context);
protected void readSubElement(String elementName, String id, List> toApply, NetworkDeserializerContext context) {
switch (elementName) {
case PropertiesSerDe.ROOT_ELEMENT_NAME -> PropertiesSerDe.read(toApply, context);
case AliasesSerDe.ROOT_ELEMENT_NAME -> {
IidmSerDeUtil.assertMinimumVersion(getRootElementName(), AliasesSerDe.ROOT_ELEMENT_NAME, IidmSerDeUtil.ErrorMessage.NOT_SUPPORTED, IidmVersion.V_1_3, context);
AliasesSerDe.read(toApply, context);
}
default -> throw new PowsyblException("Unknown element name '" + elementName + "' in '" + id + "'");
}
}
protected abstract void readSubElements(String id, A adder, List> toApply, NetworkDeserializerContext context);
@Override
public final void read(P parent, NetworkDeserializerContext context) {
List> toApply = new ArrayList<>();
A adder = createAdder(parent);
String id = readIdentifierAttributes(adder, context);
readRootElementAttributes(adder, parent, toApply, context);
readSubElements(id, adder, toApply, context);
if (postponeElementCreation()) {
context.getEndTasks().add(() -> createElement(adder, toApply));
} else {
createElement(adder, toApply);
}
}
/**
* Should the current element's creation be postponed?
* In some specific cases, the element could not be created right after it is read, typically if it references
* other network elements which may have not been yet created. (It is better to create the element without the said
* references and to fill them in later, but it is not always possible, or at high cost.)
* If this method returns true
, the element's creation will be defined as an "end task" and
* will be performed after the whole network is read.
* @return true
if the creation should be postponed, false
otherwise.
*/
protected boolean postponeElementCreation() {
return false;
}
private static , A extends IdentifiableAdder> void createElement(A adder, List> toApply) {
T identifiable = adder.add();
toApply.forEach(consumer -> consumer.accept(identifiable));
}
}