ImmutableJsonPointer implements JsonPointer { public static ImmutableJsonPointer of(final JsonKey rootLevel, final JsonKey... subLevels) { checkRootLevel(rootLevel); requireNonNull(subLevels, "The sub levels must not be null!" + " If the JSON pointer does not require sub levels, just omit this argument."); final ImmutableJsonPointer result; final List<JsonKey> keyHierarchy = new ArrayList<>(1 + subLevels.length); keyHierarchy.add(rootLevel); Collections.addAll(keyHierarchy, subLevels); result = newInstance(keyHierarchy); return result; } private  ImmutableJsonPointer(final List<JsonKey> theJsonKeys); static ImmutableJsonPointer empty(); static JsonPointer ofParsed(final CharSequence slashDelimitedCharSequence); static ImmutableJsonPointer of(final JsonKey rootLevel, final JsonKey... subLevels); @Override ImmutableJsonPointer addLeaf(final JsonKey key); @Override ImmutableJsonPointer append(final JsonPointer subPointer); @Override int getLevelCount(); @Override boolean isEmpty(); @Override Optional<JsonKey> get(final int level); @Override Optional<JsonKey> getRoot(); @Override Optional<JsonKey> getLeaf(); @SuppressWarnings("squid:S1166") @Override Optional<JsonPointer> getSubPointer(final int level); @Override Optional<JsonPointer> getPrefixPointer(final int level); @Override ImmutableJsonPointer cutLeaf(); @Override JsonPointer nextLevel(); @Override JsonFieldSelector toFieldSelector(); @Override Iterator<JsonKey> iterator(); @Override boolean equals(final Object o); @Override int hashCode(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); @Override String toString();  }
@Test public void iterationWorksAsExpected() { final JsonKey first = JsonFactory.newKey("first"); final JsonKey second = JsonFactory.newKey("second"); final JsonKey third = JsonFactory.newKey("third"); final List<JsonKey> knownLevels = Arrays.asList(first, second, third); final List<JsonKey> levelsFromIteration = new ArrayList<>(); final JsonPointer underTest = ImmutableJsonPointer.of(first, second, third); for (final JsonKey jsonKey : underTest) { levelsFromIteration.add(jsonKey); } assertThat(levelsFromIteration).containsExactly(knownLevels.toArray(new JsonKey[knownLevels.size()])); }