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

com.powsybl.dataframe.BooleanSeriesMapper 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.Predicate;

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

    private final SeriesMetadata metadata;
    private final BooleanUpdater updater;
    private final Predicate value;

    @FunctionalInterface
    public interface BooleanUpdater {
        void update(U object, boolean value);
    }

    public BooleanSeriesMapper(String name, Predicate value) {
        this(name, value, null, true);
    }

    public BooleanSeriesMapper(String name, Predicate value, BooleanUpdater updater, boolean defaultAttribute) {
        this.metadata = new SeriesMetadata(false, name, updater != null, SeriesDataType.BOOLEAN, defaultAttribute);
        this.updater = updater;
        this.value = value;
    }

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

    @Override
    public void createSeries(List items, DataframeHandler handler, C context) {
        DataframeHandler.BooleanSeriesWriter writer = handler.newBooleanSeries(metadata.getName(), items.size());
        for (int i = 0; i < items.size(); i++) {
            writer.set(i, value.test(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 == 1);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy