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

com.powsybl.dataframe.network.NetworkDataframeMapperBuilder Maven / Gradle / Ivy

/**
 * Copyright (c) 2021, 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.dataframe.network;

import com.powsybl.dataframe.BaseDataframeMapperBuilder;
import com.powsybl.dataframe.update.UpdatingDataframe;
import com.powsybl.iidm.network.Network;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * Specific build for network mappers :
 * it provides network-specific features, in particular the {@link #addProperties()} method.
 *
 * @author Sylvain Leclerc {@literal }
 */
public class NetworkDataframeMapperBuilder extends BaseDataframeMapperBuilder> {

    private boolean addProperties;

    public NetworkDataframeMapperBuilder() {
        this.addProperties = false;
    }

    public static  NetworkDataframeMapperBuilder ofStream(Function> itemProvider, ItemGetter itemGetter) {
        return new NetworkDataframeMapperBuilder()
                .itemsStreamProvider(itemProvider)
                .itemMultiIndexGetter(itemGetter);
    }

    public static  NetworkDataframeMapperBuilder ofStream(Function> itemProvider, BiFunction itemGetter) {
        return new NetworkDataframeMapperBuilder()
                .itemsStreamProvider(itemProvider)
                .itemGetter(itemGetter);
    }

    public static  NetworkDataframeMapperBuilder ofStream(BiFunction> itemProvider, ItemGetterWithContext itemGetter) {
        return new NetworkDataframeMapperBuilder()
                .itemsStreamProvider(itemProvider)
                .itemMultiIndexGetter(itemGetter);
    }

    public static  NetworkDataframeMapperBuilder ofStream(BiFunction> itemProvider, BiFunction itemGetter) {
        return new NetworkDataframeMapperBuilder()
                .itemsStreamProvider(itemProvider)
                .itemGetter(itemGetter);
    }

    public static  NetworkDataframeMapperBuilder ofStream(Function> streamProvider) {
        BiFunction noUpdate = (n, s) -> {
            throw new UnsupportedOperationException("Update is not supported");
        };
        return NetworkDataframeMapperBuilder.ofStream(streamProvider, noUpdate);
    }

    public static  NetworkDataframeMapperBuilder ofStream(BiFunction> streamProvider) {
        BiFunction noUpdate = (n, s) -> {
            throw new UnsupportedOperationException("Update is not supported");
        };
        return NetworkDataframeMapperBuilder.ofStream(streamProvider, noUpdate);
    }

    public NetworkDataframeMapperBuilder addProperties() {
        addProperties = true;
        return this;
    }

    @Override
    public NetworkDataframeMapper build() {
        return new AbstractNetworkDataframeMapper(series, addProperties) {
            @Override
            protected List getItems(Network network, NetworkDataframeContext context) {
                return itemsProvider.apply(network, context);
            }

            @Override
            protected T getItem(Network network, UpdatingDataframe updatingDataframe, int index, NetworkDataframeContext context) {
                return itemMultiIndexGetter.getItem(network, updatingDataframe, index, context);
            }
        };
    }
}