[transform] Code optimization for Java17: instanceof matching and multiline strings (#15483)

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich 2023-08-24 06:41:36 +02:00 committed by GitHub
parent db651f9e92
commit 6e49c6e6ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 111 additions and 107 deletions

View File

@ -161,41 +161,40 @@ public class Bin2Json {
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 @@ public class Bin2Json {
}
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 @@ public class Bin2Json {
} 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));
}

View File

@ -64,9 +64,9 @@ public class ExecTransformationProfile implements StateProfile {
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);

View File

@ -65,9 +65,9 @@ public class JinjaTransformationProfile implements StateProfile {
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);

View File

@ -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();
}

View File

@ -66,9 +66,9 @@ public class JSonPathTransformationProfile implements StateProfile {
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);

View File

@ -64,9 +64,9 @@ public class MapTransformationProfile implements StateProfile {
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);

View File

@ -65,9 +65,9 @@ public class RegexTransformationProfile implements StateProfile {
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);

View File

@ -17,19 +17,21 @@ package org.openhab.transform.regex.internal;
*/
public abstract class AbstractTransformationServiceTest {
protected String source = "<?xml version=\"1.0\"?><xml_api_reply version=\"1\"><weather module_id=\"0\""
+ " tab_id=\"0\" mobile_row=\"0\" mobile_zipped=\"1\" row=\"0\" section=\"0\" ><forecast_information>"
+ "<city data=\"Krefeld, North Rhine-Westphalia\"/><postal_code data=\"Krefeld Germany\"/>"
+ "<latitude_e6 data=\"\"/><longitude_e6 data=\"\"/><forecast_date data=\"2011-03-01\"/>"
+ "<current_date_time data=\"2011-03-01 15:20:00 +0000\"/><unit_system data=\"SI\"/></forecast_information>"
+ "<current_conditions><condition data=\"Meistens bew<65>lkt\"/><temp_f data=\"46\"/><temp_c data=\"8\"/>"
+ "<humidity data=\"Feuchtigkeit: 66 %\"/><icon data=\"/ig/images/weather/mostly_cloudy.gif\"/>"
+ "<wind_condition data=\"Wind: N mit 26 km/h\"/></current_conditions><forecast_conditions><day_of_week data=\"Di.\"/>"
+ "<low data=\"-1\"/><high data=\"6\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/>"
+ "</forecast_conditions><forecast_conditions><day_of_week data=\"Mi.\"/><low data=\"-1\"/><high data=\"8\"/>"
+ "<icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/></forecast_conditions><forecast_conditions>"
+ "<day_of_week data=\"Do.\"/><low data=\"-1\"/><high data=\"8\"/><icon data=\"/ig/images/weather/sunny.gif\"/>"
+ "<condition data=\"Klar\"/></forecast_conditions><forecast_conditions><day_of_week data=\"Fr.\"/><low data=\"0\"/>"
+ "<high data=\"8\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/></forecast_conditions>"
+ "</weather></xml_api_reply>";
protected String source = """
<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0"\
tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information>\
<city data="Krefeld, North Rhine-Westphalia"/><postal_code data="Krefeld Germany"/>\
<latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2011-03-01"/>\
<current_date_time data="2011-03-01 15:20:00 +0000"/><unit_system data="SI"/></forecast_information>\
<current_conditions><condition data="Meistens bew<65>lkt"/><temp_f data="46"/><temp_c data="8"/>\
<humidity data="Feuchtigkeit: 66 %"/><icon data="/ig/images/weather/mostly_cloudy.gif"/>\
<wind_condition data="Wind: N mit 26 km/h"/></current_conditions><forecast_conditions><day_of_week data="Di."/>\
<low data="-1"/><high data="6"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/>\
</forecast_conditions><forecast_conditions><day_of_week data="Mi."/><low data="-1"/><high data="8"/>\
<icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/></forecast_conditions><forecast_conditions>\
<day_of_week data="Do."/><low data="-1"/><high data="8"/><icon data="/ig/images/weather/sunny.gif"/>\
<condition data="Klar"/></forecast_conditions><forecast_conditions><day_of_week data="Fr."/><low data="0"/>\
<high data="8"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/></forecast_conditions>\
</weather></xml_api_reply>\
""";
}

View File

@ -64,9 +64,9 @@ public class ScaleTransformationProfile implements StateProfile {
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);

View File

@ -65,9 +65,9 @@ public class XPathTransformationProfile implements StateProfile {
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);

View File

