org.mydotey.scf.labeled.AbstractLabeledConfigurationSource Maven / Gradle / Ivy
package org.mydotey.scf.labeled;
import java.util.Collection;
import java.util.Collections;
import org.mydotey.scf.AbstractConfigurationSource;
import org.mydotey.scf.ConfigurationSourceConfig;
import org.mydotey.scf.PropertyConfig;
import org.mydotey.scf.facade.ConfigurationProperties;
/**
* @author koqizhao
*
* May 16, 2018
*/
public abstract class AbstractLabeledConfigurationSource
extends AbstractConfigurationSource implements LabeledConfigurationSource {
static PropertyConfig removeLabels(PropertyConfig, V> config) {
return ConfigurationProperties. newConfigBuilder().setKey(config.getKey().getKey())
.setValueType(config.getValueType()).setDefaultValue(config.getDefaultValue())
.addValueConverters(config.getValueConverters()).setValueFilter(config.getValueFilter()).build();
}
public AbstractLabeledConfigurationSource(C config) {
super(config);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public V getPropertyValue(PropertyConfig config) {
PropertyConfig, V> noLabelConfig = config;
if (config.getKey() instanceof LabeledKey)
noLabelConfig = removeLabels((PropertyConfig) config);
return super.getPropertyValue(noLabelConfig);
}
@Override
protected Object getPropertyValue(Object key) {
return getPropertyValue(key, Collections.emptyList());
}
@Override
public V getPropertyValue(PropertyConfig noLabelConfig, Collection labels) {
Object value = getPropertyValue(noLabelConfig.getKey(), labels);
return convert(noLabelConfig, value);
}
protected abstract Object getPropertyValue(Object key, Collection labels);
}