Stream API migration various fixes

1. findFirst() scenario can pull previous assignment (not declaration) now
2. anyMatch() fix did not work if there's single assignment to non-variable (e.g. array element)
3. if non-adjacent return becomes unreachable after findFirst()/anyMatch(), it returned automatically now
This commit is contained in:
Tagir Valeev
2016-10-07 11:59:52 +07:00
parent 3a7563c3f0
commit ebf320b288
11 changed files with 198 additions and 23 deletions
@@ -0,0 +1,12 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
String[] found = {"no"};
if (data.stream().map(String::trim).anyMatch(trimmed -> !trimmed.isEmpty())) {
found[0] = "yes";
}
}
}
@@ -0,0 +1,13 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
boolean find(List<String> data) {
if(data != null) {
return data.stream().map(String::trim).anyMatch(trimmed -> trimmed.startsWith("xyz"));
} else {
throw new IllegalArgumentException();
}
}
}
@@ -0,0 +1,20 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
private int getInitialSize() {return 0;}
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 10;
System.out.println(firstSize);
// loop
// comment
firstSize = map.values().stream().filter(Objects::nonNull).findFirst().map(List::size).orElse(getInitialSize());
System.out.println(firstSize);
}
}
@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.util.Collection;
import java.util.List;
public class Main {
public static String find(List<List<String>> list) {
if(list == null) {
System.out.println("oops");
return "";
} else {
return list.stream().flatMap(Collection::stream).filter(string -> string.startsWith("ABC")).findFirst().orElse(null);
}
}
}
@@ -0,0 +1,16 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public void testAssignment(List<String> data) {
String[] found = {"no"};
for(String str : da<caret>ta) {
String trimmed = str.trim();
if(!trimmed.isEmpty()) {
found[0] = "yes";
break;
}
}
}
}
@@ -0,0 +1,19 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
boolean find(List<String> data) {
if(data != null) {
for (String e : da<caret>ta) {
String trimmed = e.trim();
if (trimmed.startsWith("xyz")) {
return true;
}
}
} else {
throw new IllegalArgumentException();
}
return false;
}
}
@@ -0,0 +1,25 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
private int getInitialSize() {return 0;}
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 10;
System.out.println(firstSize);
firstSize = getInitialSize();
// loop
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
firstSize = list.size();
// comment
break;
}
}
System.out.println(firstSize);
}
}
@@ -0,0 +1,21 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public static String find(List<List<String>> list) {
if(list == null) {
System.out.println("oops");
return "";
} else {
for (List<String> innerList : lis<caret>t) {
for (String string : innerList) {
if (string.startsWith("ABC")) {
return string;
}
}
}
}
return null;
}
}