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

com.opsbears.webcomponents.configuration.ConfigurationOptionGroupWithValues Maven / Gradle / Ivy

There is a newer version: 1.0.0-alpha14
Show newest version
package com.opsbears.webcomponents.configuration;

import com.opsbears.webcomponents.immutable.ImmutableArrayList;
import com.opsbears.webcomponents.typeconverter.TypeConverterChain;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.stream.Collectors;

@ParametersAreNonnullByDefault
public class ConfigurationOptionGroupWithValues extends AbstractConfigurationOptionGroup {
    public ConfigurationOptionGroupWithValues(
        ConfigurationOptionGroup configurationOptionGroup,
        TypeConverterChain typeConverterChain
    ) {
        //noinspection unchecked
        super(
            configurationOptionGroup.uniqueId,
            configurationOptionGroup.title,
            configurationOptionGroup.description,
            new ImmutableArrayList(
                (List)configurationOptionGroup
                    .options
                    .stream()
                    .map(
                        configurationOption -> new ConfigurationOptionWithValue<>(
                            configurationOption,
                            null,
                            typeConverterChain
                        )
                    )
                    .collect(
                        Collectors.toList()
                    )
            )
        );
    }

    ConfigurationOptionGroupWithValues(
        String title,
        String description,
        List options
    ) {
        super(
            title,
            description,
            options
        );
    }

    public ConfigurationOptionGroupWithValues withValue(
        String longOption,
        Object value
    ) {
        return new ConfigurationOptionGroupWithValues(
            getTitle(),
            getDescription(),
            options.withReplaceAll(configurationOptionWithValue -> {
                if (
                    configurationOptionWithValue.matchesLongOption(longOption)
                ) {
                    //noinspection unchecked
                    return configurationOptionWithValue.withValue(value);
                } else {
                    return configurationOptionWithValue;
                }
            })
        );
    }


    public ConfigurationOptionGroupWithValues withShortValue(
        String shortOption,
        Object value
    ) {
        return new ConfigurationOptionGroupWithValues(
            getTitle(),
            getDescription(),
            options.withReplaceAll(configurationOptionWithValue -> {
                if (
                    configurationOptionWithValue.matchesShortOption(shortOption)
                ) {
                    //noinspection unchecked
                    return configurationOptionWithValue.withValue(value);
                } else {
                    return configurationOptionWithValue;
                }
            })
        );
    }

    public Object getValue(String longOptionName) {
        return getOption(longOptionName).getValue();
    }

    public  T getValue(String longOptionName, Class classDefinition) {
        //noinspection unchecked
        return (T) getOption(longOptionName).getValueAsType(classDefinition);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy