Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit 5ed46f4

Browse files
timtebeekTeamModerne
authored andcommitted
refactor: Static imports for Collections and Collectors
Use this link to re-run the recipe: https://app.moderne.io/builder/m9HsHwu2Z?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne <team@moderne.io>
1 parent 88d817f commit 5ed46f4

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

rewrite-python/src/main/java/org/openrewrite/python/PythonParser.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@
4242
import java.nio.file.Paths;
4343
import java.util.*;
4444
import java.util.concurrent.TimeUnit;
45-
import java.util.stream.Collectors;
4645
import java.util.stream.Stream;
4746

4847
import static java.util.Objects.requireNonNull;
48+
import static java.util.stream.Collectors.joining;
49+
import static java.util.stream.Collectors.toList;
4950

5051
@SuppressWarnings("unused")
5152
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@@ -192,7 +193,7 @@ private void initializeRemoting(ExecutionContext ctx) throws IOException {
192193
if (!pythonPath.isEmpty()) {
193194
Map<String, String> environment = processBuilder.environment();
194195
environment.compute("PYTHONPATH", (k, current) ->
195-
(current != null ? current + File.pathSeparator : "") + pythonPath.stream().map(Path::toString).collect(Collectors.joining(File.pathSeparator)));
196+
(current != null ? current + File.pathSeparator : "") + pythonPath.stream().map(Path::toString).collect(joining(File.pathSeparator)));
196197
}
197198
if (logCompilationWarningsAndErrors) {
198199
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
@@ -281,7 +282,7 @@ private static boolean verifyRemotingInstallation(Path dir, String executable, @
281282
}
282283

283284
try (InputStream inputStream = requireNonNull(PythonParser.class.getClassLoader().getResourceAsStream("META-INF/python-requirements.txt"))) {
284-
List<String> packages = new BufferedReader(new InputStreamReader(inputStream)).lines().filter(l -> !l.isEmpty()).collect(Collectors.toList());
285+
List<String> packages = new BufferedReader(new InputStreamReader(inputStream)).lines().filter(l -> !l.isEmpty()).collect(toList());
285286

286287
List<String> command = new ArrayList<>(Arrays.asList(executable, "-m", "pip", "install", "--target", dir.toString()));
287288
command.addAll(packages);

rewrite-python/src/main/java/org/openrewrite/python/style/Autodetect.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,24 @@
2525
import org.openrewrite.style.NamedStyles;
2626
import org.openrewrite.style.Style;
2727

28-
import java.util.*;
28+
import java.util.Arrays;
29+
import java.util.Collection;
30+
import java.util.HashMap;
31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.Map.Entry;
34+
import java.util.UUID;
35+
36+
import static java.util.Collections.emptySet;
37+
import static java.util.Collections.max;
2938

3039
public class Autodetect extends NamedStyles implements PythonStyle {
3140
public Autodetect(UUID id, Collection<Style> styles) {
3241
super(id,
3342
"org.openrewrite.python.Autodetect",
3443
"Auto-detected",
3544
"Automatically detect styles from a repository's existing code.",
36-
Collections.emptySet(),
45+
emptySet(),
3746
styles);
3847
}
3948

@@ -96,7 +105,7 @@ private static class IndentStatistics {
96105
private final Map<Integer, Integer> countsByIndentSize = new HashMap<>();
97106

98107
private <T> T keyWithHighestCount(Map<T, Integer> counts) {
99-
int max = Collections.max(counts.values());
108+
int max = max(counts.values());
100109
return counts.entrySet()
101110
.stream()
102111
.filter(entry -> entry.getValue() == max)

rewrite-python/src/main/java/org/openrewrite/python/style/IntelliJ.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import org.openrewrite.style.NamedStyles;
1919

2020
import java.util.Arrays;
21-
import java.util.Collections;
2221

22+
import static java.util.Collections.emptySet;
2323
import static org.openrewrite.Tree.randomId;
2424

2525

@@ -31,7 +31,7 @@ public IntelliJ() {
3131
"org.openrewrite.python.style.IntelliJ",
3232
"IntelliJ IDEA",
3333
"IntelliJ IDEA default Python style.",
34-
Collections.emptySet(),
34+
emptySet(),
3535
Arrays.asList(
3636
spaces(),
3737
wrappingAndBraces(),

rewrite-python/src/main/java/org/openrewrite/python/tree/Py.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
import java.nio.file.Path;
3636
import java.util.List;
3737
import java.util.UUID;
38-
import java.util.stream.Collectors;
38+
39+
import static java.util.stream.Collectors.toList;
3940

4041
public interface Py extends J {
4142

@@ -586,7 +587,7 @@ public List<ClassDeclaration> getClasses() {
586587
.map(JRightPadded::getElement)
587588
.filter(J.ClassDeclaration.class::isInstance)
588589
.map(J.ClassDeclaration.class::cast)
589-
.collect(Collectors.toList());
590+
.collect(toList());
590591
}
591592

592593
@Override

rewrite-python/src/main/java/org/openrewrite/python/tree/PySpace.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
import org.openrewrite.marker.Markers;
2323

2424
import java.util.ArrayList;
25-
import java.util.Collections;
2625
import java.util.List;
2726

27+
import static java.util.Collections.emptyList;
28+
2829
public final class PySpace {
2930

3031
public static final class SpaceBuilder {
@@ -79,7 +80,7 @@ public Space build() {
7980
finishComment();
8081
Space space = Space.build(
8182
initialWhitespace == null ? "" : initialWhitespace,
82-
comments == null ? Collections.emptyList() : comments
83+
comments == null ? emptyList() : comments
8384
);
8485
reset();
8586
return space;
@@ -139,7 +140,7 @@ public enum IndentEndMode {
139140
public static Space reindent(Space original, String indentWithoutNewline, IndentStartMode startMode, IndentEndMode endMode) {
140141

141142
if (indentWithoutNewline.contains("\n")) {
142-
throw new IllegalArgumentException("argument to `deindent` should not contain newline: " + Space.build(indentWithoutNewline, Collections.emptyList()));
143+
throw new IllegalArgumentException("argument to `deindent` should not contain newline: " + Space.build(indentWithoutNewline, emptyList()));
143144
}
144145

145146
if (indentWithoutNewline.isEmpty()) {
@@ -165,7 +166,7 @@ public static Space reindent(Space original, String indentWithoutNewline, Indent
165166
break;
166167
}
167168

168-
Space space = Space.build(original.getWhitespace(), Collections.emptyList());
169+
Space space = Space.build(original.getWhitespace(), emptyList());
169170

170171
List<Comment> originalComments = original.getComments();
171172

@@ -202,7 +203,7 @@ public static Space reindent(Space original, String indentWithoutNewline, Indent
202203

203204
public static Space deindent(Space original, String indentWithoutNewline, IndentStartMode startMode, IndentEndMode endMode) {
204205
if (indentWithoutNewline.contains("\n")) {
205-
throw new IllegalArgumentException("argument to `deindent` should not contain newline: " + Space.build(indentWithoutNewline, Collections.emptyList()));
206+
throw new IllegalArgumentException("argument to `deindent` should not contain newline: " + Space.build(indentWithoutNewline, emptyList()));
206207
}
207208

208209
if (indentWithoutNewline.isEmpty()) {
@@ -241,7 +242,7 @@ public static Space deindent(Space original, String indentWithoutNewline, Indent
241242
} else {
242243
// weird coincidence; maybe not possible?
243244
currentlyIndented = false;
244-
space = Space.build(original.getWhitespace(), Collections.emptyList());
245+
space = Space.build(original.getWhitespace(), emptyList());
245246
}
246247
} else if (original.getWhitespace().endsWith(indentWithNewline)) {
247248
currentlyIndented = true;
@@ -250,11 +251,11 @@ public static Space deindent(Space original, String indentWithoutNewline, Indent
250251
// just keep the newline
251252
0, original.getWhitespace().length() - indentWithoutNewline.length()
252253
),
253-
Collections.emptyList()
254+
emptyList()
254255
);
255256
} else {
256257
currentlyIndented = false;
257-
space = Space.build(original.getWhitespace(), Collections.emptyList());
258+
space = Space.build(original.getWhitespace(), emptyList());
258259
}
259260

260261
List<Comment> originalComments = original.getComments();

0 commit comments

Comments
 (0)