TextBufferImpl implements TextBuffer { @Override public Position write(Position position, String text) { List<String> linesToInsert = new LinkedList<>(splitString(text, true, false)); if (linesToInsert.isEmpty()) { return position; } linesToInsert.set(0, getLinePrefix(position) + linesToInsert.get(0)); String lastLine = linesToInsert.get(linesToInsert.size() - 1); int endColumnIndex = lastLine.length(); linesToInsert.set(linesToInsert.size() - 1, lastLine + getLineSuffix(position)); int lineIndex = position.lineIndex(); int endLineIndex = lineIndex + linesToInsert.size() - 1; lines.set(lineIndex, linesToInsert.remove(0)); lines.addAll(lineIndex + 1, linesToInsert); incrementVersion(); return new Position(endLineIndex, endColumnIndex); } TextBufferImpl();   TextBufferImpl(String text); @Override Version getVersion(); @Override String getString(); @Override int getMaxLineIndex(); @Override String getLine(int lineIndex); @Override Position write(Position position, String text); @Override void clear(); @Override void delete(Position start, Position end);  }
@Test public void testWrite() { assertEquals("", textBuffer.getString()); assertWrite(2, 0, "foo\nbar\n", 0, 0, "foo\nbar\n"); assertWrite(1, 6, "fofirst\nsecondo\nbar\n", 0, 2, "first\nsecond"); assertWrite(3, 1, "fofirst\nsecondo\nbar\na", 3, 0, "a"); assertWrite(4, 0, "fofirst\nsecondo\nbar\na\n", 3, 1, "\n"); }