io.helidon.config.ConfigValues Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helidon-config Show documentation
Show all versions of helidon-config Show documentation
Configuration Core module.
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.config;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import io.helidon.common.GenericType;
/**
* Factory for config values.
*/
public final class ConfigValues {
private ConfigValues() {
}
/**
* Simple empty value that can be used e.g. for unit testing.
* All ConfigValues use equals method that only cares about the optional value.
*
* @param type of the value
* @return a config value that is empty
*/
public static ConfigValue empty() {
return new ConfigValueBase(Config.Key.create("")) {
@Override
public Optional asOptional() {
return Optional.empty();
}
@Override
public ConfigValue as(Function mapper) {
return empty();
}
@Override
public Supplier supplier() {
return () -> {
throw MissingValueException.create(key());
};
}
@Override
public Supplier supplier(T defaultValue) {
return () -> defaultValue;
}
@Override
public Supplier> optionalSupplier() {
return Optional::empty;
}
@Override
public String toString() {
return "ConfigValue(empty)";
}
};
}
/**
* Simple value that can be used e.g. for unit testing.
* All ConfigValues use equals method that only cares about the optional value.
*
* @param value value to use
* @param type of the value
* @return a config value that uses the value provided
*/
public static ConfigValue simpleValue(T value) {
return new ConfigValueBase(Config.Key.create("")) {
@Override
public Optional asOptional() {
return Optional.ofNullable(value);
}
@Override
public ConfigValue as(Function mapper) {
return simpleValue(mapper.apply(value));
}
@Override
public Supplier supplier() {
return () -> value;
}
@Override
public Supplier supplier(T defaultValue) {
return () -> asOptional().orElse(defaultValue);
}
@Override
public Supplier> optionalSupplier() {
return this::asOptional;
}
@Override
public String toString() {
return "ConfigValue(" + value + ")";
}
};
}
static ConfigValue create(Config config,
Supplier> supplier,
Function> configMethod) {
return new GenericConfigValueImpl<>(config, supplier, configMethod);
}
static ConfigValue create(Config config,
Class type,
ConfigMapperManager mapperManager) {
return new GenericConfigValueImpl<>(config,
() -> Optional.ofNullable(mapperManager.map(config, type)),
aConfig -> aConfig.as(type));
}
static ConfigValue create(Config config,
GenericType genericType,
ConfigMapperManager mapperManager) {
return new GenericConfigValueImpl<>(config,
() -> Optional.ofNullable(mapperManager.map(config, genericType)),
aConfig -> aConfig.as(genericType));
}
static ConfigValue create(Config config,
Function mapper) {
return new GenericConfigValueImpl<>(config,
() -> Optional.ofNullable(mapper.apply(config)),
aConfig -> aConfig.as(mapper));
}
static ConfigValue> createList(Config config,
Function> getValue,
Function>> getListValue) {
Supplier>> valueSupplier = () -> {
try {
return config.asNodeList()
.map(list -> list.stream()
.map(theConfig -> getValue.apply(theConfig).get())
.collect(Collectors.toList())
);
} catch (MissingValueException | ConfigMappingException ex) {
throw new ConfigMappingException(config.key(),
"Error to map complex node item to list. " + ex.getLocalizedMessage(),
ex);
}
};
return new GenericConfigValueImpl<>(config, valueSupplier, getListValue);
}
static ConfigValue