@NotNull
  private static PsiIfStatement createAndAnd(@NotNull PsiElementFactory factory,
                                             @NotNull PsiStatement thenBranch,
                                             @Nullable PsiStatement elseBranch,
                                             @NotNull PsiExpression extract,
                                             @NotNull PsiExpression leave,
                                             CommentTracker tracker) {
    List<String> elseChain = new ArrayList<>();
    boolean chainFinished = false;
    loop:
    while (!chainFinished) {
      PsiIfStatement nextIf = tryCast(ControlFlowUtils.stripBraces(elseBranch), PsiIfStatement.class);
      if (nextIf == null) break;
      PsiExpression nextCondition = PsiUtil.skipParenthesizedExprDown(nextIf.getCondition());
      if (nextCondition == null) break;
      EquivalenceChecker equivalence = EquivalenceChecker.getCanonicalPsiEquivalence();
      if (nextIf.getThenBranch() == null) break;
      if (equivalence.expressionsAreEquivalent(extract, nextCondition)) {
        elseChain.add(tracker.text(nextIf.getThenBranch()));
        chainFinished = true;
      }
      else if (nextIf.getElseBranch() == null && equivalence.expressionsAreEquivalent(
        extract, factory.createExpressionFromText(BoolUtils.getNegatedExpressionText(nextCondition), nextCondition))) {
        // skip duplicating else branch in cases like
        // if(foo && bar) {1} else if (!foo) {2} =>
        // if(foo) { if(bar) {1} } else {2}
        elseBranch = nextIf.getThenBranch();
        chainFinished = true;
        break;
      }
      else {
        if (!(nextCondition instanceof PsiPolyadicExpression nextPolyadic)) break;
        if (!nextPolyadic.getOperationTokenType().equals(JavaTokenType.ANDAND)) break;
        PsiExpression[] nextOperands = nextPolyadic.getOperands();
        PsiExpression[] operands;
        if (extract instanceof PsiPolyadicExpression &&
            ((PsiPolyadicExpression)extract).getOperationTokenType().equals(JavaTokenType.ANDAND)) {
          operands = ((PsiPolyadicExpression)extract).getOperands();
        }
        else {
          operands = new PsiExpression[]{extract};
        }
        if (nextOperands.length <= operands.length) break;
        for (int i = 0; i < operands.length; i++) {
          if (!PsiEquivalenceUtil.areElementsEquivalent(nextOperands[i], operands[i])) break loop;
        }
        PsiExpression nextExtracted =
          getROperands(nextPolyadic, nextPolyadic.getTokenBeforeOperand(nextOperands[operands.length]), tracker);
        elseChain.add(createIfString(nextExtracted, nextIf.getThenBranch(), (PsiStatement)null, tracker));
      }
      elseBranch = nextIf.getElseBranch();
    }
    if (!chainFinished && elseBranch != null) {
      elseChain.add(elseBranch.getText());
    }
    String thenString;
    if (elseChain.isEmpty()) {
      thenString = createIfString(leave, thenBranch, (String)null, tracker);
      if (elseBranch != null) {
        thenString = "{" + thenString + "}";
      }
    }
    else {
      thenString = "{" + createIfString(leave, thenBranch, String.join("\n" + PsiKeyword.ELSE + " ", elseChain), tracker) + "\n}";
    }
    String ifString = createIfString(extract, thenString, elseBranch, tracker);
    return (PsiIfStatement)factory.createStatementFromText(ifString, thenBranch);
  }
@NotNull
  private static PsiIfStatement createAndAnd(@NotNull PsiElementFactory factory,
                                             @NotNull PsiStatement thenBranch,
                                             @Nullable PsiStatement elseBranch,
                                             @NotNull PsiExpression extract,
                                             @NotNull PsiExpression leave,
                                             CommentTracker tracker)