Skip to content

Commit

Permalink
[transform] Code optimization for Java17: instanceof matching and mul…
Browse files Browse the repository at this point in the history
…tiline strings

Signed-off-by: Holger Friedrich <[email protected]>
  • Loading branch information
holgerfriedrich committed Aug 23, 2023
1 parent 5f31eeb commit 9807d4f
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,62 +161,60 @@ 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 {
throw new ConversionException(String.format("Unexpected field type '%s'", field));
}
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);
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
*/
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�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�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>\
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
*/
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�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�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>\
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 9807d4f

Please sign in to comment.