SimpleLine implements Line { @Override public CharSequence subSequence(int start, int end) { final int clen = content.length(); final int elen = lineEnding.length(); if (start < clen && end <= clen) { return content.subSequence(start, end); } final int estart = start - clen; final int eend = end - clen; if (estart >= 0 && estart <= elen && eend >= 0 && eend <= elen) { return lineEnding.subSequence(estart, eend); } final StringBuilder sb = new StringBuilder(clen + elen); return sb.append(content).append(lineEnding).subSequence(start, end); } SimpleLine(CharSequence content);  SimpleLine(CharSequence content, char lineEnding);  SimpleLine(CharSequence content, CharSequence lineEnding); @Override int length(); @Override char charAt(int index); @Override CharSequence subSequence(int start, int end); @Override String getContent(); @Override int getContentLength(); @Override String getLineEnding(); @Override int getLineEndingLength(); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj); static Line subLine(Line line, int start, int end, boolean preserveLineEnding);  }
@Test public void testSubSequence() { final String CRLF = "" + Line.CR + Line.LF; final Line line = new SimpleLine("0123456789", CRLF); Assert.assertSame(line, SimpleLine.subLine(line, 0, line.length(), false)); Assert.assertSame(line, SimpleLine.subLine(line, 0, line.length(), true)); Assert.assertEquals("", line.subSequence(0, 0)); Assert.assertEquals("0", line.subSequence(0, 1)); Assert.assertEquals("01", line.subSequence(0, 2)); Assert.assertEquals("012", line.subSequence(0, 3)); Assert.assertEquals("0123", line.subSequence(0, 4)); Assert.assertEquals("01234", line.subSequence(0, 5)); Assert.assertEquals("012345", line.subSequence(0, 6)); Assert.assertEquals("0123456", line.subSequence(0, 7)); Assert.assertEquals("01234567", line.subSequence(0, 8)); Assert.assertEquals("012345678", line.subSequence(0, 9)); Assert.assertEquals("0123456789", line.subSequence(0, 10)); Assert.assertEquals("0123456789" + Line.CR, line.subSequence(0, 11)); Assert.assertEquals("0123456789" + CRLF, line.subSequence(0, 12)); Assert.assertEquals("123456789" + CRLF, line.subSequence(1, 12)); Assert.assertEquals("23456789" + CRLF, line.subSequence(2, 12)); Assert.assertEquals("3456789" + CRLF, line.subSequence(3, 12)); Assert.assertEquals("456789" + CRLF, line.subSequence(4, 12)); Assert.assertEquals("56789" + CRLF, line.subSequence(5, 12)); Assert.assertEquals("6789" + CRLF, line.subSequence(6, 12)); Assert.assertEquals("789" + CRLF, line.subSequence(7, 12)); Assert.assertEquals("89" + CRLF, line.subSequence(8, 12)); Assert.assertEquals("9" + CRLF, line.subSequence(9, 12)); Assert.assertEquals("" + CRLF, line.subSequence(10, 12)); Assert.assertEquals("" + Line.LF, line.subSequence(11, 12)); Assert.assertEquals("", line.subSequence(12, 12)); Assert.assertEquals("1", line.subSequence(1, 2)); Assert.assertEquals("9", line.subSequence(9, 10)); Assert.assertEquals("9" + Line.CR, line.subSequence(9, 11)); Assert.assertEquals("9" + CRLF, line.subSequence(9, 12)); Assert.assertEquals("" + Line.CR, line.subSequence(10, 11)); Assert.assertEquals(CRLF, line.subSequence(10, 12)); Assert.assertEquals("" + Line.LF, line.subSequence(11, 12)); final int[] start= {-1, 0, 1, 0, 0}; final int[] end = { 0, -1, 0, line.getContentLength()+1, line.length()+1}; final boolean[] preserveEnding= { true, true, true, true, false}; for (int i = 0; i < start.length; i++) { try { SimpleLine.subLine(line, start[i], end[i], preserveEnding[i]); Assert.fail("expected exception for illegal subLine input arguments: line.subLine(" + start[i] + ", " + end[i] + ", " + preserveEnding[i] + ") for line " + line); } catch (IllegalArgumentException e) { } } }