URIPath { public Optional<String> getPart(int index) { String leftover = removePartsBefore(path, index); if (leftover.isEmpty()) return Optional.empty(); leftover = leftover.substring(1); int nextSlash = leftover.indexOf('/'); if (nextSlash == -1) { return Optional.of(leftover); } else { return Optional.of(leftover.substring(0, nextSlash)); } } URIPath(String path); String asString(); Optional<String> getPart(int index); boolean endsWith(String suffix); URIPath omitFirst(); int length(); @Override String toString();  }
@Test void firstPartEmptyForRoot() { URIPath target = new URIPath("/"); Optional<String> expected = Optional.of(""); Optional<String> result = target.getPart(0); assertEquals(expected, result); }
@Test void firstPart() { URIPath target = new URIPath("/example/target"); Optional<String> expected = Optional.of("example"); Optional<String> result = target.getPart(0); assertEquals(expected, result); }
@Test void secondPart() { URIPath target = new URIPath("/example/target"); Optional<String> expected = Optional.of("target"); Optional<String> result = target.getPart(1); assertEquals(expected, result); }
@Test void noPart() { URIPath target = new URIPath("/example/target"); Optional<String> expected = Optional.empty(); Optional<String> result = target.getPart(2); assertEquals(expected, result); }
@Test void emptyLastPart() { URIPath target = new URIPath("/example/target/"); Optional<String> expected = Optional.of(""); Optional<String> result = target.getPart(2); assertEquals(expected, result); }