/** * Get the last entry in a path; for an empty path * returns "". The split logic is that of * {@link #split(String)} * @param path path of operation * @return the last path entry or "" if none. */ public static String lastPathEntry(String path) { List<String> splits = split(path); if (splits.isEmpty()) { // empty path. Return "" return ""; } else { return splits.get(splits.size() - 1); } }
/** * Get the last entry in a path; for an empty path * returns "". The split logic is that of * {@link #split(String)} * @param path path of operation * @return the last path entry or "" if none. */ public static String lastPathEntry(String path) { List<String> splits = split(path); if (splits.isEmpty()) { // empty path. Return "" return ""; } else { return splits.get(splits.size() - 1); } }
/** * Validate ZK path as valid for a DNS hostname. * @param path path to validate * @return the path parameter * @throws InvalidPathnameException if the pathname is invalid. */ public static String validateElementsAsDNS(String path) throws InvalidPathnameException { List<String> splitpath = split(path); for (String fragment : splitpath) { if (!PATH_ENTRY_VALIDATION_PATTERN.matcher(fragment).matches()) { throw new InvalidPathnameException(path, "Invalid Path element \"" + fragment + "\""); } } return path; }
/** * Validate ZK path as valid for a DNS hostname. * @param path path to validate * @return the path parameter * @throws InvalidPathnameException if the pathname is invalid. */ public static String validateElementsAsDNS(String path) throws InvalidPathnameException { List<String> splitpath = split(path); for (String fragment : splitpath) { if (!PATH_ENTRY_VALIDATION_PATTERN.matcher(fragment).matches()) { throw new InvalidPathnameException(path, "Invalid Path element \"" + fragment + "\""); } } return path; }
/** * Get the parent of a path * @param path path to look at * @return the parent path * @throws PathNotFoundException if the path was at root. */ public static String parentOf(String path) throws PathNotFoundException { List<String> elements = split(path); int size = elements.size(); if (size == 0) { throw new PathNotFoundException("No parent of " + path); } if (size == 1) { return "/"; } elements.remove(size - 1); StringBuilder parent = new StringBuilder(path.length()); for (String element : elements) { parent.append("/"); parent.append(element); } return parent.toString(); }
/** * Get the parent of a path * @param path path to look at * @return the parent path * @throws PathNotFoundException if the path was at root. */ public static String parentOf(String path) throws PathNotFoundException { List<String> elements = split(path); int size = elements.size(); if (size == 0) { throw new PathNotFoundException("No parent of " + path); } if (size == 1) { return "/"; } elements.remove(size - 1); StringBuilder parent = new StringBuilder(path.length()); for (String element : elements) { parent.append("/"); parent.append(element); } return parent.toString(); }
@Test public void testSplitting() throws Throwable { assertEquals(1, split("/a").size()); assertEquals(0, split("/").size()); assertEquals(3, split("/a/b/c").size()); assertEquals(3, split("/a/b/c/").size()); assertEquals(3, split("a/b/c").size()); assertEquals(3, split("/a/b//c").size()); assertEquals(3, split("//a/b/c/").size()); List<String> split = split("//a/b/c/"); assertEquals("a", split.get(0)); assertEquals("b", split.get(1)); assertEquals("c", split.get(2)); }
@Test public void testSplitting() throws Throwable { assertEquals(1, split("/a").size()); assertEquals(0, split("/").size()); assertEquals(3, split("/a/b/c").size()); assertEquals(3, split("/a/b/c/").size()); assertEquals(3, split("a/b/c").size()); assertEquals(3, split("/a/b//c").size()); assertEquals(3, split("//a/b/c/").size()); List<String> split = split("//a/b/c/"); assertEquals("a", split.get(0)); assertEquals("b", split.get(1)); assertEquals("c", split.get(2)); }