From a3835ebdc645f28b607f95c80d57998634e42bc2 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sat, 28 Nov 2020 01:11:06 +0100 Subject: [PATCH] [dwdunwetter] Removed dependency on org.apache.commons (#9138) * Removed dependency on org.apache.commons * Incorporated comments from review Signed-off-by: Christoph Weitkamp --- .../internal/config/DwdUnwetterConfiguration.java | 3 +-- .../dwdunwetter/internal/data/DwdWarningData.java | 6 ++---- .../internal/data/DwdWarningDataAccess.java | 12 +++++------- .../binding/dwdunwetter/internal/data/DwdXmlTag.java | 4 +--- .../binding/dwdunwetter/internal/data/Severity.java | 6 ++---- .../internal/data/SeverityComparator.java | 11 ++++++++--- .../binding/dwdunwetter/internal/data/Urgency.java | 6 ++---- .../binding/dwdunwetter/DwdUnwetterHandlerTest.java | 4 ++-- 8 files changed, 23 insertions(+), 29 deletions(-) diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/config/DwdUnwetterConfiguration.java b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/config/DwdUnwetterConfiguration.java index e553a7976..83535537c 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/config/DwdUnwetterConfiguration.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/config/DwdUnwetterConfiguration.java @@ -12,7 +12,6 @@ */ package org.openhab.binding.dwdunwetter.internal.config; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; /** @@ -24,5 +23,5 @@ import org.eclipse.jdt.annotation.NonNullByDefault; public class DwdUnwetterConfiguration { public int refresh; public int warningCount; - public String cellId = StringUtils.EMPTY; + public String cellId = ""; } diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdWarningData.java b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdWarningData.java index eb2afa255..95d537fe9 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdWarningData.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdWarningData.java @@ -15,8 +15,6 @@ package org.openhab.binding.dwdunwetter.internal.data; import java.math.BigDecimal; import java.time.Instant; -import org.apache.commons.lang.StringUtils; - /** * Data for one warning. * @@ -93,7 +91,7 @@ public class DwdWarningData { } public boolean isTest() { - return StringUtils.equalsIgnoreCase(status, "Test"); + return "Test".equalsIgnoreCase(status); } public void setMsgType(String msgType) { @@ -101,7 +99,7 @@ public class DwdWarningData { } public boolean isCancel() { - return StringUtils.equalsIgnoreCase(msgType, "Cancel"); + return "Cancel".equalsIgnoreCase(msgType); } public void setHeadline(String headline) { diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdWarningDataAccess.java b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdWarningDataAccess.java index d880537d7..ecf9419dd 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdWarningDataAccess.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdWarningDataAccess.java @@ -16,7 +16,6 @@ import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import org.apache.commons.lang.StringUtils; import org.openhab.core.io.net.http.HttpUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,23 +34,22 @@ public class DwdWarningDataAccess { /** * Returns the raw Data from the Endpoint. - * In case of errors or empty cellId value, returns an {@link StringUtils#EMPTY Empty String}. + * In case of errors or empty cellId value, returns an empty String. * * @param cellId The warnCell-Id for which the warnings should be returned * @return The raw data received or an empty string. */ public String getDataFromEndpoint(String cellId) { try { - if (StringUtils.isBlank(cellId)) { + if (cellId == null || cellId.isBlank()) { logger.warn("No cellId provided"); - return StringUtils.EMPTY; + return ""; } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(DWD_URL); stringBuilder.append("&CQL_FILTER="); - stringBuilder - .append(URLEncoder.encode("WARNCELLID LIKE '" + cellId + "'", StandardCharsets.UTF_8.toString())); + stringBuilder.append(URLEncoder.encode("WARNCELLID LIKE '" + cellId + "'", StandardCharsets.UTF_8)); logger.debug("Refreshing Data for cell {}", cellId); String rawData = HttpUtil.executeUrl("GET", stringBuilder.toString(), 5000); logger.trace("Raw request: {}", stringBuilder); @@ -63,6 +61,6 @@ public class DwdWarningDataAccess { logger.debug("Communication error trace", e); } - return StringUtils.EMPTY; + return ""; } } diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdXmlTag.java b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdXmlTag.java index 127ce0279..9fa92ebe1 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdXmlTag.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/DwdXmlTag.java @@ -14,8 +14,6 @@ package org.openhab.binding.dwdunwetter.internal.data; import java.util.Arrays; -import org.apache.commons.lang.StringUtils; - /** * The XML Tags to extract the relevant parts from the API response. * The names map directly to the XML Tags of the API Response. @@ -52,7 +50,7 @@ public enum DwdXmlTag { } public static DwdXmlTag getDwdXmlTag(String tag) { - return Arrays.asList(DwdXmlTag.values()).stream().filter(t -> StringUtils.equals(t.getTag(), tag)).findFirst() + return Arrays.asList(DwdXmlTag.values()).stream().filter(t -> tag.equals(t.getTag())).findFirst() .orElse(UNKNOWN); } } diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/Severity.java b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/Severity.java index 6fa32919f..61a688711 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/Severity.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/Severity.java @@ -14,8 +14,6 @@ package org.openhab.binding.dwdunwetter.internal.data; import java.util.Arrays; -import org.apache.commons.lang.StringUtils; - /** * Severity enum to make the severity comparable * @@ -46,7 +44,7 @@ public enum Severity { } public static Severity getSeverity(String input) { - return Arrays.asList(Severity.values()).stream() - .filter(sev -> StringUtils.equalsIgnoreCase(input, sev.getText())).findAny().orElse(UNKNOWN); + return Arrays.asList(Severity.values()).stream().filter(sev -> input.equalsIgnoreCase(sev.getText())).findAny() + .orElse(UNKNOWN); } } diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/SeverityComparator.java b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/SeverityComparator.java index 6efb21041..4c9854f1a 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/SeverityComparator.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/SeverityComparator.java @@ -14,8 +14,6 @@ package org.openhab.binding.dwdunwetter.internal.data; import java.util.Comparator; -import org.apache.commons.lang.ObjectUtils; - /** * Comperator to sort a Warning first by Severity, second by the onSet date. * @@ -30,7 +28,14 @@ public class SeverityComparator implements Comparator { int result = Integer.compare(o1.getSeverity().getOrder(), o2.getSeverity().getOrder()); if (result == 0) { - result = ObjectUtils.compare(o1.getOnset(), o2.getOnset()); + if (o1.getOnset() == o2.getOnset()) { + return 0; + } else if (o1.getOnset() == null) { + return -1; + } else if (o2.getOnset() == null) { + return 1; + } + return o1.getOnset().compareTo(o2.getOnset()); } return result; } diff --git a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/Urgency.java b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/Urgency.java index fbba85862..97200fe28 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/Urgency.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/main/java/org/openhab/binding/dwdunwetter/internal/data/Urgency.java @@ -14,8 +14,6 @@ package org.openhab.binding.dwdunwetter.internal.data; import java.util.Arrays; -import org.apache.commons.lang.StringUtils; - /** * Enum for the urgency of the warning. * @@ -38,7 +36,7 @@ public enum Urgency { } public static Urgency getUrgency(String input) { - return Arrays.asList(Urgency.values()).stream() - .filter(urg -> StringUtils.equalsIgnoreCase(input, urg.getText())).findAny().orElse(UNKNOWN); + return Arrays.asList(Urgency.values()).stream().filter(urg -> input.equalsIgnoreCase(urg.getText())).findAny() + .orElse(UNKNOWN); } } diff --git a/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/DwdUnwetterHandlerTest.java b/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/DwdUnwetterHandlerTest.java index f0a904556..7673057cc 100644 --- a/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/DwdUnwetterHandlerTest.java +++ b/bundles/org.openhab.binding.dwdunwetter/src/test/java/org/openhab/binding/dwdunwetter/DwdUnwetterHandlerTest.java @@ -19,11 +19,11 @@ import static org.mockito.Mockito.*; import java.io.InputStream; import java.util.List; +import java.util.Objects; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import org.apache.commons.lang.StringUtils; import org.hamcrest.CoreMatchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -122,7 +122,7 @@ public class DwdUnwetterHandlerTest extends JavaTest { if (nodeId == null) { continue; } - if (StringUtils.equals(nodeId.getTextContent(), uuid.getId())) { + if (Objects.equals(nodeId.getTextContent(), uuid.getId())) { return getLabel(node.getChildNodes()); } }