CallHandler<CallChainSimplification> () { return CallHandler.of(STREAM_FIND_FIRST, call -> { PsiMethodCallExpression maybeSorted = getQualifierMethodCall(call); if (!STREAM_SORTED.test(maybeSorted)) return null; PsiExpression[] args = maybeSorted.getArgumentList().getExpressions(); PsiExpression qualifier = maybeSorted.getMethodExpression().getQualifierExpression(); if (qualifier == null) return null; final String comparator; boolean reversed = false; if (args.length == 1) { PsiExpression maybeComparator = skipParenthesizedExprDown(args[0]); if (maybeComparator instanceof PsiMethodCallExpression && COMPARATOR_REVERSED.test((PsiMethodCallExpression)maybeComparator)) { PsiExpression comparatorQualifier = ((PsiMethodCallExpression)maybeComparator).getMethodExpression().getQualifierExpression(); if(comparatorQualifier == null) return null; comparator = comparatorQualifier.getText(); reversed = true; } else { if (maybeComparator == null) return null; PsiType comparatorType = FunctionalExpressionUtils.getFunctionalExpressionType(maybeComparator); if (!InheritanceUtil.isInheritor(comparatorType, JAVA_UTIL_COMPARATOR)) return null; comparator = maybeComparator.getText(); } } else return null; String methodName = reversed ? "max" : "min"; return new SortedFirstToMinMaxFix(methodName, qualifier.getText() + "." + methodName + "(" + comparator + ")"); }); }