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

com.powsybl.dataframe.DoubleSeriesMapper Maven / Gradle / Ivy

The newest version!
/**
 * 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;

import java.util.List;
import java.util.function.ToDoubleBiFunction;

/**
 * @author Sylvain Leclerc {@literal }
 */
public class DoubleSeriesMapper implements SeriesMapper {

    private final SeriesMetadata metadata;
    private final DoubleUpdater updater;
    private final ToDoubleBiFunction value;

    @FunctionalInterface
    public interface DoubleUpdater {
        void update(U object, double value, C context);
    }

    public DoubleSeriesMapper(String name, ToDoubleBiFunction value) {
        this(name, value, null, true);
    }

    public DoubleSeriesMapper(String name, ToDoubleBiFunction value, DoubleUpdater updater, boolean defaultAttribute) {
        this.metadata = new SeriesMetadata(false, name, updater != null, SeriesDataType.DOUBLE, defaultAttribute);
        this.updater = updater;
        this.value = value;
    }

    @Override
    public SeriesMetadata getMetadata() {
        return metadata;
    }

    @Override
    public void createSeries(List items, DataframeHandler factory, C context) {

        DataframeHandler.DoubleSeriesWriter writer = factory.newDoubleSeries(metadata.getName(), items.size());
        for (int i = 0; i < items.size(); i++) {
            writer.set(i, value.applyAsDouble(items.get(i), context));
        }
    }

    @Override
    public void updateDouble(T object, double value, C context) {
        if (updater == null) {
            throw new UnsupportedOperationException("Series '" + getMetadata().getName() + "' is not modifiable.");
        }
        updater.update(object, value, context);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy