Path implements IOReadableWritable, Serializable { public int depth() { String path = uri.getPath(); int depth = 0; int slash = path.length() == 1 && path.charAt(0) == '/' ? -1 : 0; while (slash != -1) { depth++; slash = path.indexOf(SEPARATOR, slash + 1); } return depth; } Path();  Path(URI uri);  Path(String parent, String child);  Path(Path parent, String child);  Path(String parent, Path child);  Path(Path parent, Path child);  Path(String pathString);  Path(String scheme, String authority, String path); URI toUri(); FileSystem getFileSystem(); boolean isAbsolute(); String getName(); Path getParent(); Path suffix(String suffix); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); int compareTo(Object o); int depth(); Path makeQualified(FileSystem fs); @Override void read(DataInputView in); @Override void write(DataOutputView out); static final String SEPARATOR; static final char SEPARATOR_CHAR; static final String CUR_DIR; }
@Test public void testDepth() { Path p = new Path("/my/path"); assertEquals(2, p.depth()); p = new Path("/my/fancy/path/"); assertEquals(3, p.depth()); p = new Path("/my/fancy/fancy/fancy/fancy/fancy/fancy/fancy/fancy/fancy/fancy/path"); assertEquals(12, p.depth()); p = new Path("/"); assertEquals(0, p.depth()); p = new Path("C:/my/windows/path"); assertEquals(4, p.depth()); }