### Question:
JsonPathParser { public static List<PathElement> parse(String path) { int elemIdx = 0; int idx = 0; int len = path.length(); ArrayList ret = new ArrayList<PathElement>(); while (idx < len) { char ch = path.charAt(idx); idx += 1; if (ch == '.') { String first = path.substring(elemIdx, idx - 1); elemIdx = idx; ret.add(new PathObjectOrField(first)); } else if (ch == '[') { int arrayIdxStart = idx; Integer out = null; while (idx < len && out == null) { int arrayIdxCh = path.charAt(idx); if (arrayIdxCh == ']') { String arrayIdxStr = path.substring(arrayIdxStart, idx); out = Integer.parseInt(arrayIdxStr); } else if (!(arrayIdxCh >= '0' && arrayIdxCh <= '9')) { throw new IllegalArgumentException("Found unexpected non-digit in middle of array index at " + debugPos(idx)); } idx += 1; } if (out == null) { throw new IllegalArgumentException("Could not find ']' to complete array index"); } String first = path.substring(elemIdx, arrayIdxStart - 1); elemIdx = idx; ret.add(new PathArray(first, out)); if (idx < len && path.charAt(idx) == '.') { idx += 1; elemIdx = idx; } } } if (idx != elemIdx) { String first = path.substring(elemIdx, idx); ret.add(new PathObjectOrField(first)); } return Collections.unmodifiableList(ret); } private  JsonPathParser(); static List<PathElement> parse(String path);  }### Answer:
@Test void name() { assertEquals(JsonPathParser.parse("name"), Collections.singletonList(new PathObjectOrField("name"))); }
@Test void secondElementOfArray() { assertEquals(JsonPathParser.parse("foo[2]"), Collections.singletonList(new PathArray("foo", 2))); }
@Test void fooBar() { assertEquals( JsonPathParser.parse("foo.bar"), Arrays.asList(new PathObjectOrField("foo"), new PathObjectOrField("bar")) ); }
@Test void fooBarSecondItemFromArray() { assertEquals( JsonPathParser.parse("foo.bar[2]"), Arrays.asList(new PathObjectOrField("foo"), new PathArray("bar", 2)) ); }
@Test void secondItemFromFooThenBar() { assertEquals( JsonPathParser.parse("foo[2].bar"), Arrays.asList(new PathArray("foo", 2), new PathObjectOrField("bar")) ); }
@Test void largeIndexFromFooThenBar() { assertEquals( JsonPathParser .parse("foo[9999].bar") , Arrays.asList(new PathArray("foo", 9999), new PathObjectOrField("bar")) ); }
@Test void secondItemFromFooThenBarThenBaz() { assertEquals( JsonPathParser .parse("foo[2].bar[80].baz") , Arrays.asList(new PathArray("foo", 2), new PathArray("bar", 80), new PathObjectOrField("baz")) ); }
@Test void badIdx() { assertThrows(IllegalArgumentException.class, () -> JsonPathParser.parse("foo[bad]")); }
@Test void missingIndexEnd() { assertThrows(IllegalArgumentException.class, () -> JsonPathParser.parse("foo[12")); }
@Test void empty() { assertEquals(JsonPathParser.parse(""), Collections.emptyList()); }