mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
IDEA-160898 Inspection to convert several commonly-used lambdas to method references
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
Function<Object, String> fn = String.class::cast;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.Predicate;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
Predicate<Object> pred = String.class::isInstance;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
Predicate<Object> pred = Objects::nonNull;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
Function<Object, String> fn = s -> (String)<caret>s;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda with method reference" "false"
|
||||
import java.util.function.Function;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
Function<Object, List<String>> fn = s -> (List<String>)<caret>s;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda with method reference" "false"
|
||||
import java.util.function.ToIntFunction;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
ToIntFunction<Object> fn = s -> (int)<caret>s;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.Predicate;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
Predicate<Object> pred = s -> s instanceof <caret>String;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda with method reference" "true"
|
||||
import java.util.function.Predicate;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
Predicate<Object> pred = s -> s != <caret>null;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace lambda with method reference" "false"
|
||||
import java.util.function.Predicate;
|
||||
|
||||
class Bar extends Random {
|
||||
public void test(Object obj) {
|
||||
Predicate<Object> pred = s -> obj != <caret>null;
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,11 +1,12 @@
|
||||
// "Replace with forEach" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
class Sample {
|
||||
List<String> foo = new ArrayList<>();
|
||||
String foo(){
|
||||
((List<String>) foo).stream().filter(s -> s == null).forEach(System.out::println);
|
||||
((List<String>) foo).stream().filter(Objects::isNull).forEach(System.out::println);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -10,6 +10,6 @@ public class Collect {
|
||||
}
|
||||
|
||||
void collectNames(List<Person> persons){
|
||||
List<String> names = persons.stream().filter(person -> person != null).map(Person::getName).collect(Collectors.toList());
|
||||
List<String> names = persons.stream().filter(Objects::nonNull).map(Person::getName).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,10 +1,11 @@
|
||||
// "Replace with count()" "true"
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public class Main {
|
||||
public void test(List<Set<String>> nested) {
|
||||
int count = (int) nested.stream().filter(element -> element != null).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).count();
|
||||
int count = (int) nested.stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).count();
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -1,12 +1,9 @@
|
||||
// "Replace with collect" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public void test(List<Set<String>> nested) {
|
||||
List<String> result = nested.stream().filter(element -> element != null).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).map(String::trim).collect(Collectors.toList());
|
||||
List<String> result = nested.stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).map(String::trim).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,10 +1,11 @@
|
||||
// "Replace with forEach" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
class Sample {
|
||||
List<String> foo = new ArrayList<>();
|
||||
{
|
||||
foo.stream().filter(s -> s != null).forEach(System.out::println);
|
||||
foo.stream().filter(Objects::nonNull).forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -2,11 +2,12 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public List<String> test(String[][] arr) {
|
||||
List<String> result = Arrays.stream(arr).filter(subArr -> subArr != null).flatMap(Arrays::stream).collect(Collectors.toList());
|
||||
List<String> result = Arrays.stream(arr).filter(Objects::nonNull).flatMap(Arrays::stream).collect(Collectors.toList());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -2,11 +2,12 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Main {
|
||||
public List<Integer> test(int[][] arr) {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
Arrays.stream(arr).filter(subArr -> subArr != null).forEach(subArr -> {
|
||||
Arrays.stream(arr).filter(Objects::nonNull).forEach(subArr -> {
|
||||
for (int str : subArr) {
|
||||
result.add(str);
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,11 +1,12 @@
|
||||
// "Replace with forEach" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
class Sample {
|
||||
List<String> foo = new ArrayList<>();
|
||||
String foo(){
|
||||
foo.stream().filter(s -> s == null).forEach(s -> {
|
||||
foo.stream().filter(Objects::isNull).forEach(s -> {
|
||||
int i = 0;
|
||||
});
|
||||
return null;
|
||||
|
||||
+2
-1
@@ -1,10 +1,11 @@
|
||||
// "Replace with forEach" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
class Sample {
|
||||
List<String> foo = new ArrayList<>();
|
||||
{
|
||||
foo.stream().filter(s -> s != null).forEach(System.out::println);
|
||||
foo.stream().filter(Objects::nonNull).forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,10 +1,11 @@
|
||||
// "Replace with forEach" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
class Sample {
|
||||
List<String> foo = new ArrayList<>();
|
||||
{
|
||||
foo.stream().filter(s -> s != null).filter(s -> s.startsWith("a")).forEach(System.out::println);
|
||||
foo.stream().filter(Objects::nonNull).filter(s -> s.startsWith("a")).forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,11 +1,12 @@
|
||||
// "Replace with sum()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Main {
|
||||
public double test(String[][] array) {
|
||||
double d = 10;
|
||||
d += Arrays.stream(array).filter(arr -> arr != null).flatMap(Arrays::stream).filter(a -> a.startsWith("xyz")).mapToDouble(a -> 1.0 / a.length()).sum();
|
||||
d += Arrays.stream(array).filter(Objects::nonNull).flatMap(Arrays::stream).filter(a -> a.startsWith("xyz")).mapToDouble(a -> 1.0 / a.length()).sum();
|
||||
return d;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user