StreamToLoopInspection: handle some if/ifPresent cases

This commit is contained in:
Tagir Valeev
2016-12-13 12:33:43 +07:00
parent 1e61604d88
commit 7b65a6919d
7 changed files with 71 additions and 20 deletions
@@ -6,19 +6,19 @@ import static java.util.Arrays.asList;
public class Main {
public static void test(List<List<String>> list) {
boolean allMatch = true;
boolean b = true;
OUTER:
for (List<String> x : list) {
if (x != null) {
for (String s : x) {
if (!s.startsWith("a")) {
allMatch = false;
b = false;
break OUTER;
}
}
}
}
if(allMatch) {
if(b) {
System.out.println("ok");
}
}
@@ -5,18 +5,14 @@ import java.util.List;
public class Main {
private static void test(List<String> list) {
boolean found = false;
for (String x : list) {
if (x != null) {
if (x.startsWith("x")) {
found = true;
System.out.println("Ok!");
break;
}
}
}
if(found) {
System.out.println("Ok!");
}
}
public static void main(String[] args) {
@@ -0,0 +1,19 @@
// "Replace Stream API chain with loop" "true"
import java.util.Arrays;
import java.util.List;
public class Main {
private static void test(List<String> list) {
for (String str : list) {
if (str.contains("x")) {
System.out.println(str);
break;
}
}
}
public static void main(String[] args) {
test(Arrays.asList("a", "b", "syz"));
}
}
@@ -13,14 +13,14 @@ public class Main {
// cannot replace as replacement would generate a statement before "super" call
Child() {
super(false);
boolean found = false;
boolean b = false;
for (String s : Arrays.asList("a", "b", "c")) {
if (Objects.nonNull(s)) {
found = true;
b = true;
break;
}
}
super.test(found);
super.test(b);
}
}
}
@@ -0,0 +1,14 @@
// "Replace Stream API chain with loop" "true"
import java.util.Arrays;
import java.util.List;
public class Main {
private static void test(List<String> list) {
list.stream().filter(str -> str.contains("x")).fin<caret>dFirst().ifPresent(System.out::println);
}
public static void main(String[] args) {
test(Arrays.asList("a", "b", "syz"));
}
}