com.opsbears.webcomponents.configuration.ConfigurationOptionGroupWithValues Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of configuration Show documentation
Show all versions of configuration Show documentation
Utilities for creating and reading configuration options
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);
}
}