Class SimpleAbstractPath
- java.lang.Object
-
- com.github.robtimus.filesystems.AbstractPath
-
- com.github.robtimus.filesystems.SimpleAbstractPath
-
public abstract class SimpleAbstractPath extends AbstractPath
This class provides a base implementation of thePathinterface that uses a string to store the actual path. This class can be used to minimize the effort required to implement thePathinterface.Note that this class assumes that the file system uses a single forward slash (
/) as itsseparator.- Author:
- Rob Spoor
-
-
Field Summary
Fields Modifier and Type Field Description static StringCURRENT_DIRThe relative path to the current directory.static StringPARENT_DIRThe relative path to the parent directory.static StringROOT_PATHThe root path.static StringSEPARATORThe file separator.
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleAbstractPath(String path)Creates a new path.protectedSimpleAbstractPath(String path, boolean normalized)Creates a new path.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description intcompareTo(Path other)Compares two abstract paths lexicographically.protected abstract SimpleAbstractPathcreatePath(String path)Creates a new path.booleanendsWith(Path other)Tests if this path starts with the given path.booleanequals(Object obj)Tests this path for equality with the given object.StringfileName()Returns the file name.intgetNameCount()Returns the number of name elements in the path.PathgetParent()Returns the parent path, ornullif this path does not have a parent.PathgetRoot()Returns the root component of this path as aPathobject, ornullif this path does not have a root component.inthashCode()booleanisAbsolute()Tells whether or not this path is absolute.booleanisNormalized()Tells whether or not this path is normalized.StringnameAt(int index)Returns the name at the given index.Pathnormalize()Returns a path that is this path with redundant name elements eliminated.StringnormalizedPath()Returns a path that is this path with redundant name elements eliminated.StringparentPath()Returns the parent path.Stringpath()Returns the actual path.Pathrelativize(Path other)Constructs a relative path between this path and a given path.Pathresolve(Path other)Resolve the given path against this path.StringrootPath()Returns the root path.booleanstartsWith(Path other)Tests if this path starts with the given path.Pathsubpath(int beginIndex, int endIndex)Returns a relativePaththat is a subsequence of the name elements of this path.StringtoString()Returns the string representation of this path.-
Methods inherited from class com.github.robtimus.filesystems.AbstractPath
endsWith, getFileName, getName, iterator, register, resolve, resolveSibling, resolveSibling, startsWith, toFile
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface java.nio.file.Path
getFileSystem, register, toAbsolutePath, toRealPath, toUri
-
-
-
-
Field Detail
-
SEPARATOR
public static final String SEPARATOR
The file separator.- Since:
- 2.2
- See Also:
- Constant Field Values
-
ROOT_PATH
public static final String ROOT_PATH
The root path.- Since:
- 2.2
- See Also:
- Constant Field Values
-
CURRENT_DIR
public static final String CURRENT_DIR
The relative path to the current directory.- Since:
- 2.2
- See Also:
- Constant Field Values
-
PARENT_DIR
public static final String PARENT_DIR
The relative path to the parent directory.- Since:
- 2.2
- See Also:
- Constant Field Values
-
-
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 nottrue, 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 toAbstractPath.getName(int)but returns the name as a string, not aPath.- 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 toAbstractPath.getFileName()but returns the file name as a string, not aPath.- Returns:
- The file name, or
nullif there is no file name.
-
isAbsolute
public boolean isAbsolute()
Tells whether or not this path is absolute.This implementation returns
trueif the path starts with a forward slash, orfalseotherwise.
-
rootPath
public final String rootPath()
Returns the root path. This method is similar togetRoot()but returns the root as a string, not aPath.- Returns:
- The root path, or
nullif this path is relative.
-
getRoot
public Path getRoot()
Returns the root component of this path as aPathobject, ornullif this path does not have a root component.This implementation returns a path
createdwith a single forward slash as its path if this path is absolute, ornullotherwise.
-
parentPath
public final String parentPath()
Returns the parent path. This method is similar togetParent()but returns the parent as a string, not aPath.- Returns:
- The parent, or
nullif this path has no parent.
-
getParent
public Path getParent()
-
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 relativePaththat is a subsequence of the name elements of this path.This implementation returns a non-absolute path
createdwith 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
FileSystemand class. If not,falseis 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
FileSystemand class. If not,falseis 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 iscreated.
-
normalizedPath
public final String normalizedPath()
Returns a path that is this path with redundant name elements eliminated. This method is similar tonormalize()but returns the parent as a string, not aPath.- 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 itsnormalized path.- Returns:
trueif this path is normalized, orfalseotherwise.- Since:
- 2.3
-
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
trueif the given object is an instance of the same class as this path, with the same file system, and with the same actual path.
-
hashCode
public int hashCode()
-
-