LengthBasedJoiningTerminal (@NotNull TerminalBlock terminalBlock, @Nullable List<PsiVariable> nonFinalVariables) { if (nonFinalVariables != null && !nonFinalVariables.isEmpty()) return null; List<PsiStatement> statements = List.of(terminalBlock.getStatements()); if (statements.size() < 2) return null; PsiIfStatement ifStatement = tryCast(statements.get(0), PsiIfStatement.class); if (ifStatement == null) return null; PsiExpression condition = ifStatement.getCondition(); if (condition == null || ifStatement.getElseBranch() != null) return null; List<PsiExpression> delimiter = extractDelimiter(ifStatement); if (delimiter == null) return null; List<PsiStatement> withoutCondition = statements.subList(1, statements.size()); PsiVariable targetBuilder = extractStringBuilder(withoutCondition.get(0)); if(!(targetBuilder instanceof PsiLocalVariable)) return null; Integer conditionPrefixLength = extractConditionPrefixLength(condition, targetBuilder); if (conditionPrefixLength == null) return null; List<PsiExpression> mainJoinParts = extractJoinParts(withoutCondition); if (mainJoinParts == null) return null; PsiStatement loop = terminalBlock.getStreamSourceStatement(); PrefixSuffixContext context = PrefixSuffixContext.extractAndVerifyRefs(loop, loop, targetBuilder, terminalBlock, emptyList(), new HashSet<>(emptyList())); if (context == null) return null; String prefix = computeConstant(context.getPrefixJoinParts()); if (prefix == null || prefix.length() != conditionPrefixLength) return null; return new LengthBasedJoiningTerminal(targetBuilder, terminalBlock.getVariable(), mainJoinParts, context, delimiter, terminalBlock); }