@Override
    String generate(ChainVariable inVar, StreamToLoopReplacementContext context) {
      if (getExtremeValue() != null && context.tryUnwrapOrElse(getExtremeValue())) {
        String best = context.declareResult("best", myType, getExtremeValueExpression(), ResultKind.NON_FINAL);
        String comparePredicate = myTemplate.replace("{best}", best).replace("{item}", inVar.getName());
        return "if(" + comparePredicate + ")\n" + best + "=" + inVar + ";\n";
      }
      String seen = context.declare("seen", "boolean", "false");
      String best = context.declareResult("best", myType, myType instanceof PsiPrimitiveType ? "0" : "null", ResultKind.UNKNOWN);
      context.setFinisher(new ConditionalExpression.Optional(myType, seen, best));
      String comparePredicate;
      if(myComparator != null) {
        myComparator.transform(context, inVar.getName(), best);
        PsiExpression expression = myComparator.getExpression();
        int expressionPrecedence = ParenthesesUtils.getPrecedence(expression);
        String text = expressionPrecedence >= ParenthesesUtils.EQUALITY_PRECEDENCE ? "("+expression.getText()+")" : expression.getText();
        comparePredicate = myTemplate.replace("{comparator}", text);
      } else {
        comparePredicate = myTemplate.replace("{best}", best).replace("{item}", inVar.getName());
      }
      return "if(!" + seen + " || " + comparePredicate + ") {\n" +
             seen + "=true;\n" +
             best + "=" + inVar + ";\n}\n";
    }
@Override
    String generate(ChainVariable inVar, StreamToLoopReplacementContext context)