Skip to content

Commit

Permalink
dogfooding - Convert String concatenation to Text Block
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Aug 3, 2024
1 parent 6477f23 commit 7977acf
Show file tree
Hide file tree
Showing 36 changed files with 462 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,19 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.BOOLEAN_VALUE_RATHER_THAN_COMPARISON)) {
return "" //$NON-NLS-1$
+ "boolean booleanValue = isValid;\n" //$NON-NLS-1$
+ "boolean booleanValue2 = !isValid;\n" //$NON-NLS-1$
+ "boolean booleanValue3 = i <= 0;\n" //$NON-NLS-1$
+ "boolean booleanValue4 = !booleanObject;\n"; //$NON-NLS-1$
return """
boolean booleanValue = isValid;
boolean booleanValue2 = !isValid;
boolean booleanValue3 = i <= 0;
boolean booleanValue4 = !booleanObject;
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "boolean booleanValue = isValid == true;\n" //$NON-NLS-1$
+ "boolean booleanValue2 = isValid == false;\n" //$NON-NLS-1$
+ "boolean booleanValue3 = Boolean.FALSE.equals(i > 0);\n" //$NON-NLS-1$
+ "boolean booleanValue4 = booleanObject.equals(Boolean.FALSE);\n"; //$NON-NLS-1$
return """
boolean booleanValue = isValid == true;
boolean booleanValue2 = isValid == false;
boolean booleanValue3 = Boolean.FALSE.equals(i > 0);
boolean booleanValue4 = booleanObject.equals(Boolean.FALSE);
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,26 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.ELSE_IF)) {
return "" //$NON-NLS-1$
+ "if (isValid) {\n" //$NON-NLS-1$
+ " System.out.println(isValid);\n" //$NON-NLS-1$
+ "} else if (isEnabled) {\n" //$NON-NLS-1$
+ " System.out.println(isEnabled);\n" //$NON-NLS-1$
+ "}\n\n\n"; //$NON-NLS-1$
return """
if (isValid) {
System.out.println(isValid);
} else if (isEnabled) {
System.out.println(isEnabled);
}
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "if (isValid) {\n" //$NON-NLS-1$
+ " System.out.println(isValid);\n" //$NON-NLS-1$
+ "} else {\n" //$NON-NLS-1$
+ " if (isEnabled) {\n" //$NON-NLS-1$
+ " System.out.println(isEnabled);\n" //$NON-NLS-1$
+ " }\n" //$NON-NLS-1$
+ "}\n"; //$NON-NLS-1$
return """
if (isValid) {
System.out.println(isValid);
} else {
if (isEnabled) {
System.out.println(isEnabled);
}
}
"""; //$NON-NLS-1$
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,19 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.INVERT_EQUALS)) {
return "" //$NON-NLS-1$
+ "boolean result = \"foo\".equals(text);\n" //$NON-NLS-1$
+ "boolean result2 = (text1 + text2).equals(text);\n" //$NON-NLS-1$
+ "boolean result3 = DayOfWeek.MONDAY.equals(object);\n" //$NON-NLS-1$
+ "boolean result4 = \"foo\".equalsIgnoreCase(text);\n"; //$NON-NLS-1$
return """
boolean result = "foo".equals(text);
boolean result2 = (text1 + text2).equals(text);
boolean result3 = DayOfWeek.MONDAY.equals(object);
boolean result4 = "foo".equalsIgnoreCase(text);
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "boolean result = text.equals(\"foo\");\n" //$NON-NLS-1$
+ "boolean result2 = text.equals(text1 + text2);\n" //$NON-NLS-1$
+ "boolean result3 = object.equals(DayOfWeek.MONDAY);\n" //$NON-NLS-1$
+ "boolean result4 = text.equalsIgnoreCase(\"foo\");\n"; //$NON-NLS-1$
return """
boolean result = text.equals("foo");
boolean result2 = text.equals(text1 + text2);
boolean result3 = object.equals(DayOfWeek.MONDAY);
boolean result4 = text.equalsIgnoreCase("foo");
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.NO_STRING_CREATION)) {
return "" //$NON-NLS-1$
+ "String bar = \"foo\";\n" //$NON-NLS-1$
+ "String newBar = bar.concat(\"abc\");\n" //$NON-NLS-1$
+ "String cantChange = new String(possibleNullObject)\n"; //$NON-NLS-1$
return """
String bar = "foo";
String newBar = bar.concat("abc");
String cantChange = new String(possibleNullObject)
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "String bar = new String(\"foo\");\n" //$NON-NLS-1$
+ "String newBar = (new String(bar)).concat(\"abc\");\n" //$NON-NLS-1$
+ "String cantChange = new String(possibleNullObject)\n"; //$NON-NLS-1$
return """
String bar = new String("foo");
String newBar = (new String(bar)).concat("abc");
String cantChange = new String(possibleNullObject)
"""; //$NON-NLS-1$
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.USE_PATTERN_MATCHING_FOR_INSTANCEOF)) {
return "" //$NON-NLS-1$
+ "if (object instanceof Integer i) {\n" //$NON-NLS-1$
+ " return i.intValue();\n" //$NON-NLS-1$
+ "}\n\n"; //$NON-NLS-1$
return """
if (object instanceof Integer i) {
return i.intValue();
}
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "if (object instanceof Integer) {\n" //$NON-NLS-1$
+ " Integer i = (Integer) object;\n" //$NON-NLS-1$
+ " return i.intValue();\n" //$NON-NLS-1$
+ "}\n"; //$NON-NLS-1$
return """
if (object instanceof Integer) {
Integer i = (Integer) object;
return i.intValue();
}
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,19 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.PLAIN_REPLACEMENT)) {
return "" //$NON-NLS-1$
+ "String result = text.replace(\"foo\", \"bar\");\n" //$NON-NLS-1$
+ "String result2 = text.replace(\"$0.02\", \"$0.50\");\n" //$NON-NLS-1$
+ "String result3 = text.replace('.', '/');\n" //$NON-NLS-1$
+ "String result4 = text.replace(placeholder, value);\n"; //$NON-NLS-1$
return """
String result = text.replace("foo", "bar");
String result2 = text.replace("$0.02", "$0.50");
String result3 = text.replace('.', '/');
String result4 = text.replace(placeholder, value);
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "String result = text.replaceAll(\"foo\", \"bar\");\n" //$NON-NLS-1$
+ "String result2 = text.replaceAll(\"\\\\$0\\\\.02\", \"\\\\$0.50\");\n" //$NON-NLS-1$
+ "String result3 = text.replaceAll(\"\\\\.\", \"/\");\n" //$NON-NLS-1$
+ "String result4 = text.replaceAll(Pattern.quote(placeholder), Matcher.quoteReplacement(value));\n"; //$NON-NLS-1$
return """
String result = text.replaceAll("foo", "bar");
String result2 = text.replaceAll("\\\\$0\\\\.02", "\\\\$0.50");
String result3 = text.replaceAll("\\\\.", "/");
String result4 = text.replaceAll(Pattern.quote(placeholder), Matcher.quoteReplacement(value));
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,29 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.PULL_OUT_IF_FROM_IF_ELSE)) {
return "" //$NON-NLS-1$
+ "if (isActive) {\n" //$NON-NLS-1$
+ " if (isFound) {\n" //$NON-NLS-1$
+ " System.out.println(\"foo\");\n" //$NON-NLS-1$
+ " } else {\n" //$NON-NLS-1$
+ " System.out.println(\"bar\");\n" //$NON-NLS-1$
+ " }\n" //$NON-NLS-1$
+ "}\n\n\n"; //$NON-NLS-1$
return """
if (isActive) {
if (isFound) {
System.out.println("foo");
} else {
System.out.println("bar");
}
}
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "if (isFound) {\n" //$NON-NLS-1$
+ " if (isActive) {\n" //$NON-NLS-1$
+ " System.out.println(\"foo\");\n" //$NON-NLS-1$
+ " }\n" //$NON-NLS-1$
+ "} else {\n" //$NON-NLS-1$
+ " if (isActive) {\n" //$NON-NLS-1$
+ " System.out.println(\"bar\");\n" //$NON-NLS-1$
+ " }\n" //$NON-NLS-1$
+ "}\n"; //$NON-NLS-1$
return """
if (isFound) {
if (isActive) {
System.out.println("foo");
}
} else {
if (isActive) {
System.out.println("bar");
}
}
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ public String getPreview() {
return "Collections.sort(listToSort);\n\n\n\n\n\n"; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "Collections.sort(listToSort, new Comparator<Date>() {\n" //$NON-NLS-1$
+ " @Override\n" //$NON-NLS-1$
+ " public int compare(Date o1, Date o2) {\n" //$NON-NLS-1$
+ " return o1.compareTo(o2);\n" //$NON-NLS-1$
+ " }\n" //$NON-NLS-1$
+ "});\n"; //$NON-NLS-1$
return """
Collections.sort(listToSort, new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
return o1.compareTo(o2);
}
});
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,39 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.USE_SWITCH)) {
return "" //$NON-NLS-1$
+ "switch (number) {\n" //$NON-NLS-1$
+ "case 0:\n" //$NON-NLS-1$
+ " i = 0;\n" //$NON-NLS-1$
+ " break;\n" //$NON-NLS-1$
+ "case 1:\n" //$NON-NLS-1$
+ " j = 10;\n" //$NON-NLS-1$
+ " break;\n" //$NON-NLS-1$
+ "case 2:\n" //$NON-NLS-1$
+ " k = 20;\n" //$NON-NLS-1$
+ " break;\n" //$NON-NLS-1$
+ "default:\n" //$NON-NLS-1$
+ " m = -1;\n" //$NON-NLS-1$
+ " break;\n" //$NON-NLS-1$
+ "}\n"; //$NON-NLS-1$
return """
switch (number) {
case 0:
i = 0;
break;
case 1:
j = 10;
break;
case 2:
k = 20;
break;
default:
m = -1;
break;
}
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "if (number == 0) {\n" //$NON-NLS-1$
+ " i = 0;\n" //$NON-NLS-1$
+ "} else if (number == 1) {\n" //$NON-NLS-1$
+ " j = 10;\n" //$NON-NLS-1$
+ "} else if (2 == number) {\n" //$NON-NLS-1$
+ " k = 20;\n" //$NON-NLS-1$
+ "} else {\n" //$NON-NLS-1$
+ " m = -1;\n" //$NON-NLS-1$
+ "}\n\n\n\n\n\n"; //$NON-NLS-1$
return """
if (number == 0) {
i = 0;
} else if (number == 1) {
j = 10;
} else if (2 == number) {
k = 20;
} else {
m = -1;
}
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,37 @@ public String[] getStepDescriptions() {
@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_CONVERT_TO_SWITCH_EXPRESSIONS)) {
return "" //$NON-NLS-1$
+ "int i = switch(j) {\n" //$NON-NLS-1$
+ " case 1 -> 3;\n" //$NON-NLS-1$
+ " case 2 -> 4;\n" //$NON-NLS-1$
+ " default -> 0;\n" //$NON-NLS-1$
+ "};\n\n\n\n\n\n\n\n\n"; //$NON-NLS-1$
return """
int i = switch(j) {
case 1 -> 3;
case 2 -> 4;
default -> 0;
};
"""; //$NON-NLS-1$
}

return "" //$NON-NLS-1$
+ "int i;\n" //$NON-NLS-1$
+ "switch(j) {\n" //$NON-NLS-1$
+ " case 1:\n" //$NON-NLS-1$
+ " i = 3;\n" //$NON-NLS-1$
+ " break;\n" //$NON-NLS-1$
+ " case 2:\n" //$NON-NLS-1$
+ " i = 4;\n" //$NON-NLS-1$
+ " break;\n" //$NON-NLS-1$
+ " default:\n" //$NON-NLS-1$
+ " i = 0;\n" //$NON-NLS-1$
+ " break;\n" //$NON-NLS-1$
+ "}\n" //$NON-NLS-1$
+ "\n"; //$NON-NLS-1$
return """
int i;
switch(j) {
case 1:
i = 3;
break;
case 2:
i = 4;
break;
default:
i = 0;
break;
}
"""; //$NON-NLS-1$
}
}
Loading

0 comments on commit 7977acf

Please sign in to comment.