@ -17,19 +17,21 @@ package org.openhab.transform.xpath.internal;
*/
public abstract class AbstractTransformationServiceTest {
protected String source = "<?xml version=\"1.0\"?><xml_api_reply version=\"1\"><weather module_id=\"0\""
+ " tab_id=\"0\" mobile_row=\"0\" mobile_zipped=\"1\" row=\"0\" section=\"0\" ><forecast_information>"
+ "<city data=\"Krefeld, North Rhine-Westphalia\"/><postal_code data=\"Krefeld Germany\"/>"
+ "<latitude_e6 data=\"\"/><longitude_e6 data=\"\"/><forecast_date data=\"2011-03-01\"/>"
+ "<current_date_time data=\"2011-03-01 15:20:00 +0000\"/><unit_system data=\"SI\"/></forecast_information>"
+ "<current_conditions><condition data=\"Meistens bew<65>lkt\"/><temp_f data=\"46\"/><temp_c data=\"8\"/>"
+ "<humidity data=\"Feuchtigkeit: 66 %\"/><icon data=\"/ig/images/weather/mostly_cloudy.gif\"/>"
+ "<wind_condition data=\"Wind: N mit 26 km/h\"/></current_conditions><forecast_conditions><day_of_week data=\"Di.\"/>"
+ "<low data=\"-1\"/><high data=\"6\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/>"
+ "</forecast_conditions><forecast_conditions><day_of_week data=\"Mi.\"/><low data=\"-1\"/><high data=\"8\"/>"
+ "<icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/></forecast_conditions><forecast_conditions>"
+ "<day_of_week data=\"Do.\"/><low data=\"-1\"/><high data=\"8\"/><icon data=\"/ig/images/weather/sunny.gif\"/>"
+ "<condition data=\"Klar\"/></forecast_conditions><forecast_conditions><day_of_week data=\"Fr.\"/><low data=\"0\"/>"
+ "<high data=\"8\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/></forecast_conditions>"
+ "</weather></xml_api_reply>";
protected String source = """
<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0"\
tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information>\
<city data="Krefeld, North Rhine-Westphalia"/><postal_code data="Krefeld Germany"/>\
<latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2011-03-01"/>\
<current_date_time data="2011-03-01 15:20:00 +0000"/><unit_system data="SI"/></forecast_information>\
<current_conditions><condition data="Meistens bew<65>lkt"/><temp_f data="46"/><temp_c data="8"/>\
<humidity data="Feuchtigkeit: 66 %"/><icon data="/ig/images/weather/mostly_cloudy.gif"/>\
<wind_condition data="Wind: N mit 26 km/h"/></current_conditions><forecast_conditions><day_of_week data="Di."/>\
<low data="-1"/><high data="6"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/>\
</forecast_conditions><forecast_conditions><day_of_week data="Mi."/><low data="-1"/><high data="8"/>\
<icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/></forecast_conditions><forecast_conditions>\
<day_of_week data="Do."/><low data="-1"/><high data="8"/><icon data="/ig/images/weather/sunny.gif"/>\
<condition data="Klar"/></forecast_conditions><forecast_conditions><day_of_week data="Fr."/><low data="0"/>\
<high data="8"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/></forecast_conditions>\
</weather></xml_api_reply>\
""";
}

View File

@ -65,9 +65,9 @@ public class XSLTTransformationProfile implements StateProfile {
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);

View File

@ -17,19 +17,21 @@ package org.openhab.transform.xslt.internal;
*/
public abstract class AbstractTransformationServiceTest {
protected String source = "<?xml version=\"1.0\"?><xml_api_reply version=\"1\"><weather module_id=\"0\""
+ " tab_id=\"0\" mobile_row=\"0\" mobile_zipped=\"1\" row=\"0\" section=\"0\" ><forecast_information>"
+ "<city data=\"Krefeld, North Rhine-Westphalia\"/><postal_code data=\"Krefeld Germany\"/>"
+ "<latitude_e6 data=\"\"/><longitude_e6 data=\"\"/><forecast_date data=\"2011-03-01\"/>"
+ "<current_date_time data=\"2011-03-01 15:20:00 +0000\"/><unit_system data=\"SI\"/></forecast_information>"
+ "<current_conditions><condition data=\"Meistens bew<65>lkt\"/><temp_f data=\"46\"/><temp_c data=\"8\"/>"
+ "<humidity data=\"Feuchtigkeit: 66 %\"/><icon data=\"/ig/images/weather/mostly_cloudy.gif\"/>"
+ "<wind_condition data=\"Wind: N mit 26 km/h\"/></current_conditions><forecast_conditions><day_of_week data=\"Di.\"/>"
+ "<low data=\"-1\"/><high data=\"6\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/>"
+ "</forecast_conditions><forecast_conditions><day_of_week data=\"Mi.\"/><low data=\"-1\"/><high data=\"8\"/>"
+ "<icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/></forecast_conditions><forecast_conditions>"
+ "<day_of_week data=\"Do.\"/><low data=\"-1\"/><high data=\"8\"/><icon data=\"/ig/images/weather/sunny.gif\"/>"
+ "<condition data=\"Klar\"/></forecast_conditions><forecast_conditions><day_of_week data=\"Fr.\"/><low data=\"0\"/>"
+ "<high data=\"8\"/><icon data=\"/ig/images/weather/sunny.gif\"/><condition data=\"Klar\"/></forecast_conditions>"
+ "</weather></xml_api_reply>";
protected String source = """
<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0"\
tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information>\
<city data="Krefeld, North Rhine-Westphalia"/><postal_code data="Krefeld Germany"/>\
<latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2011-03-01"/>\
<current_date_time data="2011-03-01 15:20:00 +0000"/><unit_system data="SI"/></forecast_information>\
<current_conditions><condition data="Meistens bew<65>lkt"/><temp_f data="46"/><temp_c data="8"/>\
<humidity data="Feuchtigkeit: 66 %"/><icon data="/ig/images/weather/mostly_cloudy.gif"/>\
<wind_condition data="Wind: N mit 26 km/h"/></current_conditions><forecast_conditions><day_of_week data="Di."/>\
<low data="-1"/><high data="6"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/>\
</forecast_conditions><forecast_conditions><day_of_week data="Mi."/><low data="-1"/><high data="8"/>\
<icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/></forecast_conditions><forecast_conditions>\
<day_of_week data="Do."/><low data="-1"/><high data="8"/><icon data="/ig/images/weather/sunny.gif"/>\
<condition data="Klar"/></forecast_conditions><forecast_conditions><day_of_week data="Fr."/><low data="0"/>\
<high data="8"/><icon data="/ig/images/weather/sunny.gif"/><condition data="Klar"/></forecast_conditions>\
</weather></xml_api_reply>\
""";
}