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

improve nullness handling for i18n stuff #5864

Merged
merged 1 commit into from
Jul 6, 2018
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 @@ -74,15 +74,14 @@ private static Action createLocalizedAction(Action module, String label, String

private static String getModuleLabel(TranslationProvider i18nProvider, Bundle bundle, String uid, String moduleName,
String defaultLabel, String prefix, Locale locale) {
String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel)
: inferModuleKey(prefix, uid, moduleName, "label");
String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferModuleKey(prefix, uid, moduleName, "label"));
return i18nProvider.getText(bundle, key, defaultLabel, locale);
}

private static String getModuleDescription(TranslationProvider i18nProvider, Bundle bundle, String uid,
String moduleName, String defaultDescription, String prefix, Locale locale) {
String key = I18nUtil.isConstant(defaultDescription) ? I18nUtil.stripConstant(defaultDescription)
: inferModuleKey(prefix, uid, moduleName, "description");
String key = I18nUtil.stripConstantOr(defaultDescription,
() -> inferModuleKey(prefix, uid, moduleName, "description"));
return i18nProvider.getText(bundle, key, defaultDescription, locale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import org.eclipse.smarthome.automation.type.Input;
import org.eclipse.smarthome.automation.type.Output;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.eclipse.smarthome.core.i18n.I18nUtil;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.osgi.framework.Bundle;

/**
Expand All @@ -34,17 +34,16 @@ public class ModuleTypeI18nUtil {

public static final String MODULE_TYPE = "module-type";

public static String getLocalizedModuleTypeLabel(TranslationProvider i18nProvider, Bundle bundle, String moduleTypeUID,
String defaultLabel, Locale locale) {
String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel)
: inferModuleTypeKey(moduleTypeUID, "label");
public static String getLocalizedModuleTypeLabel(TranslationProvider i18nProvider, Bundle bundle,
String moduleTypeUID, String defaultLabel, Locale locale) {
String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferModuleTypeKey(moduleTypeUID, "label"));
return i18nProvider.getText(bundle, key, defaultLabel, locale);
}

public static String getLocalizedModuleTypeDescription(TranslationProvider i18nProvider, Bundle bundle,
String moduleTypeUID, String defaultDescription, Locale locale) {
String key = I18nUtil.isConstant(defaultDescription) ? I18nUtil.stripConstant(defaultDescription)
: inferModuleTypeKey(moduleTypeUID, "description");
String key = I18nUtil.stripConstantOr(defaultDescription,
() -> inferModuleTypeKey(moduleTypeUID, "description"));
return i18nProvider.getText(bundle, key, defaultDescription, locale);
}

Expand All @@ -65,8 +64,8 @@ public static List<Input> getLocalizedInputs(TranslationProvider i18nProvider, L
return linputs;
}

public static List<Output> getLocalizedOutputs(TranslationProvider i18nProvider, List<Output> outputs, Bundle bundle,
String uid, Locale locale) {
public static List<Output> getLocalizedOutputs(TranslationProvider i18nProvider, List<Output> outputs,
Bundle bundle, String uid, Locale locale) {
List<Output> loutputs = new ArrayList<Output>();
if (outputs != null) {
for (Output output : outputs) {
Expand All @@ -84,29 +83,27 @@ public static List<Output> getLocalizedOutputs(TranslationProvider i18nProvider,

private static String getInputLabel(TranslationProvider i18nProvider, Bundle bundle, String moduleTypeUID,
String inputName, String defaultLabel, Locale locale) {
String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel)
: inferInputKey(moduleTypeUID, inputName, "label");
String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferInputKey(moduleTypeUID, inputName, "label"));
return i18nProvider.getText(bundle, key, defaultLabel, locale);
}

private static String getInputDescription(TranslationProvider i18nProvider, Bundle bundle, String moduleTypeUID,
String inputName, String defaultDescription, Locale locale) {
String key = I18nUtil.isConstant(defaultDescription) ? I18nUtil.stripConstant(defaultDescription)
: inferInputKey(moduleTypeUID, inputName, "description");
String key = I18nUtil.stripConstantOr(defaultDescription,
() -> inferInputKey(moduleTypeUID, inputName, "description"));
return i18nProvider.getText(bundle, key, defaultDescription, locale);
}

private static String getOutputLabel(TranslationProvider i18nProvider, Bundle bundle, String ruleTemplateUID,
String outputName, String defaultLabel, Locale locale) {
String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel)
: inferOutputKey(ruleTemplateUID, outputName, "label");
String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferOutputKey(ruleTemplateUID, outputName, "label"));
return i18nProvider.getText(bundle, key, defaultLabel, locale);
}

public static String getOutputDescription(TranslationProvider i18nProvider, Bundle bundle, String moduleTypeUID,
String outputName, String defaultDescription, Locale locale) {
String key = I18nUtil.isConstant(defaultDescription) ? I18nUtil.stripConstant(defaultDescription)
: inferOutputKey(moduleTypeUID, outputName, "description");
String key = I18nUtil.stripConstantOr(defaultDescription,
() -> inferOutputKey(moduleTypeUID, outputName, "description"));
return i18nProvider.getText(bundle, key, defaultDescription, locale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.Locale;

import org.eclipse.smarthome.automation.template.RuleTemplate;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.eclipse.smarthome.core.i18n.I18nUtil;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.osgi.framework.Bundle;

/**
Expand All @@ -30,17 +30,16 @@ public class RuleTemplateI18nUtil {

public static final String RULE_TEMPLATE = "rule-template";

public static String getLocalizedRuleTemplateLabel(TranslationProvider i18nProvider, Bundle bundle, String ruleTemplateUID,
String defaultLabel, Locale locale) {
String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel)
: inferRuleTemplateKey(ruleTemplateUID, "label");
public static String getLocalizedRuleTemplateLabel(TranslationProvider i18nProvider, Bundle bundle,
String ruleTemplateUID, String defaultLabel, Locale locale) {
String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferRuleTemplateKey(ruleTemplateUID, "label"));
return i18nProvider.getText(bundle, key, defaultLabel, locale);
}

public static String getLocalizedRuleTemplateDescription(TranslationProvider i18nProvider, Bundle bundle,
String ruleTemplateUID, String defaultDescription, Locale locale) {
String key = I18nUtil.isConstant(defaultDescription) ? I18nUtil.stripConstant(defaultDescription)
: inferRuleTemplateKey(ruleTemplateUID, "description");
String key = I18nUtil.stripConstantOr(defaultDescription,
() -> inferRuleTemplateKey(ruleTemplateUID, "description"));
return i18nProvider.getText(bundle, key, defaultDescription, locale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.net.URI;
import java.util.Locale;

import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.eclipse.smarthome.core.i18n.I18nUtil;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.osgi.framework.Bundle;

/**
Expand All @@ -28,23 +28,22 @@
*/
public class ConfigDescriptionGroupI18nUtil {

private TranslationProvider i18nProvider;
private final TranslationProvider i18nProvider;

public ConfigDescriptionGroupI18nUtil(TranslationProvider i18nProvider) {
this.i18nProvider = i18nProvider;
}

public String getGroupDescription(Bundle bundle, URI configDescriptionURI, String groupName,
String defaultDescription, Locale locale) {
String key = I18nUtil.isConstant(defaultDescription) ? I18nUtil.stripConstant(defaultDescription) : inferKey(
configDescriptionURI, groupName, "description");
String key = I18nUtil.stripConstantOr(defaultDescription,
() -> inferKey(configDescriptionURI, groupName, "description"));
return i18nProvider.getText(bundle, key, defaultDescription, locale);
}

public String getGroupLabel(Bundle bundle, URI configDescriptionURI, String groupName, String defaultLabel,
Locale locale) {
String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel) : inferKey(
configDescriptionURI, groupName, "label");
String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferKey(configDescriptionURI, groupName, "label"));
return i18nProvider.getText(bundle, key, defaultLabel, locale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ public ConfigDescriptionI18nUtil(TranslationProvider i18nProvider) {

public String getParameterPattern(Bundle bundle, URI configDescriptionURI, String parameterName,
String defaultPattern, Locale locale) {
String key = I18nUtil.isConstant(defaultPattern) ? I18nUtil.stripConstant(defaultPattern)
: inferKey(configDescriptionURI, parameterName, "pattern");
String key = I18nUtil.stripConstantOr(defaultPattern,
() -> inferKey(configDescriptionURI, parameterName, "pattern"));
return i18nProvider.getText(bundle, key, defaultPattern, locale);
}

public String getParameterDescription(Bundle bundle, URI configDescriptionURI, String parameterName,
String defaultDescription, Locale locale) {
String key = I18nUtil.isConstant(defaultDescription) ? I18nUtil.stripConstant(defaultDescription)
: inferKey(configDescriptionURI, parameterName, "description");
String key = I18nUtil.stripConstantOr(defaultDescription,
() -> inferKey(configDescriptionURI, parameterName, "description"));
return i18nProvider.getText(bundle, key, defaultDescription, locale);
}

public String getParameterLabel(Bundle bundle, URI configDescriptionURI, String parameterName, String defaultLabel,
Locale locale) {
String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel)
: inferKey(configDescriptionURI, parameterName, "label");
String key = I18nUtil.stripConstantOr(defaultLabel,
() -> inferKey(configDescriptionURI, parameterName, "label"));
return i18nProvider.getText(bundle, key, defaultLabel, locale);
}

Expand All @@ -67,8 +67,8 @@ public String getParameterOptionLabel(Bundle bundle, URI configDescriptionURI, S
return defaultOptionLabel;
}

String key = I18nUtil.isConstant(defaultOptionLabel) ? I18nUtil.stripConstant(defaultOptionLabel)
: inferKey(configDescriptionURI, parameterName, "option." + optionValue);
String key = I18nUtil.stripConstantOr(defaultOptionLabel,
() -> inferKey(configDescriptionURI, parameterName, "option." + optionValue));

return i18nProvider.getText(bundle, key, defaultOptionLabel, locale);
}
Expand All @@ -81,8 +81,8 @@ public String getParameterUnitLabel(Bundle bundle, URI configDescriptionURI, Str
return label;
}
}
String key = I18nUtil.isConstant(defaultUnitLabel) ? I18nUtil.stripConstant(defaultUnitLabel)
: inferKey(configDescriptionURI, parameterName, "unitLabel");
String key = I18nUtil.stripConstantOr(defaultUnitLabel,
() -> inferKey(configDescriptionURI, parameterName, "unitLabel"));
return i18nProvider.getText(bundle, key, defaultUnitLabel, locale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,31 +256,33 @@ protected synchronized void stopScan() {
*
* @param discoveryResult Holds the information needed to identify the discovered device.
*/
protected void thingDiscovered(DiscoveryResult discoveryResult) {
protected void thingDiscovered(final DiscoveryResult discoveryResult) {
final DiscoveryResult discoveryResultNew;
if (this.i18nProvider != null && this.localeProvider != null) {
Bundle bundle = FrameworkUtil.getBundle(this.getClass());

String defaultLabel = discoveryResult.getLabel();

String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel)
: inferKey(discoveryResult, "label");
String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferKey(discoveryResult, "label"));

String label = this.i18nProvider.getText(bundle, key, defaultLabel, this.localeProvider.getLocale());

discoveryResult = new DiscoveryResultImpl(discoveryResult.getThingTypeUID(), discoveryResult.getThingUID(),
discoveryResult.getBridgeUID(), discoveryResult.getProperties(),
discoveryResultNew = new DiscoveryResultImpl(discoveryResult.getThingTypeUID(),
discoveryResult.getThingUID(), discoveryResult.getBridgeUID(), discoveryResult.getProperties(),
discoveryResult.getRepresentationProperty(), label, discoveryResult.getTimeToLive());
} else {
discoveryResultNew = discoveryResult;
}
for (DiscoveryListener discoveryListener : discoveryListeners) {
try {
discoveryListener.thingDiscovered(this, discoveryResult);
discoveryListener.thingDiscovered(this, discoveryResultNew);
} catch (Exception e) {
logger.error("An error occurred while calling the discovery listener {}.",
discoveryListener.getClass().getName(), e);
}
}
synchronized (cachedResults) {
cachedResults.put(discoveryResult.getThingUID(), discoveryResult);
cachedResults.put(discoveryResultNew.getThingUID(), discoveryResultNew);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TranslationProvider;
import org.eclipse.smarthome.core.thing.type.BridgeType;
import org.eclipse.smarthome.core.thing.type.ChannelDefinition;
Expand Down Expand Up @@ -65,7 +66,7 @@ protected void unsetChannelTypeRegistry(ChannelTypeRegistry channelTypeRegistry)
this.channelTypeRegistry = null;
}

public ThingType createLocalizedThingType(Bundle bundle, ThingType thingType, Locale locale) {
public ThingType createLocalizedThingType(Bundle bundle, ThingType thingType, @Nullable Locale locale) {
final String label = this.thingTypeI18nUtil.getLabel(bundle, thingType.getUID(), thingType.getLabel(), locale);
final String description = this.thingTypeI18nUtil.getDescription(bundle, thingType.getUID(),
thingType.getDescription(), locale);
Expand Down Expand Up @@ -122,8 +123,14 @@ public ThingType createLocalizedThingType(Bundle bundle, ThingType thingType, Lo
channelGroupDefinition.getTypeUID(), channelGroupLabel, channelGroupDescription));
}

ThingTypeBuilder builder = ThingTypeBuilder.instance(thingType).withLabel(label).withDescription(description)
.withChannelDefinitions(localizedChannelDefinitions)
ThingTypeBuilder builder = ThingTypeBuilder.instance(thingType);
if (label != null) {
builder.withLabel(label);
}
if (description != null) {
builder.withDescription(description);
}
builder.withChannelDefinitions(localizedChannelDefinitions)
.withChannelGroupDefinitions(localizedChannelGroupDefinitions);

if (thingType instanceof BridgeType) {
Expand Down
Loading