All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.powerfactory.model.DataObjectIndex Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2022, 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.powerfactory.model;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Geoffroy Jamgotchian {@literal }
 */
public class DataObjectIndex {

    private final Map objectsById = new HashMap<>();

    private final Map> objectsByClass = new HashMap<>();

    private Map> backwardLinks;

    public void addDataObject(DataObject obj) {
        Objects.requireNonNull(obj);
        if (objectsById.containsKey(obj.getId())) {
            throw new PowerFactoryException("Object '" + obj.getId() + "' already exists");
        }
        objectsById.put(obj.getId(), obj);
        objectsByClass.computeIfAbsent(obj.getDataClassName(), k -> new ArrayList<>())
                .add(obj);
        if (backwardLinks != null) {
            indexBackwardLinks(obj);
        }
    }

    public Collection getDataObjects() {
        return objectsById.values();
    }

    public List getRootDataObjects() {
        return objectsById.values().stream().filter(obj -> obj.getParent() == null).collect(Collectors.toList());
    }

    public Optional getDataObjectById(long id) {
        return Optional.ofNullable(objectsById.get(id));
    }

    public List getDataObjectsByClass(String className) {
        Objects.requireNonNull(className);
        return objectsByClass.getOrDefault(className, Collections.emptyList());
    }

    private void indexBackwardLinks(DataObject obj) {
        for (DataAttribute attribute : obj.getDataClass().getAttributes()) {
            if (attribute.getType() == DataAttributeType.OBJECT) {
                obj.findObjectAttributeValue(attribute.getName())
                        .flatMap(DataObjectRef::resolve)
                        .ifPresent(link -> backwardLinks.computeIfAbsent(link.getId(), k -> new ArrayList<>())
                                .add(obj));
            } else if (attribute.getType() == DataAttributeType.OBJECT_VECTOR) {
                obj.findObjectVectorAttributeValue(attribute.getName())
                        .ifPresent(refs -> {
                            for (DataObjectRef ref : refs) {
                                ref.resolve().ifPresent(object ->
                                        backwardLinks.computeIfAbsent(ref.getId(), k -> new ArrayList<>())
                                                .add(obj));
                            }
                        });
            }
        }
    }

    private void indexBackwardLinks() {
        if (backwardLinks == null) {
            backwardLinks = new LinkedHashMap<>();
            for (DataObject obj : objectsById.values()) {
                indexBackwardLinks(obj);
            }
        }
    }

    public List getBackwardLinks(long id) {
        indexBackwardLinks();
        return backwardLinks.getOrDefault(id, Collections.emptyList());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy