@Test public void testFindPathAnchorFromBottomIgnoreIsSkipped() { PathElement element0 = new PathElement(NODE_NAME, "label", 0, 0); PathElement element1 = new PathElement(IGNORE, "label", 1, 0); PathElement element2 = new PathElement(null, "label", 2, 0); PathElement element3 = new PathElement(IGNORE, "label", 3, 0); PathElement element4 = new PathElement(IGNORE, "label", 4, 0); assertThat( findPathAnchorFromBottom( ImmutableList.of(element0, element1, element2, element3, element4), 0), equalTo(Optional.of(element0))); }
@VisibleForTesting static Optional<PathElement> findPathAnchorFromBottom(List<PathElement> pathElements, int level) { if (level < 0) { return Optional.empty(); } PathElement element = pathElements.get(level); if (level > 0) { Optional<PathElement> ignoreAncestor = findAncestorOfType(pathElements, level - 1, IGNORE); if (ignoreAncestor.isPresent()) { return findPathAnchorFromBottom(pathElements, ignoreAncestor.get().getLevel() - 1); } } if (element.getAnchorType() == CHAR_LITERAL) { if (level > 0) { if (pathElements.get(level - 1).getAnchorType() == STRING_LITERAL) { return findPathAnchorFromBottom(pathElements, level - 1); } Optional<PathElement> enumSetValueAncestor = findAncestorOfType(pathElements, level - 1, ENUM_SET_VALUE); if (enumSetValueAncestor.isPresent()) { return enumSetValueAncestor; } return Optional.of(element); } } else if (element.getAnchorType() != null && element.getAnchorType() != IGNORE) { return Optional.of(element); } return findPathAnchorFromBottom(pathElements, level - 1); }
ParserUtils { @VisibleForTesting static Optional<PathElement> findPathAnchorFromBottom(List<PathElement> pathElements, int level) { if (level < 0) { return Optional.empty(); } PathElement element = pathElements.get(level); if (level > 0) { Optional<PathElement> ignoreAncestor = findAncestorOfType(pathElements, level - 1, IGNORE); if (ignoreAncestor.isPresent()) { return findPathAnchorFromBottom(pathElements, ignoreAncestor.get().getLevel() - 1); } } if (element.getAnchorType() == CHAR_LITERAL) { if (level > 0) { if (pathElements.get(level - 1).getAnchorType() == STRING_LITERAL) { return findPathAnchorFromBottom(pathElements, level - 1); } Optional<PathElement> enumSetValueAncestor = findAncestorOfType(pathElements, level - 1, ENUM_SET_VALUE); if (enumSetValueAncestor.isPresent()) { return enumSetValueAncestor; } return Optional.of(element); } } else if (element.getAnchorType() != null && element.getAnchorType() != IGNORE) { return Optional.of(element); } return findPathAnchorFromBottom(pathElements, level - 1); } }
ParserUtils { @VisibleForTesting static Optional<PathElement> findPathAnchorFromBottom(List<PathElement> pathElements, int level) { if (level < 0) { return Optional.empty(); } PathElement element = pathElements.get(level); if (level > 0) { Optional<PathElement> ignoreAncestor = findAncestorOfType(pathElements, level - 1, IGNORE); if (ignoreAncestor.isPresent()) { return findPathAnchorFromBottom(pathElements, ignoreAncestor.get().getLevel() - 1); } } if (element.getAnchorType() == CHAR_LITERAL) { if (level > 0) { if (pathElements.get(level - 1).getAnchorType() == STRING_LITERAL) { return findPathAnchorFromBottom(pathElements, level - 1); } Optional<PathElement> enumSetValueAncestor = findAncestorOfType(pathElements, level - 1, ENUM_SET_VALUE); if (enumSetValueAncestor.isPresent()) { return enumSetValueAncestor; } return Optional.of(element); } } else if (element.getAnchorType() != null && element.getAnchorType() != IGNORE) { return Optional.of(element); } return findPathAnchorFromBottom(pathElements, level - 1); }  }
ParserUtils { @VisibleForTesting static Optional<PathElement> findPathAnchorFromBottom(List<PathElement> pathElements, int level) { if (level < 0) { return Optional.empty(); } PathElement element = pathElements.get(level); if (level > 0) { Optional<PathElement> ignoreAncestor = findAncestorOfType(pathElements, level - 1, IGNORE); if (ignoreAncestor.isPresent()) { return findPathAnchorFromBottom(pathElements, ignoreAncestor.get().getLevel() - 1); } } if (element.getAnchorType() == CHAR_LITERAL) { if (level > 0) { if (pathElements.get(level - 1).getAnchorType() == STRING_LITERAL) { return findPathAnchorFromBottom(pathElements, level - 1); } Optional<PathElement> enumSetValueAncestor = findAncestorOfType(pathElements, level - 1, ENUM_SET_VALUE); if (enumSetValueAncestor.isPresent()) { return enumSetValueAncestor; } return Optional.of(element); } } else if (element.getAnchorType() != null && element.getAnchorType() != IGNORE) { return Optional.of(element); } return findPathAnchorFromBottom(pathElements, level - 1); }   }
ParserUtils { @VisibleForTesting static Optional<PathElement> findPathAnchorFromBottom(List<PathElement> pathElements, int level) { if (level < 0) { return Optional.empty(); } PathElement element = pathElements.get(level); if (level > 0) { Optional<PathElement> ignoreAncestor = findAncestorOfType(pathElements, level - 1, IGNORE); if (ignoreAncestor.isPresent()) { return findPathAnchorFromBottom(pathElements, ignoreAncestor.get().getLevel() - 1); } } if (element.getAnchorType() == CHAR_LITERAL) { if (level > 0) { if (pathElements.get(level - 1).getAnchorType() == STRING_LITERAL) { return findPathAnchorFromBottom(pathElements, level - 1); } Optional<PathElement> enumSetValueAncestor = findAncestorOfType(pathElements, level - 1, ENUM_SET_VALUE); if (enumSetValueAncestor.isPresent()) { return enumSetValueAncestor; } return Optional.of(element); } } else if (element.getAnchorType() != null && element.getAnchorType() != IGNORE) { return Optional.of(element); } return findPathAnchorFromBottom(pathElements, level - 1); }    }