Class SimpleAbstractPath

  • All Implemented Interfaces:
    Comparable<Path>, Iterable<Path>, Path, Watchable

    public abstract class SimpleAbstractPath
    extends AbstractPath
    This class provides a base implementation of the Path interface that uses a string to store the actual path. This class can be used to minimize the effort required to implement the Path interface.

    Note that this class assumes that the file system uses a single forward slash (/) as its separator.

    Author:
    Rob Spoor
    • Constructor Detail

      • SimpleAbstractPath

        protected SimpleAbstractPath​(String path)
        Creates a new path.
        Parameters:
        path - The actual path.
      • SimpleAbstractPath

        protected SimpleAbstractPath​(String path,
                                     boolean normalized)
        Creates a new path.
        Parameters:
        path - The actual path.
        normalized - If not true, the path will be normalized (e.g. by removing redundant forward slashes).
    • Method Detail

      • createPath

        protected abstract SimpleAbstractPath createPath​(String path)
        Creates a new path. Implementations should create instances of the implementing class.
        Parameters:
        path - The actual path for the new path. This will already be normalized when called by the implementations of this class.
        Returns:
        The created path.
      • path

        public final String path()
        Returns the actual path.
        Returns:
        The actual path.
      • nameAt

        public final String nameAt​(int index)
        Returns the name at the given index. This method is similar to AbstractPath.getName(int) but returns the name as a string, not a Path.
        Parameters:
        index - The index of the name.
        Returns:
        The name at the given index.
        Throws:
        IllegalArgumentException - If the index is invalid.
      • fileName

        public final String fileName()
        Returns the file name. This method is similar to AbstractPath.getFileName() but returns the file name as a string, not a Path.
        Returns:
        The file name, or null if there is no file name.
      • isAbsolute

        public boolean isAbsolute()
        Tells whether or not this path is absolute.

        This implementation returns true if the path starts with a forward slash, or false otherwise.

      • rootPath

        public final String rootPath()
        Returns the root path. This method is similar to getRoot() but returns the root as a string, not a Path.
        Returns:
        The root path, or null if this path is relative.
      • getRoot

        public Path getRoot()
        Returns the root component of this path as a Path object, or null if this path does not have a root component.

        This implementation returns a path created with a single forward slash as its path if this path is absolute, or null otherwise.

      • parentPath

        public final String parentPath()
        Returns the parent path. This method is similar to getParent() but returns the parent as a string, not a Path.
        Returns:
        The parent, or null if this path has no parent.
      • getParent

        public Path getParent()
        Returns the parent path, or null if this path does not have a parent.

        This implementation returns:

        • null if this path has no name elements.
        • getRoot() if this path has only one name element.
        • A path created with this path's path up until the last forward slash otherwise.
      • getNameCount

        public int getNameCount()
        Returns the number of name elements in the path.

        This implementation returns a value calculated from the number of forward slashes in the actual path.

      • subpath

        public Path subpath​(int beginIndex,
                            int endIndex)
        Returns a relative Path that is a subsequence of the name elements of this path.

        This implementation returns a non-absolute path created with a path that is the appropriate substring of this path's actual path.

      • startsWith

        public boolean startsWith​(Path other)
        Tests if this path starts with the given path.

        This implementation will first check if the two paths have the same FileSystem and class. If not, false is returned. It will then check if the actual path of this path starts with the actual path of the given path.

      • endsWith

        public boolean endsWith​(Path other)
        Tests if this path starts with the given path.

        This implementation will first check if the two paths have the same FileSystem and class. If not, false is returned. It will then check if the actual path of this path ends with the actual path of the given path.

      • normalize

        public Path normalize()
        Returns a path that is this path with redundant name elements eliminated.

        This implementation will go over the name elements, removing all occurrences of single dots (.). For any occurrence of a double dot (..), any previous element (if any) is removed as well. With the remaining name elements, a new path is created.

      • normalizedPath

        public final String normalizedPath()
        Returns a path that is this path with redundant name elements eliminated. This method is similar to normalize() but returns the parent as a string, not a Path.
        Returns:
        A path that is this path with redundant name elements eliminated.
        Since:
        2.3
      • isNormalized

        public final boolean isNormalized()
        Tells whether or not this path is normalized. A path is normalized if its path is equal to its normalized path.
        Returns:
        true if this path is normalized, or false otherwise.
        Since:
        2.3
      • resolve

        public Path resolve​(Path other)
        Resolve the given path against this path.

        This implementation returns the given path if it's absolute or if this path has no name elements, this path if the given path has no name elements, or a path created with the paths of this path and the given path joined with a forward slash otherwise.

      • relativize

        public Path relativize​(Path other)
        Constructs a relative path between this path and a given path.

        This implementation skips past any shared name elements, then adds as many occurrences of double dots (..) as needed, then adds the remainder of the given path to the result.

      • compareTo

        public int compareTo​(Path other)
        Compares two abstract paths lexicographically.

        This implementation checks if the given path is an instance of the same class, then compares the actual paths of the two abstract paths.

      • equals

        public boolean equals​(Object obj)
        Tests this path for equality with the given object.

        This implementation will return true if the given object is an instance of the same class as this path, with the same file system, and with the same actual path.

        Specified by:
        equals in interface Path
        Overrides:
        equals in class Object
      • toString

        public String toString()
        Returns the string representation of this path.

        This implementation only returns the actual path.

        Specified by:
        toString in interface Path
        Overrides:
        toString in class Object