Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Improve usage of normalizer for configuration properties in Automation module. #1938

Merged
merged 1 commit into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.eclipse.smarthome.automation.type.TriggerType;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter.Type;
import org.eclipse.smarthome.config.core.ConfigUtil;
import org.eclipse.smarthome.config.core.Configuration;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
Expand Down Expand Up @@ -1185,25 +1186,29 @@ protected void setCompositeModuleFactory(CompositeModuleHandlerFactory composite
}

private void resolveDefaultValues(RuntimeRule r) {
setDefautlValues(r.getUID(), r.getTriggers());
setDefautlValues(r.getUID(), r.getConditions());
setDefautlValues(r.getUID(), r.getActions());
setDefaultAndNormalizeConfigValues(r.getUID(), r.getTriggers());
setDefaultAndNormalizeConfigValues(r.getUID(), r.getConditions());
setDefaultAndNormalizeConfigValues(r.getUID(), r.getActions());
}

private <T extends Module> void setDefautlValues(String ruleUID, List<T> modules) {
private <T extends Module> void setDefaultAndNormalizeConfigValues(String ruleUID, List<T> modules) {
for (T module : modules) {
Configuration moduleConfiguration = module.getConfiguration();
String typeId = module.getTypeUID();
ModuleType mt = mtManager.get(typeId);
List<ConfigDescriptionParameter> configs = mt.getConfigurationDescriptions();
if (configs != null) {
for (ConfigDescriptionParameter config : configs) {
String defaultValue = config.getDefault();
if (defaultValue != null) {
String configName = config.getName();
if (moduleConfiguration.get(configName) == null) {
moduleConfiguration.put(configName, defaultValue);
List<ConfigDescriptionParameter> configDescriptions = mt.getConfigurationDescriptions();
if (configDescriptions != null) {
for (ConfigDescriptionParameter configDescription : configDescriptions) {
String defaultValue = configDescription.getDefault();
String configName = configDescription.getName();
Object configValue = moduleConfiguration.get(configName);
if (configValue == null) {
if (defaultValue != null) {
moduleConfiguration.put(configName,
ConfigUtil.normalizeType(defaultValue, configDescription));
}
} else {
moduleConfiguration.put(configName, ConfigUtil.normalizeType(configValue, configDescription));
}
} // for
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.eclipse.smarthome.config.core.normalization.NormalizerFactory;
import org.eclipse.smarthome.config.core.validation.ConfigDescriptionValidator;

import com.google.common.base.Preconditions;

/**
* This class provides some useful static methods for handling configurations
*
Expand Down Expand Up @@ -50,7 +48,23 @@ public static Map<String, Object> normalizeTypes(Map<String, Object> configurati
* @return corresponding value as a valid type
*/
public static Object normalizeType(Object value) {
return value instanceof Double ? BigDecimal.valueOf((Double) value) : value;
return normalizeType(value, null);
}

/**
* normalizes the type of the parameter to the one allowed for configurations
*
* @param value the value to return as normalized type
*
* @return corresponding value as a valid type
*/
public static Object normalizeType(Object value, ConfigDescriptionParameter configDescriptionParameter) {
if (configDescriptionParameter != null) {
Normalizer normalizer = NormalizerFactory.getNormalizer(configDescriptionParameter);
return normalizer.normalize(value);
} else {
return value instanceof Double ? BigDecimal.valueOf((Double) value) : value;
}
}

/**
Expand All @@ -69,7 +83,9 @@ public static Object normalizeType(Object value) {
*/
public static Map<String, Object> normalizeTypes(Map<String, Object> configuration,
ConfigDescription configDescription) {
Preconditions.checkNotNull(configDescription, "Config description must not be null.");
if (configDescription == null) {
throw new NullPointerException("Config description must not be null.");
}

if (configuration == null) {
return null;
Expand All @@ -81,12 +97,7 @@ public static Map<String, Object> normalizeTypes(Map<String, Object> configurati
String name = parameter.getKey();
Object value = parameter.getValue();
ConfigDescriptionParameter configDescriptionParameter = configParams.get(name);
if (configDescriptionParameter != null) {
Normalizer normalizer = NormalizerFactory.getNormalizer(configDescriptionParameter);
convertedConfiguration.put(name, normalizer.normalize(value));
} else {
convertedConfiguration.put(name, value);
}
convertedConfiguration.put(name, normalizeType(value, configDescriptionParameter));
}
return convertedConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
*/
package org.eclipse.smarthome.config.core.normalization;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter.Type;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;

/**
* The {@link NormalizerFactory} can be used in order to obtain the {@link Normalizer} for any concrete
* {@link ConfigDescriptionParameter.Type}.
Expand All @@ -24,9 +23,16 @@
*/
public final class NormalizerFactory {

private static final Map<Type, Normalizer> normalizers = new ImmutableMap.Builder<Type, Normalizer>()
.put(Type.BOOLEAN, new BooleanNormalizer()).put(Type.TEXT, new TextNormalizer())
.put(Type.INTEGER, new IntNormalizer()).put(Type.DECIMAL, new DecimalNormalizer()).build();
private static final Map<Type, Normalizer> normalizers;

static {
Map<Type, Normalizer> map = new HashMap<Type, Normalizer>(11);
map.put(Type.BOOLEAN, new BooleanNormalizer());
map.put(Type.TEXT, new TextNormalizer());
map.put(Type.INTEGER, new IntNormalizer());
map.put(Type.DECIMAL, new DecimalNormalizer());
normalizers = Collections.unmodifiableMap(map);
}

private NormalizerFactory() {
// prevent instantiation
Expand All @@ -40,7 +46,9 @@ private NormalizerFactory() {
* @throws NullPointerException if the given config description parameter is null
*/
public static Normalizer getNormalizer(ConfigDescriptionParameter configDescriptionParameter) {
Preconditions.checkNotNull(configDescriptionParameter, "The config description parameter must not be null.");
if (configDescriptionParameter == null) {
throw new NullPointerException("The config description parameter must not be null.");
}

Normalizer ret = normalizers.get(configDescriptionParameter.getType());

Expand Down