Path implements CacheValue, Comparable<Path> { @NotNull public Path getAncestor(int nth) { Path p = this; while (nth-- > 0 && p.parent != null) { p = p.parent; } return p; } private  Path(@Nullable Path parent,
                 @NotNull String name,
                 int hash);  Path(@NotNull Path parent, @NotNull String name);  Path(@NotNull String name); @NotNull String getName(); @NotNull Iterable<String> elements(); boolean isRoot(); @Nullable Path getParent(); int length(); int getDepth(); @NotNull Path getAncestor(int nth); boolean isAncestorOf(@NotNull Path other); boolean isAbsolute(); @NotNull static Path fromString(@NotNull String path); @NotNull StringBuilder toStringBuilder(@NotNull StringBuilder sb); @Override int getMemory(); @Override int compareTo(@NotNull Path other); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj); static final Path ROOT; }
@Test public void getAncestor() { assertEquals(root, root.getAncestor(-1)); assertEquals(root, root.getAncestor(0)); assertEquals(root, root.getAncestor(1)); assertEquals(foo, foo.getAncestor(0)); assertEquals(root, foo.getAncestor(1)); assertEquals(root, foo.getAncestor(2)); assertEquals(fooBar, fooBar.getAncestor(0)); assertEquals(foo, fooBar.getAncestor(1)); assertEquals(root, fooBar.getAncestor(2)); assertEquals(root, fooBar.getAncestor(3)); assertEquals(fooBar, fooBarQuux.getAncestor(1)); assertEquals(relFoo, relFoo.getAncestor(-1)); assertEquals(relFoo, relFoo.getAncestor(0)); assertEquals(relFoo, relFoo.getAncestor(1)); assertEquals(relFooBar, relFooBar.getAncestor(0)); assertEquals(relFoo, relFooBar.getAncestor(1)); assertEquals(relFoo, relFooBar.getAncestor(2)); assertEquals(relFoo, relFooBar.getAncestor(3)); assertEquals(relFooBar, relFooBarQuux.getAncestor(1)); }