Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.eclipse.core.runtime
Class Path  view Path download Path.java

java.lang.Object
  extended byorg.eclipse.core.runtime.Path
All Implemented Interfaces:
java.lang.Cloneable, IPath

public class Path
extends java.lang.Object
implements IPath, java.lang.Cloneable

The standard implementation of the 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 is not intended to be subclassed by clients but may be instantiated.


Field Summary
private static int ALL_SEPARATORS
           
private  java.lang.String device
          The device id string.
static Path EMPTY
          Constant value containing the empty path with no device.
private static java.lang.String EMPTY_STRING
          Constant empty string value.
private static java.lang.String[] EMPTY_STRING_ARRAY
           
private static int HAS_LEADING
          masks for separator values
private static int HAS_TRAILING
           
private static int HASH_MASK
          Mask for all bits that are involved in the hashcode
private static int IS_UNC
           
private static java.lang.String[] NO_SEGMENTS
          Constant value indicating no segments
static Path ROOT
          Constant value containing the root path with no device.
private static java.lang.String ROOT_STRING
          Constant root path string ("/").
private  java.lang.String[] segments
          The path segments
private  int separators
          flags indicating separators (has leading, is UNC, has trailing)
 
Fields inherited from interface org.eclipse.core.runtime.IPath
DEVICE_SEPARATOR, SEPARATOR
 
Constructor Summary
private Path()
           
  Path(java.lang.String fullPath)
          Constructs a new path from the given string path.
  Path(java.lang.String device, java.lang.String path)
          Constructs a new path from the given device id and string path.
