Skip to content

Commit 8ce96c7

Browse files
committed
Use ListUtils.map as suggested by reviewer
1 parent f196986 commit 8ce96c7

1 file changed

Lines changed: 12 additions & 28 deletions

File tree

src/main/java/org/openrewrite/staticanalysis/SingleLineCommentSpacing.java

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
package org.openrewrite.staticanalysis;
1818

1919
import java.time.Duration;
20-
import java.util.ArrayList;
2120
import java.util.Collections;
2221
import java.util.List;
2322
import java.util.Set;
2423

2524
import org.openrewrite.ExecutionContext;
2625
import org.openrewrite.Recipe;
2726
import org.openrewrite.TreeVisitor;
27+
import org.openrewrite.internal.ListUtils;
2828
import org.openrewrite.java.JavaIsoVisitor;
2929
import org.openrewrite.java.tree.Comment;
3030
import org.openrewrite.java.tree.Space;
@@ -60,53 +60,37 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6060
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
6161
Space s = super.visitSpace(space, loc, ctx);
6262

63-
List<Comment> comments = s.getComments();
64-
if (comments.isEmpty()) {
63+
if (s.getComments().isEmpty()) {
6564
return s;
6665
}
6766

68-
boolean changed = false;
69-
List<Comment> updatedComments = new ArrayList<>(comments.size());
70-
71-
for (Comment comment : comments) {
72-
Comment updated = comment;
73-
67+
List<Comment> updatedComments = ListUtils.map(s.getComments(), comment -> {
7468
if (comment instanceof TextComment) {
7569
TextComment tc = (TextComment) comment;
7670
String text = tc.getText();
7771

7872
// Skip empty comments
7973
if (text.isEmpty()) {
80-
updatedComments.add(comment);
81-
continue;
74+
return comment;
8275
}
8376

8477
// Skip special comments like //language=java
8578
if (text.startsWith("language=")) {
86-
updatedComments.add(comment);
87-
continue;
79+
return comment;
8880
}
8981

90-
// If already has space after //
82+
// Already has space after //
9183
if (text.startsWith(" ")) {
92-
updatedComments.add(comment);
93-
continue;
84+
return comment;
9485
}
9586

96-
// Add space after //
97-
String newText = " " + text;
98-
99-
if (!newText.equals(text)) {
100-
updated = tc.withText(newText);
101-
changed = true;
102-
}
87+
// Add space
88+
return tc.withText(" " + text);
10389
}
90+
return comment;
91+
});
10492

105-
updatedComments.add(updated);
106-
}
107-
108-
// Only return new object if something changed (important for single-cycle rule)
109-
return changed ? s.withComments(updatedComments) : s;
93+
return s.withComments(updatedComments);
11094
}
11195
};
11296
}

0 commit comments

Comments
 (0)