From 2ed511066868e1dd32145870f05c50b413375d78 Mon Sep 17 00:00:00 2001 From: Markus Rathgeb Date: Thu, 20 Jun 2019 21:33:36 +0200 Subject: [PATCH] slightly cleaner nullness handling (don't split contains and get) (#885) Signed-off-by: Markus Rathgeb --- .../internal/StateDescriptionConverter.java | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/bundles/org.openhab.core.thing.xml/src/main/java/org/eclipse/smarthome/core/thing/xml/internal/StateDescriptionConverter.java b/bundles/org.openhab.core.thing.xml/src/main/java/org/eclipse/smarthome/core/thing/xml/internal/StateDescriptionConverter.java index b1021d0b41..f174e3ad21 100644 --- a/bundles/org.openhab.core.thing.xml/src/main/java/org/eclipse/smarthome/core/thing/xml/internal/StateDescriptionConverter.java +++ b/bundles/org.openhab.core.thing.xml/src/main/java/org/eclipse/smarthome/core/thing/xml/internal/StateDescriptionConverter.java @@ -16,6 +16,7 @@ import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; import org.eclipse.smarthome.config.xml.util.ConverterAttributeMapValidator; import org.eclipse.smarthome.config.xml.util.GenericUnmarshaller; @@ -92,19 +93,10 @@ public class StateDescriptionConverter extends GenericUnmarshaller attributes = nodeValue.getAttributes(); - if ((attributes != null) && (attributes.containsKey("value"))) { - value = attributes.get("value"); - } else { - throw new ConversionException("The node 'option' requires the attribute 'value'!"); - } - - label = (String) nodeValue.getValue(); - - return new StateOption(value, label); + final Map attributes = nodeValue.getAttributes(); + final String value = Optional.ofNullable(attributes).map(entry -> entry.get("value")) + .orElseThrow(() -> new ConversionException("The node 'option' requires the attribute 'value'!")); + return new StateOption(value, nodeValue.getValue().toString()); } throw new ConversionException("Unknown type in the list of 'options'!");