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

com.powsybl.glsk.cse.BlockWrapper Maven / Gradle / Ivy

The newest version!
/*
 * 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/.
 */
package com.powsybl.glsk.cse;

import xsd.etso_core_cmpts.QuantityType;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Vincent BOCHET {@literal }
 */
public final class BlockWrapper {
    private final Object block;

    public BlockWrapper(Object block) {
        this.block = block;
    }

    public Object getBlock() {
        return block;
    }

    public Optional getOrder() {
        try {
            BigInteger order = (BigInteger) block.getClass().getDeclaredField("order").get(block);
            return Optional.ofNullable(order);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            return Optional.empty();
        }
    }

    public Optional getMaximumShift() {
        try {
            QuantityType maximumShift = (QuantityType) block.getClass().getDeclaredField("maximumShift").get(block);
            return Optional.ofNullable(maximumShift).map(QuantityType::getV);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            return Optional.empty();
        }
    }

    public Optional getFactor() {
        try {
            QuantityType factor = (QuantityType) block.getClass().getDeclaredField("factor").get(block);
            return Optional.ofNullable(factor).map(QuantityType::getV);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            return Optional.empty();
        }
    }

    public Optional> getNodeList() {
        try {
            List objectList = (List) block.getClass().getDeclaredField("node").get(block);
            return Optional.ofNullable(objectList)
                .map(list -> list.stream().map(NodeWrapper::new).collect(Collectors.toList()));
        } catch (NoSuchFieldException | IllegalAccessException e) {
            return Optional.empty();
        }
    }
}