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

com.powsybl.dataframe.IntSeriesMapper 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.ToIntFunction;

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

    private final SeriesMetadata metadata;
    private final IntUpdater updater;
    private final ToIntFunction value;

    @FunctionalInterface
    public interface IntUpdater {
        void update(U object, int value);
    }

    public IntSeriesMapper(String name, ToIntFunction value) {
        this(name, false, value, null, true);
    }

    public IntSeriesMapper(String name, boolean index, ToIntFunction value) {
        this(name, index, value, null, true);
    }

    public IntSeriesMapper(String name, ToIntFunction value, IntUpdater updater) {
        this(name, false, value, updater, true);
    }

    public IntSeriesMapper(String name, ToIntFunction value, IntUpdater updater, boolean defaultAttribute) {
        this(name, false, value, updater, defaultAttribute);
    }

    public IntSeriesMapper(String name, boolean index, ToIntFunction value, IntUpdater updater, boolean defaultAttribute) {
        this.metadata = new SeriesMetadata(index, name, updater != null, SeriesDataType.INT, defaultAttribute);
        this.updater = updater;
        this.value = value;
    }

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

    @Override
    public void createSeries(List items, DataframeHandler handler, C context) {
        boolean index = metadata.isIndex();
        String name = metadata.getName();
        DataframeHandler.IntSeriesWriter writer = index ? handler.newIntIndex(name, items.size()) : handler.newIntSeries(name, items.size());
        for (int i = 0; i < items.size(); i++) {
            writer.set(i, value.applyAsInt(items.get(i)));
        }
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy