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

com.powsybl.dataframe.EnumSeriesMapper 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.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;

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

    private final SeriesMetadata metadata;
    private final Class enumClass;
    private final BiConsumer updater;
    private final Function value;

    public EnumSeriesMapper(String name, Class enumClass, Function value) {
        this(name, enumClass, value, null, true);
    }

    public EnumSeriesMapper(String name, Class enumClass, Function value, BiConsumer updater, boolean defaultAttribute) {
        this.metadata = new SeriesMetadata(false, name, updater != null, SeriesDataType.STRING, defaultAttribute);
        this.enumClass = enumClass;
        this.updater = updater;
        this.value = value;
    }

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

    @Override
    public void createSeries(List items, DataframeHandler factory, C context) {
        DataframeHandler.StringSeriesWriter writer = factory.newStringSeries(metadata.getName(), items.size());
        for (int i = 0; i < items.size(); i++) {
            writer.set(i, Objects.toString(value.apply(items.get(i)), ""));
        }
    }

    @Override
    public void updateString(T object, String stringValue) {
        if (updater == null) {
            throw new UnsupportedOperationException("Series '" + getMetadata().getName() + "' is not modifiable.");
        }
        E enumValue = Enum.valueOf(enumClass, stringValue.toUpperCase());
        updater.accept(object, enumValue);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy