Class Path

  • All Implemented Interfaces:
    java.lang.Comparable<Path>, CacheValue

    public final class Path
    extends java.lang.Object
    implements CacheValue, java.lang.Comparable<Path>
    The Path class is closely modeled after the semantics of PathUtils in oak-commons. Corresponding methods in this class can be used as a replacement for the methods in PathUtils on Path objects.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Path ROOT  
    • Constructor Summary

      Constructors 
      Constructor Description
      Path​(@NotNull java.lang.String name)
      Creates a relative path with a single name element.
      Path​(@NotNull Path parent, @NotNull java.lang.String name)
      Creates a new Path from the given parent Path.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(@NotNull Path other)  
      @NotNull java.lang.Iterable<java.lang.String> elements()
      Returns the names of the path elements with increasing getDepth() starting at depth 1.
      boolean equals​(java.lang.Object obj)  
      static @NotNull Path fromString​(@NotNull java.lang.String path)
      Creates a Path from a String.
      @NotNull Path getAncestor​(int nth)
      Get the nth ancestor of a path.
      int getDepth()
      The depth of this path.
      int getMemory()
      The estimated amount of memory used by this object, in bytes.
      @NotNull java.lang.String getName()
      Returns the name of this path.
      @Nullable Path getParent()
      The parent of this path or null if this path does not have a parent.
      int hashCode()  
      boolean isAbsolute()  
      boolean isAncestorOf​(@NotNull Path other)
      Return true if this path is an ancestor of the other path, otherwise false.
      boolean isRoot()
      Returns true if this is the ROOT path; false otherwise.
      int length()  
      java.lang.String toString()  
      @NotNull java.lang.StringBuilder toStringBuilder​(@NotNull java.lang.StringBuilder sb)
      Appends the String representation of this Path to the passed StringBuilder.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • ROOT

        public static final Path ROOT
    • Constructor Detail

      • Path

        public Path​(@NotNull
                    @NotNull Path parent,
                    @NotNull
                    @NotNull java.lang.String name)
        Creates a new Path from the given parent Path. The name of the new Path cannot be the empty String.
        Parameters:
        parent - the parent Path.
        name - the name of the new Path.
        Throws:
        java.lang.IllegalArgumentException - if the name is empty.
      • Path

        public Path​(@NotNull
                    @NotNull java.lang.String name)
        Creates a relative path with a single name element. The name cannot be the empty String.
        Parameters:
        name - the name of the first path element.
        Throws:
        java.lang.IllegalArgumentException - if the name is empty.
    • Method Detail

      • getName

        @NotNull
        public @NotNull java.lang.String getName()
        Returns the name of this path. The ROOT is the only path with an empty name. That is a String with length zero.
        Returns:
        the name of this path.
      • elements

        @NotNull
        public @NotNull java.lang.Iterable<java.lang.String> elements()
        Returns the names of the path elements with increasing getDepth() starting at depth 1.
        Returns:
        the names of the path elements.
      • isRoot

        public boolean isRoot()
        Returns true if this is the ROOT path; false otherwise.
        Returns:
        whether this is the ROOT path.
      • getParent

        @Nullable
        public @Nullable Path getParent()
        The parent of this path or null if this path does not have a parent. The ROOT path and the first path element of a relative path do not have a parent.
        Returns:
        the parent of this path or null if this path does not have a parent.
      • length

        public int length()
        Returns:
        the number of characters of the String representation of this path.
      • getDepth

        public int getDepth()
        The depth of this path. The ROOT has a depth of 0. The path /foo/bar as well as bar/baz have depth 2.
        Returns:
        the depth of the path.
      • getAncestor

        @NotNull
        public @NotNull Path getAncestor​(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, then this path is returned.

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

        public boolean isAncestorOf​(@NotNull
                                    @NotNull Path other)
        Return true if this path is an ancestor of the other path, otherwise false.
        Parameters:
        other - the other path.
        Returns:
        whether this path is an ancestor of the other path.
      • isAbsolute

        public boolean isAbsolute()
        Returns:
        true if this is an absolute path; false otherwise.
      • fromString

        @NotNull
        public static @NotNull Path fromString​(@NotNull
                                               @NotNull java.lang.String path)
                                        throws java.lang.IllegalArgumentException
        Creates a Path from a String.
        Parameters:
        path - the String to parse.
        Returns:
        the Path from the String.
        Throws:
        java.lang.IllegalArgumentException - if the path is the empty String.
      • toStringBuilder

        @NotNull
        public @NotNull java.lang.StringBuilder toStringBuilder​(@NotNull
                                                                @NotNull java.lang.StringBuilder sb)
        Appends the String representation of this Path to the passed StringBuilder. See also toString().
        Parameters:
        sb - the StringBuilder this Path is appended to.
        Returns:
        the passed StringBuilder.
      • getMemory

        public int getMemory()
        Description copied from interface: CacheValue
        The estimated amount of memory used by this object, in bytes.
        Specified by:
        getMemory in interface CacheValue
        Returns:
        the estimated number of bytes
      • compareTo

        public int compareTo​(@NotNull
                             @NotNull Path other)
        Specified by:
        compareTo in interface java.lang.Comparable<Path>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object