Class Path

java.lang.Object
org.rapidcontext.core.storage.Path

public class Path extends Object
A data storage path. This class encapsulates the path (directory plus name) of an object, a file or an index. It also provides some simple help methods to access and work with the path and to locate the object addressed by it.
Version:
1.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The prefix for hidden path elements.
    static final Path
    A root path constant.
  • Method Summary

    Modifier and Type
    Method
    Description
    child(String name, boolean isIndex)
    Creates a new path to a child index or object.
    int
    Returns the directory depth.
    boolean
    Checks if this path is identical to another path.
    static Path
    from(String path)
    Creates a new path from a string representation (similar to a file system path).
    int
    Returns a hash code for this object.
    boolean
    Checks if this path contains a hidden part.
    boolean
    Checks if this path corresponds to an index.
    boolean
    Checks if this path corresponds to the root index.
    int
    Returns the path length.
    Returns the name of the last element in the path.
    name(int pos)
    Returns the name of the path element at the specified position.
    Creates a new path to the parent index.
    Creates a new path with the specified prefix removed.
    static Path
    resolve(Path parent, String path)
    Resolves a path starting at a parent path.
    static Path
    resolve(Path parent, Path path)
    Resolves a path starting at a parent path.
    Creates a new path to a sibling index or object.
    boolean
    Checks if this path starts with the specified path.
    toIdent(int pos)
    Returns an object identifier based on this path.
    Returns a string representation of this object.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ROOT

      public static final Path ROOT
      A root path constant. Note that several paths may be root paths, so this is not a unique instance. It is only here for convenience.
    • PREFIX_HIDDEN

      public static final String PREFIX_HIDDEN
      The prefix for hidden path elements. These paths are normally not included in listings or possible to load.
      See Also:
  • Method Details

    • from

      public static Path from(String path)
      Creates a new path from a string representation (similar to a file system path).
      Parameters:
      path - the string path to parse
      Returns:
      the path created
    • resolve

      public static Path resolve(Path parent, String path)
      Resolves a path starting at a parent path. Leading and duplicate separator ('/') characters are ignored. A trailing separator ('/') character indicates that the resulting path is an index. Any relative ('../') path components will be resolved to the extent possible.
      Parameters:
      parent - the parent index path
      path - the descendant path
      Returns:
      the resolved path
    • resolve

      public static Path resolve(Path parent, Path path)
      Resolves a path starting at a parent path. Any relative ('../') path components will be resolved to the extent possible.
      Parameters:
      parent - the parent index path
      path - the descendant path
      Returns:
      the resolved path
    • toString

      public String toString()
      Returns a string representation of this object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this object
    • toIdent

      public String toIdent(int pos)
      Returns an object identifier based on this path. The identifier will start at the specified position in this path.
      Parameters:
      pos - the position, from 0 to length()
      Returns:
      an object identifier for this path (without prefix)
    • equals

      public boolean equals(Object obj)
      Checks if this path is identical to another path. The two paths will be considered equal if they have the same length, all elements are equal and the index flag is identical.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare with
      Returns:
      true if the two paths are equal, or false otherwise
    • hashCode

      public int hashCode()
      Returns a hash code for this object.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code for this object
    • isRoot

      public boolean isRoot()
      Checks if this path corresponds to the root index.
      Returns:
      true if the path is for the root index, or false otherwise
    • isIndex

      public boolean isIndex()
      Checks if this path corresponds to an index.
      Returns:
      true if the path is an index, or false otherwise
    • isHidden

      public boolean isHidden()
      Checks if this path contains a hidden part.
      Returns:
      true if the path is hidden, or false otherwise
    • startsWith

      public boolean startsWith(Path path)
      Checks if this path starts with the specified path. All the path elements must match up to the length of the specified path. As a special case, this method will return true if the two paths are identical. It will also return true for a null path.
      Parameters:
      path - the path to compare with
      Returns:
      true if this path starts with the specified path, or false otherwise
    • depth

      public int depth()
      Returns the directory depth. The root index, and any objects located directly there, have depth zero (0). For each additional sub-level traversed, the depth is increased by one (1). Objects and files in the storage tree will not affect the depth.
      Returns:
      the path directory depth
    • length

      public int length()
      Returns the path length. The length contains the number of elements in the path, counting both indices and any named object or file. The length is always greater or equal to the depth.
      Returns:
      the path length
    • name

      public String name()
      Returns the name of the last element in the path. This is normally the object name.
      Returns:
      the object or index name, or null for the root index
    • name

      public String name(int pos)
      Returns the name of the path element at the specified position. A zero position will return the first element traversed, i.e. the one located in the root.
      Parameters:
      pos - the position, from 0 to length()
      Returns:
      the name of the element at the specified position, or null if the position is out of range
    • parent

      public Path parent()
      Creates a new path to the parent index.
      Returns:
      a new path to the parent index
    • child

      public Path child(String name, boolean isIndex)
      Creates a new path to a child index or object.
      Parameters:
      name - the child name
      isIndex - the index flag
      Returns:
      a new path to a child index or object
    • sibling

      public Path sibling(String name)
      Creates a new path to a sibling index or object.
      Parameters:
      name - the sibling name
      Returns:
      a new path to a sibling index or object
    • removePrefix

      public Path removePrefix(Path prefix)
      Creates a new path with the specified prefix removed.
      Parameters:
      prefix - the prefix path to remove
      Returns:
      a new path with the common prefix removed