mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
improve codegen in simple case
This commit is contained in:
+9
-2
@@ -243,6 +243,13 @@ class FindExtremumMigration extends BaseStreamApiMigration {
|
||||
boolean isNegated);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getLambdaText(PsiExpression loopVarExpression, TerminalBlock terminalBlock) {
|
||||
return ExpressionUtils.isReferenceTo(loopVarExpression, terminalBlock.getVariable())
|
||||
? CommonClassNames.JAVA_UTIL_COMPARATOR + ".naturalOrder()"
|
||||
: LambdaUtil.createLambda(terminalBlock.getVariable(), loopVarExpression);
|
||||
}
|
||||
|
||||
//Person maxPerson = null;
|
||||
//int maxAge = 0;
|
||||
//for (Person person : personList) {
|
||||
@@ -294,7 +301,7 @@ class FindExtremumMigration extends BaseStreamApiMigration {
|
||||
TerminalBlock blockWithFilter =
|
||||
myTerminalBlock.add(new StreamApiMigrationInspection.FilterOp(condition, myTerminalBlock.getVariable(), false));
|
||||
|
||||
String lambdaText = LambdaUtil.createLambda(myTerminalBlock.getVariable(), myExtremumKeyExpr);
|
||||
String lambdaText = getLambdaText(myExtremumKeyExpr, myTerminalBlock);
|
||||
String comparator;
|
||||
if(myComparator == null) {
|
||||
comparator = CommonClassNames.JAVA_UTIL_COMPARATOR + "." + method + "(" + lambdaText + ")";
|
||||
@@ -561,7 +568,7 @@ class FindExtremumMigration extends BaseStreamApiMigration {
|
||||
if(myComparator == null) {
|
||||
String method = getComparingMethod(loopVarExpressionType);
|
||||
if (method == null) return null;
|
||||
String lambdaText = LambdaUtil.createLambda(myTerminalBlock.getVariable(), myLoopVarExpression);
|
||||
String lambdaText = getLambdaText(myLoopVarExpression, myTerminalBlock);
|
||||
comparator = CommonClassNames.JAVA_UTIL_COMPARATOR + "." + method + "(" + lambdaText + ")";
|
||||
} else {
|
||||
String comparatorName = myComparator.getName();
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
// "Replace with min()" "true"
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
static class Person implements Comparable<Person> {
|
||||
String name;
|
||||
int age;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
||||
public Person work() {
|
||||
List<Person> personList = Arrays.asList(
|
||||
new Person("Roman", 21),
|
||||
new Person("James", 25),
|
||||
new Person("Kelly", 12)
|
||||
);
|
||||
Person minPerson = personList.stream().filter(p -> p.getAge() > 13).min(Comparator.comparing(Comparator.naturalOrder())).orElse(null);
|
||||
|
||||
return minPerson;
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
// "Replace with min()" "true"
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
static class Person implements Comparable<Person> {
|
||||
String name;
|
||||
int age;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
||||
public Person work() {
|
||||
List<Person> personList = Arrays.asList(
|
||||
new Person("Roman", 21),
|
||||
new Person("James", 25),
|
||||
new Person("Kelly", 12)
|
||||
);
|
||||
Person minPerson = null;
|
||||
for <caret>(Person p : personList) {
|
||||
if(p.getAge() > 13) {
|
||||
if (minPerson == null || minPerson.compareTo(p) > 0) {
|
||||
minPerson = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return minPerson;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user