ImmutableJsonPointer implements JsonPointer { public static JsonPointer ofParsed(final CharSequence slashDelimitedCharSequence) { requireNonNull(slashDelimitedCharSequence, "The JSON pointer character sequence must not be null!"); final JsonPointer result; if (JsonPointer.class.isAssignableFrom(slashDelimitedCharSequence.getClass())) { result = (JsonPointer) slashDelimitedCharSequence; } else if (JsonKey.class.isAssignableFrom(slashDelimitedCharSequence.getClass())) { result = newInstance(Collections.singletonList(((JsonKey) slashDelimitedCharSequence))); } else if (0 == slashDelimitedCharSequence.length()) { result = empty(); } else if (DOUBLE_SLASH_PATTERN.matcher(slashDelimitedCharSequence).find()) { throw JsonPointerInvalidException.newBuilderForConsecutiveSlashes(slashDelimitedCharSequence) .build(); } else { final List<JsonKey> jsonKeys = Stream.of(SINGLE_SLASH_REGEX_PATTERN.split(slashDelimitedCharSequence)) .filter(keyName -> !keyName.isEmpty()) .map(ImmutableJsonPointer::decodeTilde) .map(JsonFactory::newKey) .collect(toList()); result = newInstance(jsonKeys); } 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 createInstanceFromStringWithLeadingSlash() { final JsonPointer underTest = ImmutableJsonPointer.ofParsed("/foo/bar/baz"); final byte expectedLevelCount = 3; assertThat(underTest).hasLevelCount(expectedLevelCount); }
@Test public void createInstanceFromStringWithTrailingSlash() { final JsonPointer underTest = ImmutableJsonPointer.ofParsed("/foo/bar/baz/"); final byte expectedLevelCount = 3; assertThat(underTest).hasLevelCount(expectedLevelCount); }
@Test(expected = NullPointerException.class) public void tryToParseNullString() { ImmutableJsonPointer.ofParsed(null); }
@Test public void createPointerWithOrWithoutLeadingSlashIsTheSame() { final JsonPointer without = ImmutableJsonPointer.ofParsed("foo/bar"); final JsonPointer with = ImmutableJsonPointer.ofParsed("/foo/bar"); assertThat(without).isEqualTo(with); }