From 59751f5b8d0d29f1cf9fa47181cb0fd044a48882 Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Mon, 21 Aug 2023 08:25:33 +0200 Subject: [PATCH] [transform] Code optimization for Java17: instanceof matching and multiline strings Signed-off-by: Holger Friedrich --- .../transform/bin2json/internal/Bin2Json.java | 70 +++++++++---------- .../profiles/ExecTransformationProfile.java | 6 +- .../profiles/JinjaTransformationProfile.java | 6 +- .../JSonPathTransformationService.java | 4 +- .../JSonPathTransformationProfile.java | 6 +- .../profiles/MapTransformationProfile.java | 6 +- .../profiles/RegexTransformationProfile.java | 6 +- .../AbstractTransformationServiceTest.java | 32 +++++---- .../profiles/ScaleTransformationProfile.java | 6 +- .../profiles/XPathTransformationProfile.java | 6 +- .../AbstractTransformationServiceTest.java | 32 +++++---- .../profiles/XSLTTransformationProfile.java | 6 +- .../AbstractTransformationServiceTest.java | 32 +++++---- 13 files changed, 111 insertions(+), 107 deletions(-) diff --git a/bundles/org.openhab.transform.bin2json/src/main/java/org/openhab/transform/bin2json/internal/Bin2Json.java b/bundles/org.openhab.transform.bin2json/src/main/java/org/openhab/transform/bin2json/internal/Bin2Json.java index a884f8367dd46..3ccb76a832006 100644 --- a/bundles/org.openhab.transform.bin2json/src/main/java/org/openhab/transform/bin2json/internal/Bin2Json.java +++ b/bundles/org.openhab.transform.bin2json/src/main/java/org/openhab/transform/bin2json/internal/Bin2Json.java @@ -161,41 +161,40 @@ private JsonObject convertToJSon(final JsonObject json, final JBBPAbstractField final String fieldName = field.getFieldName() == null ? "nonamed" : field.getFieldName(); if (field instanceof JBBPAbstractArrayField) { final JsonArray jsonArray = new JsonArray(); - if (field instanceof JBBPFieldArrayBit) { - for (final byte b : ((JBBPFieldArrayBit) field).getArray()) { + if (field instanceof JBBPFieldArrayBit bit) { + for (final byte b : bit.getArray()) { jsonArray.add(new JsonPrimitive(b)); } - } else if (field instanceof JBBPFieldArrayBoolean) { - for (final boolean b : ((JBBPFieldArrayBoolean) field).getArray()) { + } else if (field instanceof JBBPFieldArrayBoolean boolean1) { + for (final boolean b : boolean1.getArray()) { jsonArray.add(new JsonPrimitive(b)); } - } else if (field instanceof JBBPFieldArrayByte) { - for (final byte b : ((JBBPFieldArrayByte) field).getArray()) { + } else if (field instanceof JBBPFieldArrayByte byte1) { + for (final byte b : byte1.getArray()) { jsonArray.add(new JsonPrimitive(b)); } - } else if (field instanceof JBBPFieldArrayInt) { - for (final int b : ((JBBPFieldArrayInt) field).getArray()) { + } else if (field instanceof JBBPFieldArrayInt int1) { + for (final int b : int1.getArray()) { jsonArray.add(new JsonPrimitive(b)); } - } else if (field instanceof JBBPFieldArrayLong) { - for (final long b : ((JBBPFieldArrayLong) field).getArray()) { + } else if (field instanceof JBBPFieldArrayLong long1) { + for (final long b : long1.getArray()) { jsonArray.add(new JsonPrimitive(b)); } - } else if (field instanceof JBBPFieldArrayShort) { - for (final short b : ((JBBPFieldArrayShort) field).getArray()) { + } else if (field instanceof JBBPFieldArrayShort short1) { + for (final short b : short1.getArray()) { jsonArray.add(new JsonPrimitive(b)); } - } else if (field instanceof JBBPFieldArrayStruct) { - final JBBPFieldArrayStruct array = (JBBPFieldArrayStruct) field; + } else if (field instanceof JBBPFieldArrayStruct array) { for (int i = 0; i < array.size(); i++) { jsonArray.add(convertToJSon(new JsonObject(), array.getElementAt(i))); } - } else if (field instanceof JBBPFieldArrayUByte) { - for (final byte b : ((JBBPFieldArrayUByte) field).getArray()) { + } else if (field instanceof JBBPFieldArrayUByte byte1) { + for (final byte b : byte1.getArray()) { jsonArray.add(new JsonPrimitive(b & 0xFF)); } - } else if (field instanceof JBBPFieldArrayUShort) { - for (final short b : ((JBBPFieldArrayUShort) field).getArray()) { + } else if (field instanceof JBBPFieldArrayUShort short1) { + for (final short b : short1.getArray()) { jsonArray.add(new JsonPrimitive(b & 0xFFFF)); } } else { @@ -203,20 +202,19 @@ private JsonObject convertToJSon(final JsonObject json, final JBBPAbstractField } jsn.add(fieldName, jsonArray); } else { - if (field instanceof JBBPFieldBit) { - jsn.addProperty(fieldName, ((JBBPFieldBit) field).getAsInt()); - } else if (field instanceof JBBPFieldBoolean) { - jsn.addProperty(fieldName, ((JBBPFieldBoolean) field).getAsBool()); - } else if (field instanceof JBBPFieldByte) { - jsn.addProperty(fieldName, ((JBBPFieldByte) field).getAsInt()); - } else if (field instanceof JBBPFieldInt) { - jsn.addProperty(fieldName, ((JBBPFieldInt) field).getAsInt()); - } else if (field instanceof JBBPFieldLong) { - jsn.addProperty(fieldName, ((JBBPFieldLong) field).getAsLong()); - } else if (field instanceof JBBPFieldShort) { - jsn.addProperty(fieldName, ((JBBPFieldShort) field).getAsInt()); - } else if (field instanceof JBBPFieldStruct) { - final JBBPFieldStruct struct = (JBBPFieldStruct) field; + if (field instanceof JBBPFieldBit bit) { + jsn.addProperty(fieldName, bit.getAsInt()); + } else if (field instanceof JBBPFieldBoolean boolean1) { + jsn.addProperty(fieldName, boolean1.getAsBool()); + } else if (field instanceof JBBPFieldByte byte1) { + jsn.addProperty(fieldName, byte1.getAsInt()); + } else if (field instanceof JBBPFieldInt int1) { + jsn.addProperty(fieldName, int1.getAsInt()); + } else if (field instanceof JBBPFieldLong long1) { + jsn.addProperty(fieldName, long1.getAsLong()); + } else if (field instanceof JBBPFieldShort short1) { + jsn.addProperty(fieldName, short1.getAsInt()); + } else if (field instanceof JBBPFieldStruct struct) { final JsonObject obj = new JsonObject(); for (final JBBPAbstractField f : struct.getArray()) { convertToJSon(obj, f); @@ -226,10 +224,10 @@ private JsonObject convertToJSon(final JsonObject json, final JBBPAbstractField } else { jsn.add(fieldName, obj); } - } else if (field instanceof JBBPFieldUByte) { - jsn.addProperty(fieldName, ((JBBPFieldUByte) field).getAsInt()); - } else if (field instanceof JBBPFieldUShort) { - jsn.addProperty(fieldName, ((JBBPFieldUShort) field).getAsInt()); + } else if (field instanceof JBBPFieldUByte byte1) { + jsn.addProperty(fieldName, byte1.getAsInt()); + } else if (field instanceof JBBPFieldUShort short1) { + jsn.addProperty(fieldName, short1.getAsInt()); } else { throw new ConversionException(String.format("Unexpected field '%s'", field)); } diff --git a/bundles/org.openhab.transform.exec/src/main/java/org/openhab/transform/exec/internal/profiles/ExecTransformationProfile.java b/bundles/org.openhab.transform.exec/src/main/java/org/openhab/transform/exec/internal/profiles/ExecTransformationProfile.java index 92ac8d60c1359..ae42266f54fa2 100644 --- a/bundles/org.openhab.transform.exec/src/main/java/org/openhab/transform/exec/internal/profiles/ExecTransformationProfile.java +++ b/bundles/org.openhab.transform.exec/src/main/java/org/openhab/transform/exec/internal/profiles/ExecTransformationProfile.java @@ -64,9 +64,9 @@ public ExecTransformationProfile(ProfileCallback callback, ProfileContext contex if (paramSource == null) { paramSource = "%s"; } - if (paramFunction instanceof String && paramSource instanceof String) { - function = (String) paramFunction; - sourceFormat = (String) paramSource; + if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) { + function = pFunction; + sourceFormat = pFormat; } else { logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM, SOURCE_FORMAT_PARAM); diff --git a/bundles/org.openhab.transform.jinja/src/main/java/org/openhab/transform/jinja/internal/profiles/JinjaTransformationProfile.java b/bundles/org.openhab.transform.jinja/src/main/java/org/openhab/transform/jinja/internal/profiles/JinjaTransformationProfile.java index c8f4cdc3c7bf2..b6f4618e003db 100644 --- a/bundles/org.openhab.transform.jinja/src/main/java/org/openhab/transform/jinja/internal/profiles/JinjaTransformationProfile.java +++ b/bundles/org.openhab.transform.jinja/src/main/java/org/openhab/transform/jinja/internal/profiles/JinjaTransformationProfile.java @@ -65,9 +65,9 @@ public JinjaTransformationProfile(ProfileCallback callback, ProfileContext conte if (paramSource == null) { paramSource = "%s"; } - if (paramFunction instanceof String && paramSource instanceof String) { - function = (String) paramFunction; - sourceFormat = (String) paramSource; + if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) { + function = pFunction; + sourceFormat = pFormat; } else { logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM, SOURCE_FORMAT_PARAM); diff --git a/bundles/org.openhab.transform.jsonpath/src/main/java/org/openhab/transform/jsonpath/internal/JSonPathTransformationService.java b/bundles/org.openhab.transform.jsonpath/src/main/java/org/openhab/transform/jsonpath/internal/JSonPathTransformationService.java index a38bf65032464..a54896a506c84 100644 --- a/bundles/org.openhab.transform.jsonpath/src/main/java/org/openhab/transform/jsonpath/internal/JSonPathTransformationService.java +++ b/bundles/org.openhab.transform.jsonpath/src/main/java/org/openhab/transform/jsonpath/internal/JSonPathTransformationService.java @@ -68,8 +68,8 @@ public class JSonPathTransformationService implements TransformationService { logger.debug("transformation resulted in '{}'", transformationResult); if (transformationResult == null) { return null; - } else if (transformationResult instanceof List) { - return flattenList((List) transformationResult); + } else if (transformationResult instanceof List list) { + return flattenList(list); } else { return transformationResult.toString(); } diff --git a/bundles/org.openhab.transform.jsonpath/src/main/java/org/openhab/transform/jsonpath/internal/profiles/JSonPathTransformationProfile.java b/bundles/org.openhab.transform.jsonpath/src/main/java/org/openhab/transform/jsonpath/internal/profiles/JSonPathTransformationProfile.java index b343e352e1039..a02a6e8ae41a1 100644 --- a/bundles/org.openhab.transform.jsonpath/src/main/java/org/openhab/transform/jsonpath/internal/profiles/JSonPathTransformationProfile.java +++ b/bundles/org.openhab.transform.jsonpath/src/main/java/org/openhab/transform/jsonpath/internal/profiles/JSonPathTransformationProfile.java @@ -66,9 +66,9 @@ public JSonPathTransformationProfile(ProfileCallback callback, ProfileContext co if (paramSource == null) { paramSource = "%s"; } - if (paramFunction instanceof String && paramSource instanceof String) { - function = (String) paramFunction; - sourceFormat = (String) paramSource; + if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) { + function = pFunction; + sourceFormat = pFormat; } else { logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM, SOURCE_FORMAT_PARAM); diff --git a/bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/profiles/MapTransformationProfile.java b/bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/profiles/MapTransformationProfile.java index f57abec1db3a8..d2a15afc28598 100644 --- a/bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/profiles/MapTransformationProfile.java +++ b/bundles/org.openhab.transform.map/src/main/java/org/openhab/transform/map/internal/profiles/MapTransformationProfile.java @@ -64,9 +64,9 @@ public MapTransformationProfile(ProfileCallback callback, ProfileContext context if (paramSource == null) { paramSource = "%s"; } - if (paramFunction instanceof String && paramSource instanceof String) { - function = (String) paramFunction; - sourceFormat = (String) paramSource; + if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) { + function = pFunction; + sourceFormat = pFormat; } else { logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM, SOURCE_FORMAT_PARAM); diff --git a/bundles/org.openhab.transform.regex/src/main/java/org/openhab/transform/regex/internal/profiles/RegexTransformationProfile.java b/bundles/org.openhab.transform.regex/src/main/java/org/openhab/transform/regex/internal/profiles/RegexTransformationProfile.java index 3f3693c56b515..e32567940119b 100644 --- a/bundles/org.openhab.transform.regex/src/main/java/org/openhab/transform/regex/internal/profiles/RegexTransformationProfile.java +++ b/bundles/org.openhab.transform.regex/src/main/java/org/openhab/transform/regex/internal/profiles/RegexTransformationProfile.java @@ -65,9 +65,9 @@ public RegexTransformationProfile(ProfileCallback callback, ProfileContext conte if (paramSource == null) { paramSource = "%s"; } - if (paramFunction instanceof String && paramSource instanceof String) { - function = (String) paramFunction; - sourceFormat = (String) paramSource; + if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) { + function = pFunction; + sourceFormat = pFormat; } else { logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM, SOURCE_FORMAT_PARAM); diff --git a/bundles/org.openhab.transform.regex/src/test/java/org/openhab/transform/regex/internal/AbstractTransformationServiceTest.java b/bundles/org.openhab.transform.regex/src/test/java/org/openhab/transform/regex/internal/AbstractTransformationServiceTest.java index 222e0d550817b..f1c9c82169649 100644 --- a/bundles/org.openhab.transform.regex/src/test/java/org/openhab/transform/regex/internal/AbstractTransformationServiceTest.java +++ b/bundles/org.openhab.transform.regex/src/test/java/org/openhab/transform/regex/internal/AbstractTransformationServiceTest.java @@ -17,19 +17,21 @@ */ public abstract class AbstractTransformationServiceTest { - protected String source = "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + ""; + protected String source = """ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + """; } diff --git a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java index fcce627e81b79..1499f1e2b5c00 100644 --- a/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java +++ b/bundles/org.openhab.transform.scale/src/main/java/org/openhab/transform/scale/internal/profiles/ScaleTransformationProfile.java @@ -64,9 +64,9 @@ public ScaleTransformationProfile(ProfileCallback callback, ProfileContext conte if (paramSource == null) { paramSource = "%s"; } - if (paramFunction instanceof String && paramSource instanceof String) { - function = (String) paramFunction; - sourceFormat = (String) paramSource; + if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) { + function = pFunction; + sourceFormat = pFormat; } else { logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM, SOURCE_FORMAT_PARAM); diff --git a/bundles/org.openhab.transform.xpath/src/main/java/org/openhab/transform/xpath/internal/profiles/XPathTransformationProfile.java b/bundles/org.openhab.transform.xpath/src/main/java/org/openhab/transform/xpath/internal/profiles/XPathTransformationProfile.java index 9661b36b4cf45..97e498c428503 100644 --- a/bundles/org.openhab.transform.xpath/src/main/java/org/openhab/transform/xpath/internal/profiles/XPathTransformationProfile.java +++ b/bundles/org.openhab.transform.xpath/src/main/java/org/openhab/transform/xpath/internal/profiles/XPathTransformationProfile.java @@ -65,9 +65,9 @@ public XPathTransformationProfile(ProfileCallback callback, ProfileContext conte if (paramSource == null) { paramSource = "%s"; } - if (paramFunction instanceof String && paramSource instanceof String) { - function = (String) paramFunction; - sourceFormat = (String) paramSource; + if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) { + function = pFunction; + sourceFormat = pFormat; } else { logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM, SOURCE_FORMAT_PARAM); diff --git a/bundles/org.openhab.transform.xpath/src/test/java/org/openhab/transform/xpath/internal/AbstractTransformationServiceTest.java b/bundles/org.openhab.transform.xpath/src/test/java/org/openhab/transform/xpath/internal/AbstractTransformationServiceTest.java index 531ede33b867e..0ee067cee49a0 100644 --- a/bundles/org.openhab.transform.xpath/src/test/java/org/openhab/transform/xpath/internal/AbstractTransformationServiceTest.java +++ b/bundles/org.openhab.transform.xpath/src/test/java/org/openhab/transform/xpath/internal/AbstractTransformationServiceTest.java @@ -17,19 +17,21 @@ */ public abstract class AbstractTransformationServiceTest { - protected String source = "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + ""; + protected String source = """ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + """; } diff --git a/bundles/org.openhab.transform.xslt/src/main/java/org/openhab/transform/xslt/internal/profiles/XSLTTransformationProfile.java b/bundles/org.openhab.transform.xslt/src/main/java/org/openhab/transform/xslt/internal/profiles/XSLTTransformationProfile.java index b585b99b72a1f..73c388d99674e 100644 --- a/bundles/org.openhab.transform.xslt/src/main/java/org/openhab/transform/xslt/internal/profiles/XSLTTransformationProfile.java +++ b/bundles/org.openhab.transform.xslt/src/main/java/org/openhab/transform/xslt/internal/profiles/XSLTTransformationProfile.java @@ -65,9 +65,9 @@ public XSLTTransformationProfile(ProfileCallback callback, ProfileContext contex if (paramSource == null) { paramSource = "%s"; } - if (paramFunction instanceof String && paramSource instanceof String) { - function = (String) paramFunction; - sourceFormat = (String) paramSource; + if (paramFunction instanceof String pFunction && paramSource instanceof String pFormat) { + function = pFunction; + sourceFormat = pFormat; } else { logger.warn("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM, SOURCE_FORMAT_PARAM); diff --git a/bundles/org.openhab.transform.xslt/src/test/java/org/openhab/transform/xslt/internal/AbstractTransformationServiceTest.java b/bundles/org.openhab.transform.xslt/src/test/java/org/openhab/transform/xslt/internal/AbstractTransformationServiceTest.java index 8edb7e070c2a8..d6bbe238d7d98 100644 --- a/bundles/org.openhab.transform.xslt/src/test/java/org/openhab/transform/xslt/internal/AbstractTransformationServiceTest.java +++ b/bundles/org.openhab.transform.xslt/src/test/java/org/openhab/transform/xslt/internal/AbstractTransformationServiceTest.java @@ -17,19 +17,21 @@ */ public abstract class AbstractTransformationServiceTest { - protected String source = "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + ""; + protected String source = """ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + """; }