CallHandler<CallChainSimplification> () { return CallHandler.of(COLLECTION_STREAM, call -> { PsiMethodCallExpression maybeNCopies = getQualifierMethodCall(call); if(!N_COPIES.test(maybeNCopies)) return null; PsiExpression[] nCopiesArgs = maybeNCopies.getArgumentList().getExpressions(); PsiExpression count = nCopiesArgs[0]; PsiExpression obj = nCopiesArgs[1]; if(!ExpressionUtils.isSafelyRecomputableExpression(obj)) return null; PsiMethodCallExpression maybeMap = ExpressionUtils.getCallForQualifier(call); if(!STREAM_MAP_TO_ALL.test(maybeMap)) return null; PsiExpression arg = maybeMap.getArgumentList().getExpressions()[0]; PsiLambdaExpression lambda = tryCast(skipParenthesizedExprDown(arg), PsiLambdaExpression.class); if(lambda == null) return null; PsiParameter[] parameters = lambda.getParameterList().getParameters(); if(parameters.length != 1) return null; PsiParameter lambdaVar = parameters[0]; PsiExpression body = tryCast(lambda.getBody(), PsiExpression.class); if (body == null || body.getType() == null) return null; String streamClass = getStreamClassName(maybeMap); if (VariableAccessUtils.variableIsUsed(lambdaVar, body)) return null; return new NCopiesToGenerateStreamFix(streamClass + ".generate(()->" + body.getText() + ").limit(" + count.getText() + ")"); }); }