public abstract class SimpleAbstractPath extends AbstractPath
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
.
Modifier and Type | Field and 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.
|
Modifier | Constructor and Description |
---|---|
protected |
SimpleAbstractPath(String path)
Creates a new path.
|
protected |
SimpleAbstractPath(String path,
boolean normalized)
Creates a new path.
|
Modifier and Type | Method and 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, or
null if this path does not have a parent. |
Path |
getRoot()
Returns the root component of this path as a
Path object, or null 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 relative
Path that is a subsequence of the name elements of this path. |
String |
toString()
Returns the string representation of this path.
|
endsWith, getFileName, getName, iterator, register, resolve, resolveSibling, resolveSibling, startsWith, toFile
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
getFileSystem, register, toAbsolutePath, toRealPath, toUri
forEach, spliterator
public static final String SEPARATOR
public static final String ROOT_PATH
public static final String CURRENT_DIR
public static final String PARENT_DIR
protected SimpleAbstractPath(String path)
path
- The actual path.protected SimpleAbstractPath(String path, boolean normalized)
path
- The actual path.normalized
- If not true
, the path will be normalized (e.g. by removing redundant forward slashes).protected abstract SimpleAbstractPath createPath(String path)
path
- The actual path for the new path. This will already be normalized when called by the implementations of this class.public final String path()
public final String nameAt(int index)
AbstractPath.getName(int)
but returns the name as a string, not a Path
.index
- The index of the name.IllegalArgumentException
- If the index is invalid.public final String fileName()
AbstractPath.getFileName()
but returns the file name as a string, not a Path
.null
if there is no file name.public boolean isAbsolute()
This implementation returns true
if the path starts with a forward slash, or false
otherwise.
public final String rootPath()
getRoot()
but returns the root as a string, not a Path
.null
if this path is relative.public Path getRoot()
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.
public final String parentPath()
getParent()
but returns the parent as a string, not a Path
.null
if this path has no parent.public Path getParent()
public int getNameCount()
This implementation returns a value calculated from the number of forward slashes in the actual path.
public Path subpath(int beginIndex, int endIndex)
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.
public boolean startsWith(Path other)
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.
public boolean endsWith(Path other)
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.
public Path normalize()
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
.
public final String normalizedPath()
normalize()
but returns the parent as a string, not a Path
.public final boolean isNormalized()
normalized path
.true
if this path is normalized, or false
otherwise.public Path relativize(Path other)
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.
public int compareTo(Path other)
This implementation checks if the given path is an instance of the same class, then compares the actual paths of the two abstract paths.
public boolean equals(Object obj)
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.
public int hashCode()
Copyright © 2016–2023. All rights reserved.