final String commentStart,
final String commentEnd,
final String leading,
final int maxLineLengh) {
assert CharSequenceUtils.isNotBlank(text) && CharSequenceUtils.isNotBlank(commentStart) && CharSequenceUtils.isNotBlank(commentEnd);
final EnhancedStringBuilder builder = new EnhancedStringBuilder(text).deletePattern("\r");
final Collection<MatchEntry> newLines = builder.findPattern("\n");
for (MatchEntry match : newLines) {
if (match.getStart() > 0 && isPeriodFinalization(builder.charAt(match.getStart() - 1))
&& '\n' != builder.charAt(match.getStart() + 1)) {
/* new lines not after a punctuation are removed */
builder.replace(match, " ");
}
}
builder.replacePattern(" +", " ");
final Collection<String> lines = builder.split("\n");
builder.clear().appendln(commentStart);
int max = maxLineLengh - leading.length();
for (CharSequence line : lines) {
if (line.length() <= max) {
// short line, just append the leading txt and the line itself
builder.append(leading).appendln(line);
} else {
// long line, we need to break it down
final List<String> subLines = breakdownLine(line.toString(), max);
for (String subLine : subLines) {
builder.append(leading).appendln(subLine);
}
}
}
return builder.appendln(commentEnd);
}