public class Path extends Object implements IPath, Cloneable
IPath
interface.
Paths are always maintained in canonicalized form. That is, parent
references (i.e., ../../
) and duplicate separators are
resolved. For example,
new Path("/a/b").append("../foo/bar")will yield the path
/a/foo/bar
This class can be used without OSGi running.
This class is not intended to be subclassed by clients but may be instantiated.
IPath
Modifier and Type | Field and Description |
---|---|
static Path |
EMPTY
Constant value containing the empty path with no device on the local file system.
|
static Path |
ROOT
Constant value containing the root path with no device on the local file system.
|
DEVICE_SEPARATOR, SEPARATOR
Constructor and Description |
---|
Path(String fullPath)
Constructs a new path from the given string path.
|
Path(String device,
String path)
Constructs a new path from the given device id and string path.
|
Modifier and Type | Method and Description |
---|---|
IPath |
addFileExtension(String extension)
Returns a new path which is the same as this path but with
the given file extension added.
|
IPath |
addTrailingSeparator()
Returns a path with the same segments as this path
but with a trailing separator added.
|
IPath |
append(IPath tail)
Returns the canonicalized path obtained from the
concatenation of the given path's segments to the
end of this path.
|
IPath |
append(String tail)
Returns the canonicalized path obtained from the
concatenation of the given string path to the
end of this path.
|
Object |
clone()
Returns a copy of this path.
|
boolean |
equals(Object obj)
Returns whether this path equals the given object.
|
static Path |
forPosix(String fullPath)
Constructs a new POSIX path from the given string path.
|
static Path |
forWindows(String fullPath)
Constructs a new Windows path from the given string path.
|
static IPath |
fromOSString(String pathString)
Constructs a new path from the given string path.
|
static IPath |
fromPortableString(String pathString)
Constructs a new path from the given path string.
|
String |
getDevice()
Returns the device id for this path, or
null if this
path has no device id. |
String |
getFileExtension()
Returns the file extension portion of this path,
or
null if there is none. |
int |
hashCode() |
boolean |
hasTrailingSeparator()
Returns whether this path has a trailing separator.
|
boolean |
isAbsolute()
Returns whether this path is an absolute path (ignoring
any device id).
|
boolean |
isEmpty()
Returns whether this path has no segments and is not
a root path.
|
boolean |
isPrefixOf(IPath anotherPath)
Returns whether this path is a prefix of the given path.
|
boolean |
isRoot()
Returns whether this path is a root path.
|
boolean |
isUNC()
Returns a boolean value indicating whether or not this path
is considered to be in UNC form.
|
boolean |
isValidPath(String path)
Returns whether the given string is syntactically correct as a path, on
this path's file system.
|
static boolean |
isValidPosixPath(String path)
Returns whether the given string is syntactically correct as a path on a
POSIX file system.
|
static boolean |
isValidPosixSegment(String segment)
Returns whether the given string is valid as a segment in a path on a
POSIX file system.
|
boolean |
isValidSegment(String segment)
Returns whether the given string is valid as a segment in this path.
|
static boolean |
isValidWindowsPath(String path)
Returns whether the given string is syntactically correct as a path on
the Windows file system.
|
static boolean |
isValidWindowsSegment(String segment)
Returns whether the given string is valid as a segment in a path on the
Windows file system.
|
String |
lastSegment()
Returns the last segment of this path, or
null if it does not have any segments. |
IPath |
makeAbsolute()
Returns an absolute path with the segments and device id of this path.
|
IPath |
makeRelative()
Returns a relative path with the segments and device id of this path.
|
IPath |
makeRelativeTo(IPath base)
Returns a path equivalent to this path, but relative to the given base path if possible.
|
IPath |
makeUNC(boolean toUNC)
Return a new path which is the equivalent of this path converted to UNC
form (if the given boolean is true) or this path not as a UNC path (if the given
boolean is false).
|
int |
matchingFirstSegments(IPath anotherPath)
Returns a count of the number of segments which match in
this path and the given path (device ids are ignored),
comparing in increasing segment number order.
|
IPath |
removeFileExtension()
Returns a new path which is the same as this path but with
the file extension removed.
|
IPath |
removeFirstSegments(int count)
Returns a copy of this path with the given number of segments
removed from the beginning.
|
IPath |
removeLastSegments(int count)
Returns a copy of this path with the given number of segments
removed from the end.
|
IPath |
removeTrailingSeparator()
Returns a path with the same segments as this path
but with a trailing separator removed.
|
String |
segment(int index)
Returns the specified segment of this path, or
null if the path does not have such a segment. |
int |
segmentCount()
Returns the number of segments in this path.
|
String[] |
segments()
Returns the segments in this path in order.
|
IPath |
setDevice(String value)
Returns a new path which is the same as this path but with
the given device id.
|
File |
toFile()
Returns a
java.io.File corresponding to this path. |
String |
toOSString()
Returns a string representation of this path which uses the
platform-dependent path separator defined by
java.io.File . |
String |
toPortableString()
Returns a platform-neutral string representation of this path.
|
String |
toString()
Returns a string representation of this path, including its
device id.
|
IPath |
uptoSegment(int count)
Returns a copy of this path truncated after the
given number of segments.
|
public static final Path EMPTY
public static final Path ROOT
public Path(String fullPath)
fullPath
- the string pathisValidPath(String)
public Path(String device, String path)
device
- the device idpath
- the string pathisValidPath(String)
,
setDevice(String)
public static IPath fromOSString(String pathString)
pathString
- the portable string pathIPath.toPortableString()
public static IPath fromPortableString(String pathString)
IPath.toPortableString
.pathString
- the portable path stringIPath.toPortableString()
public static Path forPosix(String fullPath)
fullPath
- the string pathisValidPosixPath(String)
public static Path forWindows(String fullPath)
fullPath
- the string pathisValidWindowsPath(String)
public IPath addFileExtension(String extension)
IPath
The file extension portion is defined as the string following the last period (".") character in the last segment. The given extension should not include a leading ".".
addFileExtension
in interface IPath
extension
- the file extension to appendpublic IPath addTrailingSeparator()
IPath
If this path already has a trailing separator, this path is returned.
addTrailingSeparator
in interface IPath
IPath.hasTrailingSeparator()
,
IPath.removeTrailingSeparator()
public IPath append(IPath tail)
IPath
public IPath append(String tail)
IPath
append
in interface IPath
tail
- the string path to concatenateIPath.isValidPath(String)
public boolean equals(Object obj)
IPath
Equality for paths is defined to be: same sequence of segments, same absolute/relative status, and same device. Trailing separators are disregarded. The paths' file systems are disregarded. Paths are not generally considered equal to objects other than paths.
public String getDevice()
IPath
null
if this
path has no device id. Note that the result will end in ':'.getDevice
in interface IPath
null
IPath.setDevice(String)
public String getFileExtension()
IPath
null
if there is none.
The file extension portion is defined as the string following the last period (".") character in the last segment. If there is no period in the last segment, the path has no file extension portion. If the last segment ends in a period, the file extension portion is the empty string.
getFileExtension
in interface IPath
null
public boolean hasTrailingSeparator()
IPath
Note: In the root path ("/"), the separator is considered to be leading rather than trailing.
hasTrailingSeparator
in interface IPath
true
if this path has a trailing
separator, and false
otherwiseIPath.addTrailingSeparator()
,
IPath.removeTrailingSeparator()
public boolean isAbsolute()
IPath
Absolute paths start with a path separator.
A root path, like /
or C:/
,
is considered absolute. UNC paths are always absolute.
isAbsolute
in interface IPath
true
if this path is an absolute path,
and false
otherwisepublic boolean isEmpty()
IPath
public boolean isPrefixOf(IPath anotherPath)
IPath
An empty path is a prefix of all paths with the same device; a root path is a prefix of all absolute paths with the same device.
isPrefixOf
in interface IPath
anotherPath
- the other pathtrue
if this path is a prefix of the given path,
and false
otherwisepublic boolean isRoot()
IPath
The root path is the absolute non-UNC path with zero segments;
e.g., /
or C:/
.
The separator is considered a leading separator, not a trailing one.
public boolean isUNC()
IPath
Path.SEPARATOR
.public boolean isValidPath(String path)
IPath
isValidPath
in interface IPath
path
- the path to checktrue
if the given string is a valid path,
and false
otherwiseIPath.isValidSegment(String)
public static boolean isValidPosixPath(String path)
path
- the path to checktrue
if the given string is a valid path,
and false
otherwiseisValidPosixSegment(String)
public static boolean isValidWindowsPath(String path)
path
- the path to checktrue
if the given string is a valid path,
and false
otherwiseisValidWindowsSegment(String)
public boolean isValidSegment(String segment)
IPath
isValidSegment
in interface IPath
segment
- the path segment to checktrue
if the given path segment is valid,
and false
otherwisepublic static boolean isValidPosixSegment(String segment)
segment
- the path segment to checktrue
if the given path segment is valid,
and false
otherwisepublic static boolean isValidWindowsSegment(String segment)
segment
- the path segment to checktrue
if the given path segment is valid,
and false
otherwisepublic String lastSegment()
IPath
null
if it does not have any segments.lastSegment
in interface IPath
null
public IPath makeAbsolute()
IPath
makeAbsolute
in interface IPath
public IPath makeRelative()
IPath
makeRelative
in interface IPath
public IPath makeRelativeTo(IPath base)
The path is only made relative if the base path if both paths have the same device and have a non-zero length common prefix. If the paths have different devices, or no common prefix, then this path is simply returned. If the path is successfully made relative, then appending the returned path to the base will always produce a path equal to this path.
makeRelativeTo
in interface IPath
base
- The base path to make this path relative topublic IPath makeUNC(boolean toUNC)
IPath
Path.SEPARATOR
. If not UNC, the
first 2 characters of the returned path string will not be Path.SEPARATOR
.public int matchingFirstSegments(IPath anotherPath)
IPath
matchingFirstSegments
in interface IPath
anotherPath
- the other pathpublic IPath removeFileExtension()
IPath
The file extension portion is defined as the string following the last period (".") character in the last segment. If there is no period in the last segment, the path has no file extension portion. If the last segment ends in a period, the file extension portion is the empty string.
removeFileExtension
in interface IPath
public IPath removeFirstSegments(int count)
IPath
If the count is zero, this path is returned. This is the only case where
the returned path can be absolute. Use IPath.makeRelative()
if necessary.
If the count is greater than zero, the resulting path will always be a relative path.
If the count equals or exceeds the number of segments in this path, an empty relative path is returned.
removeFirstSegments
in interface IPath
count
- the number of segments to removepublic IPath removeLastSegments(int count)
IPath
If this path has a trailing separator, it will still have a trailing separator after the last segments are removed (assuming there are some segments left). If there is no trailing separator, the result will not have a trailing separator. If the number equals or exceeds the number of segments in this path, a path with no segments is returned.
removeLastSegments
in interface IPath
count
- the number of segments to removepublic IPath removeTrailingSeparator()
IPath
If this path does not have a trailing separator, this path is returned.
removeTrailingSeparator
in interface IPath
IPath.addTrailingSeparator()
,
IPath.hasTrailingSeparator()
public String segment(int index)
IPath
null
if the path does not have such a segment.public int segmentCount()
IPath
Note that both root and empty paths have 0 segments.
segmentCount
in interface IPath
public String[] segments()
IPath
public IPath setDevice(String value)
IPath
null
.
For example, "C:" and "Server/Volume:" are typical device ids.
setDevice
in interface IPath
value
- the device id or null
IPath.getDevice()
public File toFile()
IPath
java.io.File
corresponding to this path.public String toOSString()
IPath
java.io.File
.
This method is like toString()
except that the
latter always uses the same separator (/
) regardless of platform.
This string is suitable for passing to java.io.File(String)
.
toOSString
in interface IPath
public String toPortableString()
IPath
Path#fromPortableString(String)
constructor to produce the exact same path on any platform.
This string is suitable for passing to Path#fromPortableString(String)
.
toPortableString
in interface IPath
fromPortableString(String)
public String toString()
IPath
Example result strings (without and with device id):
"/foo/bar.txt" "bar.txt" "/foo/" "foo/" "" "/" "C:/foo/bar.txt" "C:bar.txt" "C:/foo/" "C:foo/" "C:" "C:/"This string is suitable for passing to
Path(String)
.
public IPath uptoSegment(int count)
IPath
If this path has a trailing separator, the result will too (assuming there are some segments left). If there is no trailing separator, the result will not have a trailing separator. Copying up to segment zero simply means making an copy with no path segments.
uptoSegment
in interface IPath
count
- the segment number at which to truncate the path
Copyright (c) 2000, 2016 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.