Class PathType

  • Direct Known Subclasses:
    UnixPathType, WindowsPathType

    public abstract class PathType
    extends java.lang.Object
    An object defining a specific type of path. Knows how to parse strings to a path and how to render a path as a string as well as what the path separator is and what other separators are recognized when parsing paths.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  PathType.ParseResult
      Simple result of parsing a path.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected PathType​(boolean allowsMultipleRoots, char separator, char... otherSeparators)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      boolean allowsMultipleRoots()
      Returns whether or not this type of path allows multiple root directories.
      private static void appendToRegex​(char separator, java.lang.StringBuilder patternBuilder)  
      private static com.google.common.base.Splitter createSplitter​(char separator, char... otherSeparators)  
      protected PathType.ParseResult emptyPath()
      Returns an empty path.
      PathType.ParseResult fromUri​(java.net.URI uri)
      Parses a path from the given URI.
      java.lang.String getOtherSeparators()
      Returns the other separators that are recognized when parsing a path.
      java.lang.String getSeparator()
      Returns the canonical separator for this path type.
      private static boolean isRegexReserved​(char c)  
      com.google.common.base.Joiner joiner()
      Returns the path joiner for this path type.
      abstract PathType.ParseResult parsePath​(java.lang.String path)
      Parses the given strings as a path.
      protected abstract PathType.ParseResult parseUriPath​(java.lang.String uriPath)
      Parses a path from the given URI path.
      com.google.common.base.Splitter splitter()
      Returns the path splitter for this path type.
      java.lang.String toString()  
      abstract java.lang.String toString​(@Nullable java.lang.String root, java.lang.Iterable<java.lang.String> names)
      Returns the string form of the given path.
      java.net.URI toUri​(java.net.URI fileSystemUri, java.lang.String root, java.lang.Iterable<java.lang.String> names, boolean directory)
      Creates a URI for the path with the given root and names in the file system with the given URI.
      protected abstract java.lang.String toUriPath​(java.lang.String root, java.lang.Iterable<java.lang.String> names, boolean directory)
      Returns the string form of the given path for use in the path part of a URI.
      static PathType unix()
      Returns a Unix-style path type.
      static PathType windows()
      Returns a Windows-style path type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • allowsMultipleRoots

        private final boolean allowsMultipleRoots
      • separator

        private final java.lang.String separator
      • otherSeparators

        private final java.lang.String otherSeparators
      • joiner

        private final com.google.common.base.Joiner joiner
      • splitter

        private final com.google.common.base.Splitter splitter
      • regexReservedChars

        private static final char[] regexReservedChars
    • Constructor Detail

      • PathType

        protected PathType​(boolean allowsMultipleRoots,
                           char separator,
                           char... otherSeparators)
    • Method Detail

      • unix

        public static PathType unix()
        Returns a Unix-style path type. "/" is both the root and the only separator. Any path starting with "/" is considered absolute. The nul character ('\0') is disallowed in paths.
      • windows

        public static PathType windows()
        Returns a Windows-style path type. The canonical separator character is "\". "/" is also treated as a separator when parsing paths.

        As much as possible, this implementation follows the information provided in this article. Paths with drive-letter roots (e.g. "C:\") and paths with UNC roots (e.g. "\\host\share\") are supported.

        Two Windows path features are not currently supported as they are too Windows-specific:

        • Relative paths containing a drive-letter root, for example "C:" or "C:foo\bar". Such paths have a root component and optionally have names, but are relative paths, relative to the working directory of the drive identified by the root.
        • Absolute paths with no root, for example "\foo\bar". Such paths are absolute paths on the current drive.
      • isRegexReserved

        private static boolean isRegexReserved​(char c)
      • createSplitter

        private static com.google.common.base.Splitter createSplitter​(char separator,
                                                                      char... otherSeparators)
      • appendToRegex

        private static void appendToRegex​(char separator,
                                          java.lang.StringBuilder patternBuilder)
      • allowsMultipleRoots

        public final boolean allowsMultipleRoots()
        Returns whether or not this type of path allows multiple root directories.
      • getSeparator

        public final java.lang.String getSeparator()
        Returns the canonical separator for this path type. The returned string always has a length of one.
      • getOtherSeparators

        public final java.lang.String getOtherSeparators()
        Returns the other separators that are recognized when parsing a path. If no other separators are recognized, the empty string is returned.
      • joiner

        public final com.google.common.base.Joiner joiner()
        Returns the path joiner for this path type.
      • splitter

        public final com.google.common.base.Splitter splitter()
        Returns the path splitter for this path type.
      • parsePath

        public abstract PathType.ParseResult parsePath​(java.lang.String path)
        Parses the given strings as a path.
        Throws:
        java.nio.file.InvalidPathException - if the path isn't valid for this path type
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        public abstract java.lang.String toString​(@Nullable java.lang.String root,
                                                  java.lang.Iterable<java.lang.String> names)
        Returns the string form of the given path.
      • toUriPath

        protected abstract java.lang.String toUriPath​(java.lang.String root,
                                                      java.lang.Iterable<java.lang.String> names,
                                                      boolean directory)
        Returns the string form of the given path for use in the path part of a URI. The root element is not nullable as the path must be absolute. The elements of the returned path do not need to be escaped. The directory boolean indicates whether the file the URI is for is known to be a directory.
      • parseUriPath

        protected abstract PathType.ParseResult parseUriPath​(java.lang.String uriPath)
        Parses a path from the given URI path.
        Throws:
        java.nio.file.InvalidPathException - if the given path isn't valid for this path type
      • toUri

        public final java.net.URI toUri​(java.net.URI fileSystemUri,
                                        java.lang.String root,
                                        java.lang.Iterable<java.lang.String> names,
                                        boolean directory)
        Creates a URI for the path with the given root and names in the file system with the given URI.
      • fromUri

        public final PathType.ParseResult fromUri​(java.net.URI uri)
        Parses a path from the given URI.