|
19 | 19 | import org.openrewrite.Preconditions; |
20 | 20 | import org.openrewrite.Recipe; |
21 | 21 | import org.openrewrite.TreeVisitor; |
| 22 | +import org.openrewrite.internal.ListUtils; |
22 | 23 | import org.openrewrite.java.JavaIsoVisitor; |
23 | 24 | import org.openrewrite.java.JavaTemplate; |
24 | 25 | import org.openrewrite.java.MethodMatcher; |
25 | 26 | import org.openrewrite.java.search.UsesMethod; |
26 | 27 | import org.openrewrite.java.tree.*; |
27 | 28 |
|
28 | | -import java.util.ArrayList; |
29 | | -import java.util.List; |
| 29 | +import static java.util.Objects.requireNonNull; |
30 | 30 |
|
31 | 31 | public class OptionalStreamRecipe extends Recipe { |
32 | 32 | @Override |
@@ -69,27 +69,22 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation invocation, E |
69 | 69 | return mapInvocation; |
70 | 70 | } |
71 | 71 |
|
72 | | - JRightPadded<Expression> filterSelect = filterInvocation.getPadding().getSelect(); |
73 | | - JRightPadded<Expression> mapSelect = mapInvocation.getPadding().getSelect(); |
74 | | - JavaType.Method mapInvocationType = mapInvocation.getMethodType(); |
75 | | - Space flatMapComments = getFlatMapComments(mapSelect, filterSelect); |
76 | | - JavaTemplate template = |
77 | | - JavaTemplate.builder("#{any(java.util.stream.Stream)}.flatMap(Optional::stream)") |
78 | | - .imports("java.util.Optional") |
79 | | - .build(); |
80 | | - J.MethodInvocation flatMapInvocation = template |
81 | | - .apply(updateCursor(mapInvocation), mapInvocation.getCoordinates().replace(), filterInvocation.getSelect()); |
| 72 | + JRightPadded<Expression> filterSelect = requireNonNull(filterInvocation.getPadding().getSelect()); |
| 73 | + JRightPadded<Expression> mapSelect = requireNonNull(mapInvocation.getPadding().getSelect()); |
| 74 | + JavaType.Method mapInvocationType = requireNonNull(mapInvocation.getMethodType()); |
| 75 | + J.MethodInvocation flatMapInvocation = JavaTemplate.builder("#{any(java.util.stream.Stream)}.flatMap(Optional::stream)") |
| 76 | + .imports("java.util.Optional") |
| 77 | + .build() |
| 78 | + .apply(updateCursor(mapInvocation), mapInvocation.getCoordinates().replace(), filterSelect.getElement()); |
| 79 | + Space flatMapComments = filterSelect.getAfter().withComments(ListUtils.concatAll( |
| 80 | + filterSelect.getAfter().getComments(), |
| 81 | + mapSelect.getAfter().getComments() |
| 82 | + )); |
82 | 83 | return flatMapInvocation.getPadding() |
83 | 84 | .withSelect(filterSelect.withAfter(flatMapComments)) |
84 | 85 | .withMethodType(mapInvocationType.withName("flatMap")) |
85 | 86 | .withPrefix(mapInvocation.getPrefix()); |
86 | 87 | } |
87 | 88 |
|
88 | | - private static Space getFlatMapComments(JRightPadded<Expression> mapSelect, JRightPadded<Expression> filterSelect) { |
89 | | - List<Comment> comments = new ArrayList<>(); |
90 | | - comments.addAll(filterSelect.getAfter().getComments()); |
91 | | - comments.addAll(mapSelect.getAfter().getComments()); |
92 | | - return filterSelect.getAfter().withComments(comments); |
93 | | - } |
94 | 89 | } |
95 | 90 | } |
0 commit comments