support min/max in stream api migration, rearrange tests

This commit is contained in:
Roman Ivanov
2017-08-02 12:57:45 +07:00
parent dd4ba58889
commit 72bab18a58
561 changed files with 2091 additions and 17 deletions
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.util.Collection;
import java.util.List;
public class Main {
public static String find(List<List<String>> list) {
/*
Block comment
*/
return list.stream().flatMap(Collection::stream).filter(string -> string.startsWith("ABC")).findFirst().map(string -> string.substring(3)).orElse("");
}
}
@@ -0,0 +1,12 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
public int[] testMap(Map<String, List<String>> map) throws Exception {
int[] arr = map.values().stream().filter(Objects::nonNull).findFirst().map(list -> new int[]{list.size()}).orElse(null);
return arr;
}
}
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = map.values().stream().filter(Objects::nonNull).findFirst().map(List::size).orElse(0);
// comment
System.out.println(firstSize);
}
}
@@ -0,0 +1,10 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public void testCast(List<Object> data) {
String found = (String) data.stream().filter(obj -> obj instanceof String).findFirst().orElse(null);
System.out.println(found);
}
}
@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
List<String> firstList = new ArrayList<>();
firstList.add("none");
firstList = map.values().stream().filter(Objects::nonNull).findFirst().orElse(firstList);
System.out.println(firstList);
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize;
int other = map.size();
if(other > 10) {
System.out.println("Big");
}
// comment
firstSize = map.values().stream().filter(Objects::nonNull).findFirst().map(List::size).orElse(0);
System.out.println(firstSize);
}
}
@@ -0,0 +1,11 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int bigSize = map.values().stream().mapToInt(List::size).filter(size -> size > 10).findFirst().orElse(0);
System.out.println(bigSize);
}
}
@@ -0,0 +1,14 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 0, other = firstSize;
// comment
firstSize = map.values().stream().filter(Objects::nonNull).findFirst().map(List::size).orElse(firstSize);
System.out.println(firstSize);
}
}
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
String firstStr = map.values().stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> !str.isEmpty()).findFirst().orElse("");
System.out.println(firstStr);
}
}
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.util.Collection;
import java.util.List;
public class Main {
public static boolean find(List<List<String>> list) {
/*
Block comment
*/
return list.stream().flatMap(Collection::stream).filter(string -> string.startsWith("ABC")).findFirst().filter(string -> string.substring(3).equals("xyz")).isPresent();
}
}
@@ -0,0 +1,9 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public String testCast(List<Object> data) {
return (String) data.stream().filter(obj -> obj instanceof String).findFirst().orElse(null);
}
}
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
public class Main {
enum MyEnum { FOO, BAR, BAZ }
public static MyEnum find(List<EnumSet<MyEnum>> list) {
return list.stream().flatMap(Collection::stream).filter(val -> val.name().startsWith("B")).findFirst().orElse(MyEnum.FOO);
}
}
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.awt.*;
import java.util.List;
import java.util.Objects;
public class Main {
private Point field = new Point(0, 0);
public Point find(List<Point> points) {
return points.stream().filter(Objects::nonNull).findFirst().orElse(field);
}
}
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public Integer[] testFindFirstIfPresent(List<List<String>> data) {
List<Integer> result = new ArrayList<>();
for (List<String> list : data) {
list.stream().filter(str -> !str.isEmpty()).findFirst().ifPresent(str -> result.add(str.length()));
}
return result.toArray(new Integer[result.size()]);
}
}
@@ -0,0 +1,16 @@
// "Replace with findFirst()" "true"
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Main {
public List<Integer> testFindFirstIfPresent(List<List<String>> data) {
List<Integer> result = new ArrayList<>();
if (!data.isEmpty()) {
data.stream().flatMap(Collection::stream).filter(str -> !str.isEmpty()).findFirst().ifPresent(str -> result.add(str.length()));
}
return result;
}
}
@@ -0,0 +1,17 @@
// "Replace with findFirst()" "true"
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public class Main {
private static String test(List<String> list) {
Optional<String> found = list.stream().filter(Objects::nonNull).findFirst();
return found.orElse(null);
}
public static void main(String[] args) {
System.out.println(test(Arrays.asList(null, null, "aa", "bbb", "c", null, "dd")));
}
}
@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Optional;
public class Main {
public Optional<String> trim(String s) {
return s.isEmpty() ? Optional.empty() : Optional.of(s.trim());
}
public Optional<String> test(List<Object> objects) {
Optional<String> result = objects.stream().filter(obj -> obj instanceof String).findFirst().flatMap(obj -> trim((String) obj));
return result;
}
}
@@ -0,0 +1,10 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public List<String> getErrors(List<String> data) {
List<String> def = Collections.singletonList("Not found");
return data.stream().filter(s -> s.startsWith("xyz")).findFirst().<List<String>>map(s -> s.length() < 10 ? Collections.emptyList() : Arrays.asList()).orElse(def);
}
}
@@ -0,0 +1,13 @@
// "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) {
return list.stream().flatMap(Collection::stream).filter(string -> string.startsWith("ABC")).findFirst().orElse(null);
}
return null;
}
}
@@ -0,0 +1,9 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public int testPrimitiveMap(List<String> data) {
return data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).findFirst().orElse(0);
}
}
@@ -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,10 @@
// "Replace with findFirst()" "true"
import java.util.Arrays;
import java.util.stream.Stream;
public class TestFile {
public void test() {
Stream.of("a", "b").filter(s -> !s.isEmpty()).findFirst().ifPresent(System.out::println);
}
}
@@ -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,10 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public Runnable getRunnable(List<String> data) {
Runnable def = () -> {};
return data.stream().filter(s -> s.startsWith("xyz")).findFirst().<Runnable>map(s -> s.length() > 2 ? s::trim : System.out::println).orElse(def);
}
}
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.awt.*;
import java.util.List;
import java.util.Objects;
public class Main {
private static Point ZERO = new Point(0, 0);
public static Point find(List<Point> points) {
return points.stream().filter(Objects::nonNull).findFirst().orElse(ZERO);
}
}
@@ -0,0 +1,10 @@
// "Replace with findFirst()" "true"
import java.util.Collection;
import java.util.List;
public class Main {
public static String find(List<List<String>> list) {
return list.stream().flatMap(Collection::stream).filter(string -> string.startsWith("ABC")).findFirst().filter(string -> string.substring(3).equals("xyz")).map(String::trim).orElse(null);
}
}
@@ -0,0 +1,14 @@
// "Replace with findFirst()" "true"
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args) {
String s = " hello ";
String res = s.trim();
if(args.length == 0) {
res = IntStream.range(0, s.length()).boxed().filter(x -> s.charAt(x) == 'l').findFirst().map(String::valueOf).orElse(res);
}
System.out.println(res);
}
}
@@ -0,0 +1,19 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public static String find(List<List<String>> list) {
for(List<String> innerList : li<caret>st) {
for(String string : innerList) {
if(string.startsWith("ABC")) {
return string.substring(3);
/*
Block comment
*/
}
}
}
return "";
}
}
@@ -0,0 +1,17 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public int[] testMap(Map<String, List<String>> map) throws Exception {
int[] arr = null;
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
arr = {list.size()};
break;
}
}
return arr;
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 0;
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
firstSize = list.size();
// comment
break;
}
}
System.out.println(firstSize);
}
}
@@ -0,0 +1,16 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public void testCast(List<Object> data) {
String found = null;
for (Object obj : da<caret>ta) {
if(obj instanceof String) {
found = (String)obj;
break;
}
}
System.out.println(found);
}
}
@@ -0,0 +1,19 @@
// "Replace with findFirst()" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
List<String> firstList = new ArrayList<>();
firstList.add("none");
for(List<String> list : ma<caret>p.values()) {
if(list != null) {
firstList = list;
break;
}
}
System.out.println(firstList);
}
}
@@ -0,0 +1,22 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 0;
int other = map.size();
if(other > 10) {
System.out.println("Big");
}
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
firstSize = list.size();
// comment
break;
}
}
System.out.println(firstSize);
}
}
@@ -0,0 +1,22 @@
// "Replace with findFirst()" "false"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
String firstStr = "";
OUTER:
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
for(String str : list) {
if(!str.isEmpty()) {
firstStr = str;
break;
}
}
}
}
System.out.println(firstStr);
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int bigSize = 0;
for(List<String> list : map.valu<caret>es()) {
int size = list.size();
if(size > 10) {
bigSize = size;
break;
}
}
System.out.println(bigSize);
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "false"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int bigSize = 0;
for(List<String> list : map.valu<caret>es()) {
int size = list.size();
if(size > 10) {
bigSize = size*2;
break;
}
}
System.out.println(bigSize);
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
int firstSize = 0, other = firstSize;
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
firstSize = list.size();
// comment
break;
}
}
System.out.println(firstSize);
}
}
@@ -0,0 +1,22 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Map;
public class Main {
public void testMap(Map<String, List<String>> map) throws Exception {
String firstStr = "";
OUTER:
for(List<String> list : map.valu<caret>es()) {
if(list != null) {
for(String str : list) {
if(!str.isEmpty()) {
firstStr = str;
break OUTER;
}
}
}
}
System.out.println(firstStr);
}
}
@@ -0,0 +1,19 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public static boolean find(List<List<String>> list) {
for(List<String> innerList : li<caret>st) {
for(String string : innerList) {
if(string.startsWith("ABC")) {
return string.substring(3).equals("xyz");
/*
Block comment
*/
}
}
}
return false;
}
}
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "false"
import java.awt.*;
import java.util.List;
public class Main {
public Point find(List<Point> points, Main other) {
for (Point pt : poin<caret>ts) {
if (pt != null) return pt;
}
return new Point(0, 0);
}
}
@@ -0,0 +1,14 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public String testCast(List<Object> data) {
for (Object obj : d<caret>ata) {
if(obj instanceof String) {
return (String)obj;
}
}
return null;
}
}
@@ -0,0 +1,19 @@
// "Replace with findFirst()" "true"
import java.util.EnumSet;
import java.util.List;
public class Main {
enum MyEnum { FOO, BAR, BAZ }
public static MyEnum find(List<EnumSet<MyEnum>> list) {
for (EnumSet<MyEnum> set : lis<caret>t) {
for (MyEnum val : set) {
if (val.name().startsWith("B")) {
return val;
}
}
}
return MyEnum.FOO;
}
}
@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.awt.*;
import java.util.List;
public class Main {
private Point field = new Point(0, 0);
public Point find(List<Point> points) {
for (Point pt : poin<caret>ts) {
if (pt != null) return pt;
}
return field;
}
}
@@ -0,0 +1,15 @@
// "Replace with findFirst()" "false"
import java.awt.*;
import java.util.List;
public class Main {
private Point field = new Point(0, 0);
public Point find(List<Point> points, Main other) {
for (Point pt : poin<caret>ts) {
if (pt != null) return pt;
}
return other.field;
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public Integer[] testFindFirstIfPresent(List<List<String>> data) {
List<Integer> result = new ArrayList<>();
for (List<String> list : data) {
for (String str : lis<caret>t) {
if (!str.isEmpty()) {
result.add(str.length());
break;
}
}
}
return result.toArray(new Integer[result.size()]);
}
}
@@ -0,0 +1,20 @@
// "Replace with findFirst()" "false"
import java.util.*;
// The result will have block-lambda so we don't suggest the replacement here
public class Main {
public Integer[] testFindFirstIfPresent(List<List<String>> data) {
List<Integer> result = new ArrayList<>();
for (List<String> list : data) {
for (String str : lis<caret>t) {
if (!str.isEmpty()) {
result.add(str.length());
System.out.println(str);
break;
}
}
}
return result.toArray(new Integer[result.size()]);
}
}
@@ -0,0 +1,22 @@
// "Replace with findFirst()" "true"
import java.util.ArrayList;
import java.util.List;
public class Main {
public List<Integer> testFindFirstIfPresent(List<List<String>> data) {
List<Integer> result = new ArrayList<>();
if (!data.isEmpty()) {
for (List<String> list : da<caret>ta) {
for (String str : list) {
if (!str.isEmpty()) {
result.add(str.length());
return result;
}
}
}
}
return result;
}
}
@@ -0,0 +1,23 @@
// "Replace with findFirst()" "true"
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public class Main {
private static String test(List<String> list) {
Optional<String> found = Optional.empty();
for (String s : li<caret>st) {
if (Objects.nonNull(s)) {
found = Optional.of(s);
break;
}
}
return found.orElse(null);
}
public static void main(String[] args) {
System.out.println(test(Arrays.asList(null, null, "aa", "bbb", "c", null, "dd")));
}
}
@@ -0,0 +1,21 @@
// "Replace with findFirst()" "true"
import java.util.List;
import java.util.Optional;
public class Main {
public Optional<String> trim(String s) {
return s.isEmpty() ? Optional.empty() : Optional.of(s.trim());
}
public Optional<String> test(List<Object> objects) {
Optional<String> result = Optional.empty();
for(Object obj : obj<caret>ects) {
if(obj instanceof String) {
result = trim((String)obj);
break;
}
}
return result;
}
}
@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public List<String> getErrors(List<String> data) {
List<String> def = Collections.singletonList("Not found");
for(String s : dat<caret>a) {
if(s.startsWith("xyz")) {
return s.length() < 10 ? Collections.emptyList() : Arrays.asList();
}
}
return def;
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public static String find(List<List<String>> list) {
if(list != null) {
for (List<String> innerList : lis<caret>t) {
for (String string : innerList) {
if (string.startsWith("ABC")) {
return string;
}
}
}
}
return null;
}
}
@@ -0,0 +1,17 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public int testPrimitiveMap(List<String> data) {
for(String str : dat<caret>a) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
return len;
}
}
}
return 0;
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "false"
import java.util.List;
// Not supported as OptionalInt lacks mapping method
public class Main {
public int testPrimitiveMap(List<String> data) {
for(String str : dat<caret>a) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
return len * 2;
}
}
}
return 0;
}
}
@@ -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,14 @@
// "Replace with findFirst()" "true"
import java.util.Arrays;
public class TestFile {
public void test() {
for(String s : Ar<caret>rays.asList("a", "b")) {
if(!s.isEmpty()) {
System.out.println(s);
return;
}
}
}
}
@@ -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;
}
}
@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public Runnable getRunnable(List<String> data) {
Runnable def = () -> {};
for(String s : dat<caret>a) {
if(s.startsWith("xyz")) {
return s.length() > 2 ? s::trim : System.out::println;
}
}
return def;
}
}
@@ -0,0 +1,15 @@
// "Replace with findFirst()" "true"
import java.awt.*;
import java.util.List;
public class Main {
private static Point ZERO = new Point(0, 0);
public static Point find(List<Point> points) {
for (Point pt : point<caret>s) {
if (pt != null) return pt;
}
return ZERO;
}
}
@@ -0,0 +1,16 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public static String find(List<List<String>> list) {
for(List<String> innerList : li<caret>st) {
for(String string : innerList) {
if(string.startsWith("ABC")) {
return string.substring(3).equals("xyz") ? string.trim() : null;
}
}
}
return null;
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "false"
public class Main {
public boolean check(int value) {
return value == 3;
}
public void find() {
int end = 10;
for(<caret>int i=0; i<end; i++) {
if(check(i)) {
end = i;
break;
}
}
System.out.println(end);
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
public class Test {
public static void main(String[] args) {
String s = " hello ";
String res = s.trim();
if(args.length == 0) {
for (<caret>int i = 0; i < s.length(); i++) {
Integer x = i;
if (s.charAt(x) == 'l') {
res = String.valueOf(x);
break;
}
}
}
System.out.println(res);
}
}