public class RelativePath extends Object
Modifier and Type | Field and Description |
---|---|
static char |
DEFAULT_SEPARATOR
The default separator to use.
|
Constructor and Description |
---|
RelativePath()
Create a new empty RelativePath using the default separator.
|
RelativePath(char separator,
String... elements)
Create a new RelativePath using the given path elements and the separator.
|
RelativePath(RelativePath... paths)
Create a new RelativePath by appending the provided
paths . |
RelativePath(String... elements)
Create a new RelativePath using the given path elements and the default separator.
|
RelativePath(String path)
Create a new RelativePath using the path and the default separator.
|
Modifier and Type | Method and Description |
---|---|
boolean |
endsWith(RelativePath other)
Tests if this RelativePath ends with the given RelativePath.
|
boolean |
endsWith(String other)
Tests if this RelativePath ends with the given RelativePath.
|
boolean |
equals(Object obj) |
String |
getAbsolutePath()
Return a
String representation of this RelativePath interpreted as an absolute path. |
RelativePath |
getFileName()
Get the file name, or
null if the RelativePath is empty. |
String |
getFileNameAsString()
Get the file name as a
String , or null if the RelativePath is empty. |
RelativePath |
getName(int index)
Get a name element of this RelativePath.
|
int |
getNameCount()
Get the number of name elements in the RelativePath.
|
RelativePath |
getParent()
Get the parent RelativePath, or
null if this RelativePath does not have a parent. |
String |
getRelativePath()
Return a
String representation of this RelativePath interpreted as a relative path. |
char |
getSeparator()
Get the separator.
|
int |
hashCode() |
boolean |
isEmpty()
Is this RelativePath empty ?
|
Iterator<RelativePath> |
iterator()
Create an
Iterator that returns all possible sub RelativePaths of this RelativePath, in order of increasing length. |
RelativePath |
normalize()
Normalize this RelativePath by removing as many redundant path elements as possible.
|
RelativePath |
relativize(RelativePath other)
Create a relative RelativePath between the given RelativePath and this RelativePath.
|
RelativePath |
resolve(RelativePath other)
Resolve a RelativePath against this RelativePath.
|
RelativePath |
resolve(String other)
Resolve a String containing a RelativePath against this path.
|
RelativePath |
resolveSibling(RelativePath other)
Resolves the given RelativePath to this paths parent RelativePath, thereby creating a sibling to this RelativePath.
|
boolean |
startsWith(RelativePath other)
Tests if this RelativePath starts with the given RelativePath.
|
boolean |
startsWith(String other)
Tests if this RelativePath starts with the given RelativePath.
|
RelativePath |
subpath(int beginIndex,
int endIndex)
Returns a RelativePath that is a subsequence of the name elements of this path.
|
String |
toString() |
public static final char DEFAULT_SEPARATOR
public RelativePath()
public RelativePath(String path)
path
is null
or an empty String, the resulting RelativePath is empty.
If path
contains the separator it will be split into multiple elements.path
- the path to use.public RelativePath(String... elements)
elements
is null
or an empty String array, the resulting RelativePath is empty.
Any elements that are null
or an empty String will be ignored.
Any elements that contain the separator will be split into multiple elements.elements
- the path elements to use.public RelativePath(RelativePath... paths)
paths
.
If the paths
is null
the resulting RelativePath is empty.paths
- the path elements to use.public RelativePath(char separator, String... elements)
elements
is null
or an empty String array, the resulting RelativePath is empty.
Otherwise, each of the elements will be parsed individually, splitting them into elements wherever a separator is
encountered. Elements that are null
or contain an empty String are ignored.elements
- the path elements to use.separator
- the separator to use.public RelativePath getFileName()
null
if the RelativePath is empty.
The file name is the last element of the RelativePath.null
.public String getFileNameAsString()
String
, or null
if the RelativePath is empty.
The file name is the last element of the RelativePath.null
.public char getSeparator()
public RelativePath getParent()
null
if this RelativePath does not have a parent.public int getNameCount()
public RelativePath getName(int index)
index
- the index of the elementIllegalArgumentException
- If the index is negative or greater or equal to the number of elements in the path.public RelativePath subpath(int beginIndex, int endIndex)
beginIndex
- the index of the first element, inclusiveendIndex
- the index of the last element, exclusiveIllegalArgumentException
- If the beginIndex or endIndex is negative or greater or equal to the number of elements in the path, or if
beginIndex is larger that or equal to the endIndex.public boolean startsWith(RelativePath other)
true
if this RelativePath starts with the name elements in the given RelativePath. If the
given RelativePath has more name elements than this path then false is returned.other
- the RelativePath to compare to.public boolean endsWith(RelativePath other)
true
if this RelativePath end with the name elements in the given RelativePath. If the
given RelativePath has more name elements than this RelativePath then false is returned.other
- the RelativePath to compare to.public boolean startsWith(String other)
other
into a RelativePath
using RelativePath(String)
and then uses
startsWith(RelativePath)
to compare the result to this RelativePath.other
- the path to test.other
.public boolean endsWith(String other)
other
into a RelativePath
using RelativePath(String)
and then uses
endsWith(RelativePath)
to compare the result to this path.other
- the path to test.other
.public RelativePath resolve(RelativePath other)
other
represents an empty RelativePath, this RelativePath is returned.
If this RelativePath is empty, the other
RelativePath is returned.
Otherwise, a new RelativePath is returned that contains the concatenation of the path elements this RelativePath and the
other
RelativePath.other
- the RelativePath.public RelativePath resolve(String other)
other
into a RelativePath
using RelativePath(String)
and then uses
resolve(RelativePath)
to resolve the result against this path.
If other
represents an empty path, or null
this RelativePath is returned.other
- the path.public boolean isEmpty()
public RelativePath resolveSibling(RelativePath other)
other
will be returned, unless other is null
in which case an
empty RelativePath is returned.
If this RelativePath is not empty, but other
is null
or empty, the parent of this RelativePath
will be returned.
If neither this RelativePath and other are empty, getParent.resolve(other)
will be returned.other
- the RelativePath to resolve as sibling.IllegalArgumentException
- If the RelativePath can not be resolved as a sibling to this RelativePath.public RelativePath relativize(RelativePath other)
other
.other
- the RelativePath to relativize.IllegalArgumentException
- If the path can not be relativized to this path.public Iterator<RelativePath> iterator()
Iterator
that returns all possible sub RelativePaths of this RelativePath, in order of increasing length.
For example, for the RelativePath "/a/b/c/d" the iterator returns "/a", "/a/b", "a/b/c", "/a/b/c/d".public String getRelativePath()
String
representation of this RelativePath interpreted as a relative path.
A relative path does not start with a separator.public String getAbsolutePath()
String
representation of this RelativePath interpreted as an absolute path.
An absolute path starts with a separator.public RelativePath normalize()
"."
(indicating the current directory) and ".."
(indicating the
parent directory).
Note that the resulting normalized path does may still contain ".."
elements which are not redundant.