boolean (@NotNull PsiExpression optionalExpression, @NotNull PsiExpression previousExpression, boolean negated) { PsiPolyadicExpression parent = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprUp(previousExpression.getParent()), PsiPolyadicExpression.class); if (parent == null) return false; IElementType expectedToken = negated ? JavaTokenType.OROR : JavaTokenType.ANDAND; if (!parent.getOperationTokenType().equals(expectedToken)) return false; PsiExpression nextExpression = StreamEx.of(parent.getOperands()).dropWhile(op -> !PsiTreeUtil.isAncestor(op, previousExpression, false)) .skip(1) .findFirst() .orElse(null); nextExpression = PsiUtil.skipParenthesizedExprDown(nextExpression); if (nextExpression == null) return false; if (negated) { if (!BoolUtils.isNegation(nextExpression)) return false; nextExpression = BoolUtils.getNegated(nextExpression); } if (!(nextExpression instanceof PsiMethodCallExpression call)) return false; if (!"isPresent".equals(call.getMethodExpression().getReferenceName()) || !call.getArgumentList().isEmpty()) return false; PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); return qualifier != null && PsiEquivalenceUtil.areElementsEquivalent(qualifier, optionalExpression); }