StreamToLoopInspection: support block void lambdas (and returns in forEach not inside nested loop)

This commit is contained in:
Tagir Valeev
2017-01-31 16:29:03 +03:00
parent c15f721206
commit d8836c315d
6 changed files with 222 additions and 27 deletions
@@ -0,0 +1,77 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("a", "b", "c", "d");
for (String o : list) {
if (!o.isEmpty()) {
System.out.println("Peek: " + o);
System.out.println("Peek2: " + o);
String n = "";
System.out.println(o);
if (o.equals("c")) continue;
System.out.println(o + "!!!" + n);
new Runnable() {
String e = "x";
public void run() {
System.out.println(e);
for (int i = 0; i < 10; i++) {
if (e.length() == i) return;
}
}
}.run();
}
}
for (String s : list) {
if ("b".equals(s)) {
System.out.println("Found:");
System.out.println(s);
break;
}
}
Optional<String> found = Optional.empty();
for (String qq : list) {
if ("b".equals(qq)) {
found = Optional.of(qq);
break;
}
}
found.ifPresent(str -> {
System.out.println("Found:");
if(str.isEmpty()) return; // return inside ifPresent is not supported
System.out.println(str);
});
StringBuilder res = new StringBuilder();
for (String str : list) {
if (str != null) {
str = "[" + str + "]";
res.append(str);
}
}
System.out.println(res);
long count = 0L;
for (String n : list) {
if (!"a".equals(n)) {
for (int i = 0; i < 3; i++) {
System.out.println("In flatmap idx: " + i);
System.out.println("In flatmap: " + n);
count++;
}
}
}
System.out.println(count);
}
}
@@ -0,0 +1,51 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("a", "b", "c", "d");
list.stream().filter(n -> !n.isEmpty()).peek(o -> {
System.out.println("Peek: "+o);
System.out.println("Peek2: "+o);
}).for<caret>Each(e -> {
String n = "";
System.out.println(e);
if (e.equals("c")) return;
System.out.println(e + "!!!" + n);
new Runnable() {String e = "x"; public void run() {
System.out.println(e);
for(int i=0; i<10; i++) {
if(e.length() == i) return;
}
}}.run();
});
list.stream().filter("b"::equals).findFirst().ifPresent(str -> {
System.out.println("Found:");
System.out.println(str);
});
list.stream().filter(qq -> "b".equals(qq)).findFirst().ifPresent(str -> {
System.out.println("Found:");
if(str.isEmpty()) return; // return inside ifPresent is not supported
System.out.println(str);
});
StringBuilder res = list.stream().filter(str -> str != null).collect(StringBuilder::new, (sb, s) -> {
s = "[" + s + "]";
sb.append(s);
}, StringBuilder::append);
System.out.println(res);
System.out.println(list.stream().filter(n -> !"a".equals(n)).flatMapToInt(l -> IntStream.range(0, 3).peek(n -> {
System.out.println("In flatmap idx: "+n);
System.out.println("In flatmap: "+l);
})).count());
}
}