List<String> (@Nullable String s, int width) { List<String> list = new ArrayList<>(); List<Pair<String, Boolean>> pairs = splitToParagraphs(s); if (pairs == null) { return null; } for (Pair<String, Boolean> pair : pairs) { String seq = pair.getFirst(); boolean isMarked = pair.getSecond(); if (seq.isEmpty()) { // keep empty lines list.add(""); continue; } while (true) { if (seq.length() < width || isMarked) { // keep remaining line and proceed with next paragraph seq = isMarked ? seq : seq.trim(); list.add(seq); break; } else { // wrap paragraph int wrapPos = computeWrapPosition(seq, width); // wrap now if (wrapPos >= seq.length() - 1 || wrapPos < 0) { seq = seq.trim(); list.add(seq); break; } else { list.add(seq.substring(0, wrapPos)); seq = seq.substring(wrapPos + 1); } } } } return list; }