PsiMethodCallExpression (PsiMethodCallExpression call, Predicate<? super String> isWantedCall, Predicate<? super PsiMethodCallExpression> isWantedCollector, Predicate<? super String> isAllowedIntermediateCall) { for (PsiMethodCallExpression chainCall = getCallForQualifier(call); chainCall != null; chainCall = getCallForQualifier(chainCall)) { String name = chainCall.getMethodExpression().getReferenceName(); if (name == null) return null; if (isWantedCall.test(name)) return chainCall; if (name.equals("collect")) { PsiExpression[] args = chainCall.getArgumentList().getExpressions(); if (args.length == 1) { PsiMethodCallExpression collectorCall = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(args[0]), PsiMethodCallExpression.class); if (collectorCall != null && isWantedCollector.test(collectorCall)) { return collectorCall; } } } if (!isAllowedIntermediateCall.test(name) || !InheritanceUtil.isInheritor(chainCall.getType(), CommonClassNames.JAVA_UTIL_STREAM_BASE_STREAM)) { return null; } } return null; }