private Path(java.lang.String device, java.lang.String[] segments, int _separators)
           
 
Method Summary
 IPath addFileExtension(java.lang.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(java.lang.String tail)
          Returns the canonicalized path obtained from the concatenation of the given string path to the end of this path.
private  boolean canonicalize()
          Destructively converts this path to its canonical form.
 java.lang.Object clone()
          Returns a copy of this path.
private  void collapseParentReferences()
          Destructively removes all occurrences of ".." segments from this path.
private  java.lang.String collapseSlashes(java.lang.String path)
          Removes duplicate slashes from the given path, with the exception of leading double slash which represents a UNC path.
private  int computeHashCode()
           
private  int computeLength()
           
private  int computeSegmentCount(java.lang.String path)
           
private  java.lang.String[] computeSegments(java.lang.String path)
          Computes the segment array for the given canonicalized path.
 boolean equals(java.lang.Object obj)
          Returns whether this path equals the given object.
 java.lang.String getDevice()
          Returns the device id for this path, or null if this path has no device id.
 java.lang.String getFileExtension()
          Returns the file extension portion of this path, or null if there is none.
 int hashCode()
          Get a value that represents this Object, as uniquely as possible within the confines of an int.
 boolean hasTrailingSeparator()
          Returns whether this path has a trailing separator.
private  void initialize(java.lang.String device, java.lang.String fullPath)
           
 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(java.lang.String path)
          Returns whether the given string is syntactically correct as a path.
 boolean isValidSegment(java.lang.String segment)
          Returns whether the given string is valid as a segment in a path.
 java.lang.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 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.
 java.lang.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.
 java.lang.String[] segments()
          Returns the segments in this path in order.
 IPath setDevice(java.lang.String value)
          Returns a new path which is the same as this path but with the given device id.
 java.io.File toFile()
          Returns a java.io.File corresponding to this path.
 java.lang.String toOSString()
          Returns a string representation of this path which uses the platform-dependent path separator defined by java.io.File.
 java.lang.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.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

segments

private java.lang.String[] segments
The path segments


device

private java.lang.String device
The device id string. May be null if there is no device.


separators

private int separators
flags indicating separators (has leading, is UNC, has trailing)


HAS_LEADING

private static final int HAS_LEADING
masks for separator values

See Also:
Constant Field Values

IS_UNC

private static final int IS_UNC
See Also:
Constant Field Values

HAS_TRAILING

private static final int HAS_TRAILING
See Also:
Constant Field Values

ALL_SEPARATORS

private static final int ALL_SEPARATORS
See Also:
Constant Field Values

HASH_MASK

private static final int HASH_MASK
Mask for all bits that are involved in the hashcode

See Also:
Constant Field Values

NO_SEGMENTS

private static final java.lang.String[] NO_SEGMENTS
Constant value indicating no segments


ROOT_STRING

private static final java.lang.String ROOT_STRING
Constant root path string ("/").

See Also:
Constant Field Values

ROOT

public static final Path ROOT
Constant value containing the root path with no device.


EMPTY_STRING

private static final java.lang.String EMPTY_STRING
Constant empty string value.

See Also:
Constant Field Values

EMPTY_STRING_ARRAY

private static final java.lang.String[] EMPTY_STRING_ARRAY

EMPTY

public static final Path EMPTY
Constant value containing the empty path with no device.

Constructor Detail

Path

private Path()

Path

private Path(java.lang.String device,
             java.lang.String[] segments,
             int _separators)

Path

public Path(java.lang.String fullPath)
Constructs a new path from the given string path. The given string path must be valid. The path is canonicalized and double slashes are removed except at the beginning. (to handle UNC paths) All backslashes ('\') are replaced with forward slashes. ('/')


Path

public Path(java.lang.String device,
            java.lang.String path)
Constructs a new path from the given device id and string path. The given string path must be valid. The path is canonicalized and double slashes are removed except at the beginning (to handle UNC paths). All backslashes ('\') are replaced with forward slashes. ('/')

Method Detail

addFileExtension

public IPath addFileExtension(java.lang.String extension)
Description copied from interface: IPath
Returns a new path which is the same as this path but with the given file extension added. If this path is empty, root or has a trailing separator, this path is returned. If this path already has an extension, the existing extension is left and the given extension simply appended. Clients wishing to replace the current extension should first remove the extension and then add the desired one.

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 ".".

Specified by:
addFileExtension in interface IPath

addTrailingSeparator

public IPath addTrailingSeparator()
Description copied from interface: IPath
Returns a path with the same segments as this path but with a trailing separator added. This path must have at least one segment.

If this path already has a trailing separator, this path is returned.

Specified by:
addTrailingSeparator in interface IPath

append

public IPath append(java.lang.String tail)
Description copied from interface: IPath
Returns the canonicalized path obtained from the concatenation of the given string path to the end of this path. The given string path must be a valid path. If it has a trailing separator, the result will have a trailing separator. The device id of this path is preserved (the one of the given string is ignored). Duplicate slashes are removed from the path except at the beginning where the path is considered to be UNC.

Specified by:
append in interface IPath

append

public IPath append(IPath tail)
Description copied from interface: IPath
Returns the canonicalized path obtained from the concatenation of the given path's segments to the end of this path. If the given path has a trailing separator, the result will have a trailing separator. The device id of this path is preserved (the one of the given path is ignored). Duplicate slashes are removed from the path except at the beginning where the path is considered to be UNC.

Specified by:
append in interface IPath

canonicalize

private boolean canonicalize()
Destructively converts this path to its canonical form.

In its canonical form, a path does not have any "." segments, and parent references ("..") are collapsed where possible.


clone

public java.lang.Object clone()
Description copied from interface: IPath
Returns a copy of this path.

Specified by:
clone in interface IPath

collapseParentReferences

private void collapseParentReferences()
Destructively removes all occurrences of ".." segments from this path.


collapseSlashes

private java.lang.String collapseSlashes(java.lang.String path)
Removes duplicate slashes from the given path, with the exception of leading double slash which represents a UNC path.


computeHashCode

private int computeHashCode()

computeLength

private int computeLength()

computeSegmentCount

private int computeSegmentCount(java.lang.String path)

computeSegments

private java.lang.String[] computeSegments(java.lang.String path)
Computes the segment array for the given canonicalized path.


equals

public boolean equals(java.lang.Object obj)
Description copied from interface: IPath
Returns whether this path equals the given object.

Equality for paths is defined to be: same sequence of segments, same absolute/relative status, and same device. Trailing separators are disregarded. Paths are not generally considered equal to objects other than paths.

Specified by:
equals in interface IPath

getDevice

public java.lang.String getDevice()
Description copied from interface: IPath
Returns the device id for this path, or null if this path has no device id. Note that the result will end in ':'.

Specified by:
getDevice in interface IPath

getFileExtension

public java.lang.String getFileExtension()
Description copied from interface: IPath
Returns the file extension portion of this path, or 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.

Specified by:
getFileExtension in interface IPath

hashCode

public int hashCode()
Description copied from class: java.lang.Object
Get a value that represents this Object, as uniquely as possible within the confines of an int.

There are some requirements on this method which subclasses must follow:

  • Semantic equality implies identical hashcodes. In other words, if a.equals(b) is true, then a.hashCode() == b.hashCode() must be as well. However, the reverse is not necessarily true, and two objects may have the same hashcode without being equal.
  • It must be consistent. Whichever value o.hashCode() returns on the first invocation must be the value returned on all later invocations as long as the object exists. Notice, however, that the result of hashCode may change between separate executions of a Virtual Machine, because it is not invoked on the same object.

Notice that since hashCode is used in java.util.Hashtable and other hashing classes, a poor implementation will degrade the performance of hashing (so don't blindly implement it as returning a constant!). Also, if calculating the hash is time-consuming, a class may consider caching the results.

The default implementation returns System.identityHashCode(this)


hasTrailingSeparator

public boolean hasTrailingSeparator()
Description copied from interface: IPath
Returns whether this path has a trailing separator.

Note: In the root path ("/"), the separator is considered to be leading rather than trailing.

Specified by:
hasTrailingSeparator in interface IPath

initialize

private void initialize(java.lang.String device,
                        java.lang.String fullPath)

isAbsolute

public boolean isAbsolute()
Description copied from interface: IPath
Returns whether this path is an absolute path (ignoring any device id).

Absolute paths start with a path separator. A root path, like / or C:/, is considered absolute. UNC paths are always absolute.

Specified by:
isAbsolute in interface IPath

isEmpty

public boolean isEmpty()
Description copied from interface: IPath
Returns whether this path has no segments and is not a root path.

Specified by:
isEmpty in interface IPath

isPrefixOf

public boolean isPrefixOf(IPath anotherPath)
Description copied from interface: IPath
Returns whether this path is a prefix of the given path. To be a prefix, this path's segments must appear in the argument path in the same order, and their device ids must match.

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.

Specified by:
isPrefixOf in interface IPath

isRoot

public boolean isRoot()
Description copied from interface: IPath
Returns whether this path is a root path.

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.

Specified by:
isRoot in interface IPath

isUNC

public boolean isUNC()
Description copied from interface: IPath
Returns a boolean value indicating whether or not this path is considered to be in UNC form. Return false if this path has a device set or if the first 2 characters of the path string are not Path.SEPARATOR.

Specified by:
isUNC in interface IPath

isValidPath

public boolean isValidPath(java.lang.String path)
Description copied from interface: IPath
Returns whether the given string is syntactically correct as a path. The device id is the prefix up to and including the first ":"; the path proper is everything to the right of it, or the entire string if there is no ":". The device id is not checked for validity; the path proper is correct if each of the segments in its canonicalized form is valid.

Specified by:
isValidPath in interface IPath

isValidSegment

public boolean isValidSegment(java.lang.String segment)
Description copied from interface: IPath
Returns whether the given string is valid as a segment in a path. The rules for valid segments are as follows:
  • the empty string is not valid
  • any string containing the colon character (":") is not valid
  • any string containing the slash character ("/") is not valid
  • any string containing the backslash character ("\") is not valid
  • any string starting or ending with a whitespace character is not valid
  • all other strings are valid

Specified by:
isValidSegment in interface IPath

lastSegment

public java.lang.String lastSegment()
Description copied from interface: IPath
Returns the last segment of this path, or null if it does not have any segments.

Specified by:
lastSegment in interface IPath

makeAbsolute

public IPath makeAbsolute()
Description copied from interface: IPath
Returns an absolute path with the segments and device id of this path. Absolute paths start with a path separator. If this path is absolute, it is simply returned.

Specified by:
makeAbsolute in interface IPath

makeRelative

public IPath makeRelative()
Description copied from interface: IPath
Returns a relative path with the segments and device id of this path. Absolute paths start with a path separator and relative paths do not. If this path is relative, it is simply returned.

Specified by:
makeRelative in interface IPath

makeUNC

public IPath makeUNC(boolean toUNC)
Description copied from interface: IPath
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). If UNC, the returned path will not have a device and the first 2 characters of the path string will be Path.SEPARATOR. If not UNC, the first 2 characters of the returned path string will not be Path.SEPARATOR.

Specified by:
makeUNC in interface IPath

matchingFirstSegments

public int matchingFirstSegments(IPath anotherPath)
Description copied from interface: IPath
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.

Specified by:
matchingFirstSegments in interface IPath

removeFileExtension

public IPath removeFileExtension()
Description copied from interface: IPath
Returns a new path which is the same as this path but with the file extension removed. If this path does not have an extension, this path is returned.

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.

Specified by:
removeFileExtension in interface IPath

removeFirstSegments

public IPath removeFirstSegments(int count)
Description copied from interface: IPath
Returns a copy of this path with the given number of segments removed from the beginning. The device id is preserved. The number must be greater or equal zero. If the count is zero, this path is returned. The resulting path will always be a relative path with respect to this path. If the number equals or exceeds the number of segments in this path, an empty relative path is returned.

Specified by:
removeFirstSegments in interface IPath

removeLastSegments

public IPath removeLastSegments(int count)
Description copied from interface: IPath
Returns a copy of this path with the given number of segments removed from the end. The device id is preserved. The number must be greater or equal zero. If the count is zero, this path is returned.

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, an empty path is returned.

Specified by:
removeLastSegments in interface IPath

removeTrailingSeparator

public IPath removeTrailingSeparator()
Description copied from interface: IPath
Returns a path with the same segments as this path but with a trailing separator removed. Does nothing if this path does not have at least one segment. The device id is preserved.

If this path does not have a trailing separator, this path is returned.

Specified by:
removeTrailingSeparator in interface IPath

segment

public java.lang.String segment(int index)
Description copied from interface: IPath
Returns the specified segment of this path, or null if the path does not have such a segment.

Specified by:
segment in interface IPath

segmentCount

public int segmentCount()
Description copied from interface: IPath
Returns the number of segments in this path.

Note that both root and empty paths have 0 segments.

Specified by:
segmentCount in interface IPath

segments

public java.lang.String[] segments()
Description copied from interface: IPath
Returns the segments in this path in order.

Specified by:
segments in interface IPath

setDevice

public IPath setDevice(java.lang.String value)
Description copied from interface: IPath
Returns a new path which is the same as this path but with the given device id. The device id must end with a ":". A device independent path is obtained by passing null.

For example, "C:" and "Server/Volume:" are typical device ids.

Specified by:
setDevice in interface IPath

toFile

public java.io.File toFile()
Description copied from interface: IPath
Returns a java.io.File corresponding to this path.

Specified by:
toFile in interface IPath

toOSString

public java.lang.String toOSString()
Description copied from interface: IPath
Returns a string representation of this path which uses the platform-dependent path separator defined by 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).

Specified by:
toOSString in interface IPath

toString

public java.lang.String toString()
Description copied from interface: IPath
Returns a string representation of this path, including its device id. The same separator, "/", is used on all platforms.

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).

Specified by:
toString in interface IPath

uptoSegment

public IPath uptoSegment(int count)
Description copied from interface: IPath
Returns a copy of this path truncated after the given number of segments. The number must not be negative. The device id is preserved.

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.

Specified by:
uptoSegment in interface IPath