SimpleLine implements Line { public static Line subLine(Line line, int start, int end, boolean preserveLineEnding) { if (start == 0 && (end == line.length() || (preserveLineEnding && end == line.getContentLength()))) { return line; } if (start < 0) { throw new IllegalArgumentException("start cannot be negative: " + start); } if (end < 0) { throw new IllegalArgumentException("end cannot be negative: " + end); } if (start > end) { throw new IllegalArgumentException("start cannot be after end: " + start + " > " + end); } if (preserveLineEnding) { if (end > line.getContentLength()) { throw new IllegalArgumentException("start cannot be after line content end: " + end + " > " + line.getContentLength()); } return new SimpleLine(line.subSequence(start, end), line.getLineEnding()); } else { final int clen = line.getContentLength(); if (end > line.length()) { throw new IllegalArgumentException("start cannot be after line end: " + end + " > " + line.length()); } if (end <= clen) { return new SimpleLine(line.subSequence(start, end), ""); } else { if (start < clen) { return new SimpleLine(line.subSequence(start, clen), line.subSequence(line.getContentLength(), end)); } else { return new SimpleLine("", line.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 testSubLine() { 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(new SimpleLine("", ""), SimpleLine.subLine(line, 0, 0, false)); Assert.assertEquals(new SimpleLine("", CRLF), SimpleLine.subLine(line, 0, 0, true)); Assert.assertEquals(new SimpleLine("0", ""), SimpleLine.subLine(line, 0, 1, false)); Assert.assertEquals(new SimpleLine("0", CRLF), SimpleLine.subLine(line, 0, 1, true)); Assert.assertEquals(new SimpleLine("1", ""), SimpleLine.subLine(line, 1, 2, false)); Assert.assertEquals(new SimpleLine("1", CRLF), SimpleLine.subLine(line, 1, 2, true)); Assert.assertEquals(new SimpleLine("9", ""), SimpleLine.subLine(line, 9, 10, false)); Assert.assertEquals(new SimpleLine("9", CRLF), SimpleLine.subLine(line, 9, 10, true)); Assert.assertEquals(new SimpleLine("9", "" + Line.CR), SimpleLine.subLine(line, 9, 11, false)); Assert.assertEquals(new SimpleLine("9", CRLF), SimpleLine.subLine(line, 9, 12, false)); Assert.assertEquals(new SimpleLine("", "" + Line.CR), SimpleLine.subLine(line, 10, 11, false)); Assert.assertEquals(new SimpleLine("", CRLF), SimpleLine.subLine(line, 10, 12, false)); Assert.assertEquals(new SimpleLine("", "" + Line.LF), SimpleLine.subLine(line, 11, 12, false)); 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) { } } }