PathUtils { public static int getDepth(String path) { assert isValid(path) : "Invalid path ["+path+"]"; if (path.isEmpty()) { return 0; } int count = 1, i = 0; if (isAbsolutePath(path)) { if (denotesRootPath(path)) { return 0; } i++; } while (true) { i = path.indexOf('/', i) + 1; if (i == 0) { return count; } count++; } } private  PathUtils(); static boolean denotesRoot(String path); static boolean denotesCurrent(String element); static boolean denotesParent(String element); static boolean isAbsolute(String path); @NotNull static String getParentPath(String path); @NotNull static String getAncestorPath(String path, int nth); @NotNull static String getName(String path); @NotNull static String dropIndexFromName(@NotNull String name); static int getDepth(String path); @NotNull static Iterable<String> elements(final String path); @NotNull static String concat(String parentPath, String... relativePaths); @NotNull static String concat(String parentPath, String subPath); @Nullable static String concatRelativePaths(String... relativePaths); static boolean isAncestor(String ancestor, String path); @NotNull static String relativize(String parentPath, String path); static int getNextSlash(String path, int index); static void validate(String path); static boolean isValid(String path); static void unifyInExcludes(Set<String> includePaths, Set<String> excludedPaths); static final String ROOT_PATH; static final String ROOT_NAME; }
@Test public void testGetDepth() { assertEquals(0, PathUtils.getDepth("/")); assertEquals(0, PathUtils.getDepth("")); assertEquals(1, PathUtils.getDepth("/a")); assertEquals(1, PathUtils.getDepth("a")); assertEquals(2, PathUtils.getDepth("/a/b")); assertEquals(2, PathUtils.getDepth("a/b")); }