LimitedContainer (PsiMethodCallExpression qualifierCall, PsiExpression mapper) { if (!INT_STREAM_RANGE.test(qualifierCall)) return null; PsiExpression[] rangeArgs = qualifierCall.getArgumentList().getExpressions(); if (!ExpressionUtils.isZero(rangeArgs[0])) return null; PsiExpression bound = ExpressionUtils.resolveExpression(rangeArgs[1]); IndexedContainer container = IndexedContainer.fromLengthExpression(bound); PsiExpression limit = null; if (container == null) { if(bound instanceof PsiMethodCallExpression && MIN_INT.test((PsiMethodCallExpression)bound)) { PsiExpression[] args = ((PsiMethodCallExpression)bound).getArgumentList().getExpressions(); container = IndexedContainer.fromLengthExpression(args[0]); if(container != null) { limit = args[1]; } else { container = IndexedContainer.fromLengthExpression(args[1]); if(container != null) { limit = args[0]; } } } if(container == null) return null; } if (!StreamApiUtil.isSupportedStreamElement(container.getElementType())) return null; LimitedContainer limitedContainer = new LimitedContainer(container, limit); if (mapper instanceof PsiMethodReferenceExpression && container.isGetMethodReference((PsiMethodReferenceExpression)mapper)) { return limitedContainer; } if (mapper instanceof PsiLambdaExpression lambda) { PsiParameter[] parameters = lambda.getParameterList().getParameters(); if (parameters.length != 1) return null; PsiParameter indexParameter = parameters[0]; PsiElement body = lambda.getBody(); if (body == null) return null; Collection<PsiReference> refs = ReferencesSearch.search(indexParameter, new LocalSearchScope(body)).findAll(); if (!refs.isEmpty() && refs.stream().map(ref -> limitedContainer.myContainer.extractGetExpressionFromIndex(tryCast(ref, PsiExpression.class))) .allMatch(expression -> expression != null && !PsiUtil.isAccessedForWriting(expression))) { return limitedContainer; } } return null; }