/** * Whether the path is the root path ("/"). * * @param path the path * @return whether this is the root */ public static boolean denotesRoot(String path) { assert isValid(path) : "Invalid path ["+path+"]"; return denotesRootPath(path); }
/** * Whether the path is the root path ("/"). * * @param path the path * @return whether this is the root */ public static boolean denotesRoot(String path) { assert isValid(path) : "Invalid path ["+path+"]"; return denotesRootPath(path); }
/** * Whether the path is the root path ("/"). * * @param path the path * @return whether this is the root */ public static boolean denotesRoot(String path) { assert isValid(path) : "Invalid path ["+path+"]"; return denotesRootPath(path); }
/** * Check if the path is valid, and throw an IllegalArgumentException if not. * A valid path is absolute (starts with a '/') or relative (doesn't start * with '/'), and contains none or more elements. A path may not end with * '/', except for the root path. Elements itself must be at least one * character long. * * @param path the path */ public static void validate(String path) { if (path.isEmpty() || denotesRootPath(path)) { return; } else if (path.charAt(path.length() - 1) == '/') { throw new IllegalArgumentException("Path may not end with '/': " + path); } char last = 0; for (int index = 0, len = path.length(); index < len; index++) { char c = path.charAt(index); if (c == '/') { if (last == '/') { throw new IllegalArgumentException("Path may not contains '//': " + path); } } last = c; } }
/** * Check if the path is valid, and throw an IllegalArgumentException if not. * A valid path is absolute (starts with a '/') or relative (doesn't start * with '/'), and contains none or more elements. A path may not end with * '/', except for the root path. Elements itself must be at least one * character long. * * @param path the path */ public static void validate(String path) { if (path.isEmpty() || denotesRootPath(path)) { return; } else if (path.charAt(path.length() - 1) == '/') { throw new IllegalArgumentException("Path may not end with '/': " + path); } char last = 0; for (int index = 0, len = path.length(); index < len; index++) { char c = path.charAt(index); if (c == '/') { if (last == '/') { throw new IllegalArgumentException("Path may not contains '//': " + path); } } last = c; } }
/** * Check if the path is valid. A valid path is absolute (starts with a '/') * or relative (doesn't start with '/'), and contains none or more elements. * A path may not end with '/', except for the root path. Elements itself must * be at least one character long. * * @param path the path * @return {@code true} iff the path is valid. */ public static boolean isValid(String path) { if (path.isEmpty() || denotesRootPath(path)) { return true; } else if (path.charAt(path.length() - 1) == '/') { return false; } char last = 0; for (int index = 0, len = path.length(); index < len; index++) { char c = path.charAt(index); if (c == '/') { if (last == '/') { return false; } } last = c; } return true; }
/** * Check if the path is valid. A valid path is absolute (starts with a '/') * or relative (doesn't start with '/'), and contains none or more elements. * A path may not end with '/', except for the root path. Elements itself must * be at least one character long. * * @param path the path * @return {@code true} iff the path is valid. */ public static boolean isValid(String path) { if (path.isEmpty() || denotesRootPath(path)) { return true; } else if (path.charAt(path.length() - 1) == '/') { return false; } char last = 0; for (int index = 0, len = path.length(); index < len; index++) { char c = path.charAt(index); if (c == '/') { if (last == '/') { return false; } } last = c; } return true; }
/** * Check if the path is valid, and throw an IllegalArgumentException if not. * A valid path is absolute (starts with a '/') or relative (doesn't start * with '/'), and contains none or more elements. A path may not end with * '/', except for the root path. Elements itself must be at least one * character long. * * @param path the path */ public static void validate(String path) { if (path.isEmpty() || denotesRootPath(path)) { return; } else if (path.charAt(path.length() - 1) == '/') { throw new IllegalArgumentException("Path may not end with '/': " + path); } char last = 0; for (int index = 0, len = path.length(); index < len; index++) { char c = path.charAt(index); if (c == '/') { if (last == '/') { throw new IllegalArgumentException("Path may not contains '//': " + path); } } last = c; } }
/** * Get the last element of the (absolute or relative) path. The name of the * root node ("/") and the name of the empty path ("") is the empty path. * * @param path the complete path * @return the last element */ @NotNull public static String getName(String path) { assert isValid(path) : "Invalid path ["+path+"]"; if (path.isEmpty() || denotesRootPath(path)) { return ROOT_NAME; } int end = path.length() - 1; int pos = path.lastIndexOf('/', end); if (pos != -1) { return path.substring(pos + 1, end + 1); } return path; }
/** * Get the last element of the (absolute or relative) path. The name of the * root node ("/") and the name of the empty path ("") is the empty path. * * @param path the complete path * @return the last element */ @NotNull public static String getName(String path) { assert isValid(path) : "Invalid path ["+path+"]"; if (path.isEmpty() || denotesRootPath(path)) { return ROOT_NAME; } int end = path.length() - 1; int pos = path.lastIndexOf('/', end); if (pos != -1) { return path.substring(pos + 1, end + 1); } return path; }
/** * Get the last element of the (absolute or relative) path. The name of the * root node ("/") and the name of the empty path ("") is the empty path. * * @param path the complete path * @return the last element */ @Nonnull public static String getName(String path) { assert isValid(path) : "Invalid path ["+path+"]"; if (path.isEmpty() || denotesRootPath(path)) { return ROOT_NAME; } int end = path.length() - 1; int pos = path.lastIndexOf('/', end); if (pos != -1) { return path.substring(pos + 1, end + 1); } return path; }
/** * Relativize a path wrt. a parent path such that * {@code relativize(parentPath, concat(parentPath, path)) == paths} * holds. * * @param parentPath parent pth * @param path path to relativize * @return relativized path */ @NotNull public static String relativize(String parentPath, String path) { assert isValid(parentPath) : "Invalid parent path ["+parentPath+"]"; assert isValid(path) : "Invalid path ["+path+"]"; if (parentPath.equals(path)) { return ""; } String prefix = denotesRootPath(parentPath) ? parentPath : parentPath + '/'; if (path.startsWith(prefix)) { return path.substring(prefix.length()); } throw new IllegalArgumentException("Cannot relativize " + path + " wrt. " + parentPath); }
assert isValid(path) : "Invalid path ["+path+"]"; if (path.isEmpty() || denotesRootPath(path) || nth <= 0) { return path;
assert isValid(path) : "Invalid path ["+path+"]"; if (path.isEmpty() || denotesRootPath(path) || nth <= 0) { return path;
/** * Relativize a path wrt. a parent path such that * {@code relativize(parentPath, concat(parentPath, path)) == paths} * holds. * * @param parentPath parent pth * @param path path to relativize * @return relativized path */ @Nonnull public static String relativize(String parentPath, String path) { assert isValid(parentPath) : "Invalid parent path ["+parentPath+"]"; assert isValid(path) : "Invalid path ["+path+"]"; if (parentPath.equals(path)) { return ""; } String prefix = denotesRootPath(parentPath) ? parentPath : parentPath + '/'; if (path.startsWith(prefix)) { return path.substring(prefix.length()); } throw new IllegalArgumentException("Cannot relativize " + path + " wrt. " + parentPath); }
/** * Relativize a path wrt. a parent path such that * {@code relativize(parentPath, concat(parentPath, path)) == paths} * holds. * * @param parentPath parent pth * @param path path to relativize * @return relativized path */ @NotNull public static String relativize(String parentPath, String path) { assert isValid(parentPath) : "Invalid parent path ["+parentPath+"]"; assert isValid(path) : "Invalid path ["+path+"]"; if (parentPath.equals(path)) { return ""; } String prefix = denotesRootPath(parentPath) ? parentPath : parentPath + '/'; if (path.startsWith(prefix)) { return path.substring(prefix.length()); } throw new IllegalArgumentException("Cannot relativize " + path + " wrt. " + parentPath); }
/** * Calculate the number of elements in the path. The root path has zero * elements. * * @param path the path * @return the number of elements */ 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++; } }
/** * Concatenate path elements. * * @param parentPath the parent path * @param relativePaths the relative path elements to add * @return the concatenated path */ @NotNull public static String concat(String parentPath, String... relativePaths) { assert isValid(parentPath) : "Invalid parent path ["+parentPath+"]"; int parentLen = parentPath.length(); int size = relativePaths.length; StringBuilder buff = new StringBuilder(parentLen + size * 5); buff.append(parentPath); boolean needSlash = parentLen > 0 && !denotesRootPath(parentPath); for (String s : relativePaths) { assert isValid(s); if (isAbsolutePath(s)) { throw new IllegalArgumentException("Cannot append absolute path " + s); } if (!s.isEmpty()) { if (needSlash) { buff.append('/'); } buff.append(s); needSlash = true; } } return buff.toString(); }
/** * Concatenate path elements. * * @param parentPath the parent path * @param relativePaths the relative path elements to add * @return the concatenated path */ @NotNull public static String concat(String parentPath, String... relativePaths) { assert isValid(parentPath) : "Invalid parent path ["+parentPath+"]"; int parentLen = parentPath.length(); int size = relativePaths.length; StringBuilder buff = new StringBuilder(parentLen + size * 5); buff.append(parentPath); boolean needSlash = parentLen > 0 && !denotesRootPath(parentPath); for (String s : relativePaths) { assert isValid(s); if (isAbsolutePath(s)) { throw new IllegalArgumentException("Cannot append absolute path " + s); } if (!s.isEmpty()) { if (needSlash) { buff.append('/'); } buff.append(s); needSlash = true; } } return buff.toString(); }
/** * Concatenate path elements. * * @param parentPath the parent path * @param relativePaths the relative path elements to add * @return the concatenated path */ @Nonnull public static String concat(String parentPath, String... relativePaths) { assert isValid(parentPath) : "Invalid parent path ["+parentPath+"]"; int parentLen = parentPath.length(); int size = relativePaths.length; StringBuilder buff = new StringBuilder(parentLen + size * 5); buff.append(parentPath); boolean needSlash = parentLen > 0 && !denotesRootPath(parentPath); for (String s : relativePaths) { assert isValid(s); if (isAbsolutePath(s)) { throw new IllegalArgumentException("Cannot append absolute path " + s); } if (!s.isEmpty()) { if (needSlash) { buff.append('/'); } buff.append(s); needSlash = true; } } return buff.toString(); }