mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
StreamToLoop: add finisher parentheses automatically if necessary
This commit is contained in:
+9
-3
@@ -33,6 +33,7 @@ import com.intellij.psi.impl.PsiDiamondTypeUtil;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import com.siyeh.ig.psiutils.StreamApiUtil;
|
||||
import one.util.streamex.IntStreamEx;
|
||||
import one.util.streamex.StreamEx;
|
||||
@@ -252,7 +253,12 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
temporaryStreamPlaceholder.delete();
|
||||
}
|
||||
else {
|
||||
normalize(project, temporaryStreamPlaceholder.replace(factory.createExpressionFromText(finisher, temporaryStreamPlaceholder)));
|
||||
PsiExpression expression = factory.createExpressionFromText(finisher, temporaryStreamPlaceholder);
|
||||
PsiElement parent = temporaryStreamPlaceholder.getParent();
|
||||
if (parent instanceof PsiExpression && ParenthesesUtils.areParenthesesNeeded(expression, (PsiExpression)parent, false)) {
|
||||
expression = factory.createExpressionFromText("("+expression.getText()+")", temporaryStreamPlaceholder);
|
||||
}
|
||||
normalize(project, temporaryStreamPlaceholder.replace(expression));
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
@@ -430,8 +436,8 @@ public class StreamToLoopInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
|
||||
public void setOptionalUnwrapperFinisher(String seenVariable, String accVariable, String type) {
|
||||
String optionalClass = OptionalUtil.getOptionalClass(type);
|
||||
setFinisher("(" + seenVariable + "?" + optionalClass + ".of(" + accVariable + "):" + optionalClass +
|
||||
"." + (TypeConversionUtil.isPrimitive(type) ? "" : "<" + type + ">") + "empty())");
|
||||
setFinisher(seenVariable + "?" + optionalClass + ".of(" + accVariable + "):" + optionalClass +
|
||||
"." + (TypeConversionUtil.isPrimitive(type) ? "" : "<" + type + ">") + "empty()");
|
||||
}
|
||||
|
||||
public Project getProject() {
|
||||
|
||||
@@ -398,9 +398,9 @@ abstract class TerminalOperation extends Operation {
|
||||
String emptyCheck = count + "==0";
|
||||
String result = (myDoubleAccumulator ? "" : "(double)") + sum + "/" + count;
|
||||
context.setFinisher(myUseOptional
|
||||
? "(" + emptyCheck + "?java.util.OptionalDouble.empty():"
|
||||
+ "java.util.OptionalDouble.of(" + result + "))"
|
||||
: "(" + emptyCheck + "?0.0:" + result + ")");
|
||||
? emptyCheck + "?java.util.OptionalDouble.empty():"
|
||||
+ "java.util.OptionalDouble.of(" + result + ")"
|
||||
: emptyCheck + "?0.0:" + result);
|
||||
return sum + "+=" + inVar + ";\n" + count + "++;\n";
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return (count == 0 ? OptionalDouble.empty() : OptionalDouble.of(sum / count));
|
||||
return count == 0 ? OptionalDouble.empty() : OptionalDouble.of(sum / count);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return (count == 0 ? OptionalDouble.empty() : OptionalDouble.of((double) sum / count));
|
||||
return count == 0 ? OptionalDouble.empty() : OptionalDouble.of((double) sum / count);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return (count == 0 ? OptionalDouble.empty() : OptionalDouble.of((double) sum / count));
|
||||
return count == 0 ? OptionalDouble.empty() : OptionalDouble.of((double) sum / count);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
System.out.println((count == 0 ? 0.0 : sum / count));
|
||||
System.out.println(count == 0 ? 0.0 : sum / count);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
System.out.println((count == 0 ? 0.0 : (double) sum / count));
|
||||
System.out.println(count == 0 ? 0.0 : (double) sum / count);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ public class Main {
|
||||
}
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(best) : Optional.empty());
|
||||
return seen ? Optional.of(best) : Optional.empty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ public class Main {
|
||||
acc = acc + s;
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(acc) : Optional.empty());
|
||||
return seen ? Optional.of(acc) : Optional.empty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ public class Main {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return (count == 0 ? 0.0 : (double) sum / count);
|
||||
return count == 0 ? 0.0 : (double) sum / count;
|
||||
};
|
||||
System.out.println(s.getAsDouble());
|
||||
}
|
||||
|
||||
+3
-5
@@ -1,11 +1,9 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
public static double test(List<String> strings) {
|
||||
public static OptionalDouble test(List<String> strings) {
|
||||
boolean seen = false;
|
||||
double best = 0;
|
||||
for (String string : strings) {
|
||||
@@ -15,7 +13,7 @@ public class Main {
|
||||
best = length;
|
||||
}
|
||||
}
|
||||
return (seen ? OptionalDouble.of(best) : OptionalDouble.empty()).orElse(-1);
|
||||
return seen ? OptionalDouble.of(best) : OptionalDouble.empty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ public class Main {
|
||||
acc = acc * i;
|
||||
}
|
||||
}
|
||||
return (seen ? OptionalInt.of(acc) : OptionalInt.empty());
|
||||
return seen ? OptionalInt.of(acc) : OptionalInt.empty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ public class Main {
|
||||
acc = acc * number;
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(acc) : Optional.empty());
|
||||
return seen ? Optional.of(acc) : Optional.empty();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+3
-4
@@ -1,11 +1,10 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
public static double test(List<String> strings) {
|
||||
return strings.stream().mapToDouble(String::length).m<caret>ax().orElse(-1);
|
||||
public static OptionalDouble test(List<String> strings) {
|
||||
return strings.stream().mapToDouble(String::length).m<caret>ax();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Reference in New Issue
Block a user