Class PathUtils


  • public final class PathUtils
    extends java.lang.Object
    Utility methods to parse a path.

    Each method validates the input, except if the system property {packageName}.SKIP_VALIDATION is set, in which case only minimal validation takes place within this function, so when the parameter is an illegal path, the the result of this method is undefined.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String ROOT_NAME  
      static java.lang.String ROOT_PATH  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static @NotNull java.lang.String concat​(java.lang.String parentPath, java.lang.String subPath)
      Concatenate path elements.
      static @NotNull java.lang.String concat​(java.lang.String parentPath, java.lang.String... relativePaths)
      Concatenate path elements.
      static @Nullable java.lang.String concatRelativePaths​(java.lang.String... relativePaths)
      Relative path concatenation.
      static boolean denotesCurrent​(java.lang.String element)  
      static boolean denotesParent​(java.lang.String element)  
      static boolean denotesRoot​(java.lang.String path)
      Whether the path is the root path ("/").
      static @NotNull java.lang.String dropIndexFromName​(@NotNull java.lang.String name)
      Returns the given name without the possible SNS index suffix.
      static @NotNull java.lang.Iterable<java.lang.String> elements​(java.lang.String path)
      Returns an Iterable for the path elements.
      static @NotNull java.lang.String getAncestorPath​(java.lang.String path, int nth)
      Get the nth ancestor of a path.
      static int getDepth​(java.lang.String path)
      Calculate the number of elements in the path.
      static @NotNull java.lang.String getName​(java.lang.String path)
      Get the last element of the (absolute or relative) path.
      static int getNextSlash​(java.lang.String path, int index)
      Get the index of the next slash.
      static @NotNull java.lang.String getParentPath​(java.lang.String path)
      Get the parent of a path.
      static boolean isAbsolute​(java.lang.String path)
      Whether the path is absolute (starts with a slash) or not.
      static boolean isAncestor​(java.lang.String ancestor, java.lang.String path)
      Check if a path is a (direct or indirect) ancestor of another path.
      static boolean isValid​(java.lang.String path)
      Check if the path is valid.
      static @NotNull java.lang.String relativize​(java.lang.String parentPath, java.lang.String path)
      Relativize a path wrt.
      static void unifyInExcludes​(java.util.Set<java.lang.String> includePaths, java.util.Set<java.lang.String> excludedPaths)
      Unify path inclusions and exclusions.
      static void validate​(java.lang.String path)
      Check if the path is valid, and throw an IllegalArgumentException if not.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • denotesRoot

        public static boolean denotesRoot​(java.lang.String path)
        Whether the path is the root path ("/").
        Parameters:
        path - the path
        Returns:
        whether this is the root
      • denotesCurrent

        public static boolean denotesCurrent​(java.lang.String element)
        Parameters:
        element - The path segment to check for being the current element
        Returns:
        true if the specified element equals "."; false otherwise.
      • denotesParent

        public static boolean denotesParent​(java.lang.String element)
        Parameters:
        element - The path segment to check for being the parent element
        Returns:
        true if the specified element equals ".."; false otherwise.
      • isAbsolute

        public static boolean isAbsolute​(java.lang.String path)
        Whether the path is absolute (starts with a slash) or not.
        Parameters:
        path - the path
        Returns:
        true if it starts with a slash
      • getParentPath

        @NotNull
        public static @NotNull java.lang.String getParentPath​(java.lang.String path)
        Get the parent of a path. The parent of the root path ("/") is the root path.
        Parameters:
        path - the path
        Returns:
        the parent path
      • getAncestorPath

        @NotNull
        public static @NotNull java.lang.String getAncestorPath​(java.lang.String path,
                                                                int nth)
        Get the nth ancestor of a path. The 1st ancestor is the parent path, 2nd ancestor the grandparent path, and so on...

        If nth <= 0, the path argument is returned as is.

        Parameters:
        path - the path
        nth - indicates the ancestor level for which the path should be calculated.
        Returns:
        the ancestor path
      • getName

        @NotNull
        public static @NotNull java.lang.String getName​(java.lang.String 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.
        Parameters:
        path - the complete path
        Returns:
        the last element
      • dropIndexFromName

        @NotNull
        public static @NotNull java.lang.String dropIndexFromName​(@NotNull
                                                                  @NotNull java.lang.String name)
        Returns the given name without the possible SNS index suffix. If the name does not contain an SNS index, then it is returned as-is.
        Parameters:
        name - name with a possible SNS index suffix
        Returns:
        name without the SNS index suffix
      • getDepth

        public static int getDepth​(java.lang.String path)
        Calculate the number of elements in the path. The root path has zero elements.
        Parameters:
        path - the path
        Returns:
        the number of elements
      • elements

        @NotNull
        public static @NotNull java.lang.Iterable<java.lang.String> elements​(java.lang.String path)
        Returns an Iterable for the path elements. The root path ("/") and the empty path ("") have zero elements.
        Parameters:
        path - the path
        Returns:
        an Iterable for the path elements
      • concat

        @NotNull
        public static @NotNull java.lang.String concat​(java.lang.String parentPath,
                                                       java.lang.String... relativePaths)
        Concatenate path elements.
        Parameters:
        parentPath - the parent path
        relativePaths - the relative path elements to add
        Returns:
        the concatenated path
      • concat

        @NotNull
        public static @NotNull java.lang.String concat​(java.lang.String parentPath,
                                                       java.lang.String subPath)
        Concatenate path elements.
        Parameters:
        parentPath - the parent path
        subPath - the subPath path to add
        Returns:
        the concatenated path
      • concatRelativePaths

        @Nullable
        public static @Nullable java.lang.String concatRelativePaths​(java.lang.String... relativePaths)
        Relative path concatenation.
        Parameters:
        relativePaths - relative paths
        Returns:
        the concatenated path or null if the resulting path is empty.
      • isAncestor

        public static boolean isAncestor​(java.lang.String ancestor,
                                         java.lang.String path)
        Check if a path is a (direct or indirect) ancestor of another path.
        Parameters:
        ancestor - the ancestor path
        path - the potential offspring path
        Returns:
        true if the path is an offspring of the ancestor
      • relativize

        @NotNull
        public static @NotNull java.lang.String relativize​(java.lang.String parentPath,
                                                           java.lang.String path)
        Relativize a path wrt. a parent path such that relativize(parentPath, concat(parentPath, path)) == paths holds.
        Parameters:
        parentPath - parent pth
        path - path to relativize
        Returns:
        relativized path
      • getNextSlash

        public static int getNextSlash​(java.lang.String path,
                                       int index)
        Get the index of the next slash.
        Parameters:
        path - the path
        index - the starting index
        Returns:
        the index of the next slash (possibly the starting index), or -1 if not found
      • validate

        public static void validate​(java.lang.String 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.
        Parameters:
        path - the path
      • isValid

        public static boolean isValid​(java.lang.String path)
        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.
        Parameters:
        path - the path
        Returns:
        true iff the path is valid.
      • unifyInExcludes

        public static void unifyInExcludes​(java.util.Set<java.lang.String> includePaths,
                                           java.util.Set<java.lang.String> excludedPaths)
        Unify path inclusions and exclusions.
        • A path in includePaths is only retained if includePaths contains none of its ancestors and excludePaths contains neither of its ancestors nor that path itself.
        • A path in excludePaths is only retained if includePaths contains an ancestor of that path.
        When a set of paths is filtered wrt. includePaths and excludePaths by first excluding all paths that have an ancestor or are contained in excludePaths and then including all paths that have an ancestor or are contained in includePaths then the result is the same regardless whether the includePaths and excludePaths sets have been run through this method or not.
        Parameters:
        includePaths - set of paths to be included
        excludedPaths - set of paths to be excluded