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 thePath
interface that uses a string to store the actual path. This class can be used to minimize the effort required to implement thePath
interface.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 String
CURRENT_DIR
The relative path to the current directory.static String
PARENT_DIR
The relative path to the parent directory.static String
ROOT_PATH
The root path.static String
SEPARATOR
The file separator.
-
Constructor Summary
Constructors Modifier Constructor Description protected
SimpleAbstractPath(String path)
Creates a new path.protected
SimpleAbstractPath(String path, boolean normalized)
Creates a new path.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
compareTo(Path other)
Compares two abstract paths lexicographically.protected abstract SimpleAbstractPath
createPath(String path)
Creates a new path.boolean
endsWith(Path other)
Tests if this path starts with the given path.boolean
equals(Object obj)
Tests this path for equality with the given object.String
fileName()
Returns the file name.int
getNameCount()
Returns the number of name elements in the path.Path
getParent()
Returns the parent path, ornull
if this path does not have a parent.Path
getRoot()
Returns the root component of this path as aPath
object, ornull
if this path does not have a root component.int
hashCode()
boolean
isAbsolute()
Tells whether or not this path is absolute.boolean
isNormalized()
Tells whether or not this path is normalized.String
nameAt(int index)
Returns the name at the given index.Path
normalize()
Returns a path that is this path with redundant name elements eliminated.String
normalizedPath()
Returns a path that is this path with redundant name elements eliminated.String
parentPath()
Returns the parent path.String
path()
Returns the actual path.Path
relativize(Path other)
Constructs a relative path between this path and a given path.Path
resolve(Path other)
Resolve the given path against this path.String
rootPath()
Returns the root path.boolean
startsWith(Path other)
Tests if this path starts with the given path.Path
subpath(int beginIndex, int endIndex)
Returns a relativePath
that is a subsequence of the name elements of this path.String
toString()
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
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, orfalse
otherwise.
-
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
null
if this path is relative.
-
getRoot
public Path getRoot()
Returns the root component of this path as aPath
object, ornull
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, ornull
otherwise.
-
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
null
if 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 relativePath
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 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:
true
if this path is normalized, orfalse
otherwise.- 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
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.
-
hashCode
public int hashCode()
-
-