public class Path extends java.lang.Object implements java.lang.Iterable<java.lang.String>, java.lang.Comparable<Path>
This solution matches the way URLs represents paths. The string representation uses forward slashes (/) to separate subpaths. The root is represented as a single slash (/). Other paths look like /path/to/something.
Embedded slashes and other characters that are illegal in URLs are encoded the same way they are in URLs, using the recommended UTF-8 encoding. This may make the string representations look a little strange, since even spaces will be encoded.
Since one of the most common use for paths is to represent the locations of files, there are also methods to translate between Paths and the OS-specific String representation of a file path. Generally where you see the term "path string" that means the URL-encoded style, while "file string" means a file-encoded style.
See the the Wikipedia entry for Path for a description of the path concept.
Copyright 2005-2006 Partner Software, Inc.
| Constructor and Description |
|---|
Path()
Constructs a path representing the current relative node ("./").
|
Path(boolean absolute,
java.util.List<java.lang.String> parts)
Create a relative path from the given parts.
|
Path(java.util.List<java.lang.String> parts)
Create a relative path from the given parts.
|
Path(PathBuilder builder)
Create a Path from a PathBuilder.
|
Path(Path start,
Path subpath)
Creates a path derived from the given root and relative subpath.
|
Path(Path start,
java.lang.String part)
Creates a path derived from the given root and part.
|
Path(java.lang.String... parts)
Create a relative path from the given parts.
|
Path(java.lang.String pathString)
Constructs a Path based on the String path representation (URL-encoded).
|
| Modifier and Type | Method and Description |
|---|---|
Path |
childNamed(java.lang.String childName)
Creates a new instance of Path representing the named "child" of this
path.
|
int |
compareTo(Path nother) |
boolean |
contains(java.lang.String sub)
Returns true if and only if this Path contains the specified string
|
Path |
descendentFor(Path sub)
Creates a new instance of Path representing the named "descendent" of
this path.
|
Path |
descendentFor(java.lang.String pathString)
Creates a new instance of Path representing the named "descendent" of
this path.
|
boolean |
equals(java.lang.Object nother) |
Path |
findAncestorNamed(java.lang.String name)
Returns the closest ancestor of this Path with the given case-insensitive name.
|
static Path |
forFile(java.io.File file)
Creates a Path object from the given File.
|
static Path |
forFileString(java.lang.String filePath)
Creates a Path object from the given path, expressed in file notation
(e.g.
|
static Path |
forPathString(java.lang.String pathString)
Creates a Path object based on a URL-encoded (similar to Unix, with
forward slashes) String.
|
static Path |
forUrl(java.net.URL url)
Creates a Path object based on the given URL.
|
java.lang.String |
getBaseName()
Gets just the base name of the file name (last part without the
extension).
|
java.lang.String |
getExtension()
Returns the file extension, generally the part including and after the LAST period
(.).
|
java.lang.String |
getFirstPart()
Returns the first path part.
|
java.lang.String |
getHead()
This is a class list operator.
|
java.lang.String |
getLastPart()
Returns the last path part.
|
int |
getLength() |
java.lang.String |
getName()
Gets the last part as a single string representing the full file name
(including extension, if any).
|
Path |
getParent()
Creates a new instance of Path representing the "parent" of this path.
|
java.lang.String |
getPart(int index) |
Path |
getTail()
This is a class list operator.
|
int |
hashCode() |
int |
indexOf(java.lang.String sub)
Returns the index of the part of the path matching the argument sub, if no match returns -1
|
int |
indexOf(java.lang.String sub,
int fromIndex)
Returns the index of the part of the path matching the argument sub, beginning at the index specified by the arg fromIndex
if no match returns -1
|
boolean |
isAbsolute()
Returns true if this is an absolute path.
|
boolean |
isAncestorOf(Path nother)
Returns true if this path is an ancestor of the one given.
|
boolean |
isDescendentOf(Path nother)
Returns true if this path is a descendent of the one given.
|
java.util.Iterator<java.lang.String> |
iterator() |
int |
lastIndexOf(java.lang.String sub)
Returns the index of the last occurring part of the path matching the argument sub
if no match returns -1
|
java.util.List<java.lang.String> |
listParts()
Returns the parts as a list.
|
Path |
relativeTo(Path ancestor)
Returns a path relative to the one given.
|
int |
size() |
Path |
subpath(int first)
This is intended to work like String.substring().
|
Path |
subpath(int first,
int last)
This is intended to work like String.substring().
|
java.lang.String[] |
toArray()
Returns an array of Strings representing the parts of the path.
|
java.lang.String |
toFileString()
Gets the path as a single, file-encoded String.
|
java.lang.String |
toPathString()
Gets the path as a single, URL-encoded String.
|
java.lang.String |
toString() |
Path |
withAbsolute(boolean isit) |
public Path()
public Path(java.lang.String pathString)
pathString - String representation, "/" delimited, of the path.public Path(Path start, Path subpath)
start - a path representing the starting nodesubpath - relative path to the starting nodepublic Path(Path start, java.lang.String part)
start - a path representing the starting nodepart - next part in the pathpublic Path(PathBuilder builder)
builder - builder that built the pathpublic Path(java.lang.String... parts)
public Path(boolean absolute,
java.util.List<java.lang.String> parts)
public Path(java.util.List<java.lang.String> parts)
public static Path forFileString(java.lang.String filePath)
public static Path forFile(java.io.File file)
public static Path forPathString(java.lang.String pathString)
public static Path forUrl(java.net.URL url)
public Path getParent()
public Path childNamed(java.lang.String childName)
public Path descendentFor(Path sub)
public Path descendentFor(java.lang.String pathString)
public Path relativeTo(Path ancestor)
public boolean isAbsolute()
public boolean isDescendentOf(Path nother)
public boolean isAncestorOf(Path nother)
public java.lang.String toPathString()
public java.lang.String toFileString()
public java.lang.String getName()
public java.lang.String getBaseName()
public java.lang.String getExtension()
public java.lang.String getLastPart()
public java.lang.String getFirstPart()
public int getLength()
public java.lang.String getPart(int index)
public int indexOf(java.lang.String sub)
sub - public int indexOf(java.lang.String sub,
int fromIndex)
sub - public int lastIndexOf(java.lang.String sub)
sub - public boolean contains(java.lang.String sub)
sub - public Path subpath(int first, int last)
first - the index of the first path element to copy.last - the index after the last element to copy.public Path subpath(int first)
first - the index of the first path element to copy.public java.lang.String getHead()
public Path getTail()
public java.lang.String[] toArray()
public java.util.List<java.lang.String> listParts()
public int size()
public Path withAbsolute(boolean isit)
public java.util.Iterator<java.lang.String> iterator()
iterator in interface java.lang.Iterable<java.lang.String>public java.lang.String toString()
toString in class java.lang.Objectpublic int compareTo(Path nother)
compareTo in interface java.lang.Comparable<Path>public boolean equals(java.lang.Object nother)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic Path findAncestorNamed(java.lang.String name)
name - case-insensitive ancestor name