@Test void mixedEmptyAndNonEmptyLines() { String text = "line 1\nline 2\r\r\nline 4\n\n"; LineMap map = new LineMap(text); List<LineMap.Line> expected = Arrays.asList( new LineMap.Line(1, 0, "line 1"), new LineMap.Line(2, 7, "line 2"), new LineMap.Line(3, 14, ""), new LineMap.Line(4, 16, "line 4"), new LineMap.Line(5, 23, "") ); assertEquals(expected, map.getSpannedLines(0, text.length())); }
public List<Line> getSpannedLines(final int startPosition, final int length) { if (startPosition < 0 || startPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring start position out of range: " + startPosition); } int endPosition = startPosition + length; if (endPosition < startPosition || endPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring length out of range: " + length); } if (lines.isEmpty()) { return Collections.emptyList(); } final Map.Entry<Integer, LineInfo> firstSpannedLine = lines.floorEntry(startPosition); if (length == 0) { return Collections.singletonList(lineFromMapEntry(firstSpannedLine)); } final SortedMap<Integer, LineInfo> spannedLines = lines.subMap(firstSpannedLine.getKey(), endPosition); return spannedLines.entrySet().stream() .map(this::lineFromMapEntry) .collect(Collectors.toList()); }
LineMap { public List<Line> getSpannedLines(final int startPosition, final int length) { if (startPosition < 0 || startPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring start position out of range: " + startPosition); } int endPosition = startPosition + length; if (endPosition < startPosition || endPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring length out of range: " + length); } if (lines.isEmpty()) { return Collections.emptyList(); } final Map.Entry<Integer, LineInfo> firstSpannedLine = lines.floorEntry(startPosition); if (length == 0) { return Collections.singletonList(lineFromMapEntry(firstSpannedLine)); } final SortedMap<Integer, LineInfo> spannedLines = lines.subMap(firstSpannedLine.getKey(), endPosition); return spannedLines.entrySet().stream() .map(this::lineFromMapEntry) .collect(Collectors.toList()); } }
LineMap { public List<Line> getSpannedLines(final int startPosition, final int length) { if (startPosition < 0 || startPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring start position out of range: " + startPosition); } int endPosition = startPosition + length; if (endPosition < startPosition || endPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring length out of range: " + length); } if (lines.isEmpty()) { return Collections.emptyList(); } final Map.Entry<Integer, LineInfo> firstSpannedLine = lines.floorEntry(startPosition); if (length == 0) { return Collections.singletonList(lineFromMapEntry(firstSpannedLine)); } final SortedMap<Integer, LineInfo> spannedLines = lines.subMap(firstSpannedLine.getKey(), endPosition); return spannedLines.entrySet().stream() .map(this::lineFromMapEntry) .collect(Collectors.toList()); } LineMap(final String text); }
LineMap { public List<Line> getSpannedLines(final int startPosition, final int length) { if (startPosition < 0 || startPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring start position out of range: " + startPosition); } int endPosition = startPosition + length; if (endPosition < startPosition || endPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring length out of range: " + length); } if (lines.isEmpty()) { return Collections.emptyList(); } final Map.Entry<Integer, LineInfo> firstSpannedLine = lines.floorEntry(startPosition); if (length == 0) { return Collections.singletonList(lineFromMapEntry(firstSpannedLine)); } final SortedMap<Integer, LineInfo> spannedLines = lines.subMap(firstSpannedLine.getKey(), endPosition); return spannedLines.entrySet().stream() .map(this::lineFromMapEntry) .collect(Collectors.toList()); } LineMap(final String text); List<Line> getSpannedLines(final int startPosition, final int length); }
LineMap { public List<Line> getSpannedLines(final int startPosition, final int length) { if (startPosition < 0 || startPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring start position out of range: " + startPosition); } int endPosition = startPosition + length; if (endPosition < startPosition || endPosition > text.length()) { throw new StringIndexOutOfBoundsException("Substring length out of range: " + length); } if (lines.isEmpty()) { return Collections.emptyList(); } final Map.Entry<Integer, LineInfo> firstSpannedLine = lines.floorEntry(startPosition); if (length == 0) { return Collections.singletonList(lineFromMapEntry(firstSpannedLine)); } final SortedMap<Integer, LineInfo> spannedLines = lines.subMap(firstSpannedLine.getKey(), endPosition); return spannedLines.entrySet().stream() .map(this::lineFromMapEntry) .collect(Collectors.toList()); } LineMap(final String text); List<Line> getSpannedLines(final int startPosition, final int length);  }