@Test void emptySubstrings() { String text = "single line\n"; LineMap map = new LineMap(text); List<LineMap.Line> expected = Collections.singletonList( new LineMap.Line(1, 0, "single line") ); for (int start = 0; start <= text.length(); start++) { assertEquals(expected, map.getSpannedLines(start, 0)); } }
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);  }