public class MorePaths extends Object
Path
. If a function is going to take a ProjectFilesystem
, then it should be in MoreProjectFilesystems
instead.Modifier and Type | Method and Description |
---|---|
static com.google.common.io.ByteSource |
asByteSource(Path path) |
static int |
commonSuffixLength(Path a,
Path b) |
static void |
createSymLink(WindowsFS winFS,
Path symLink,
Path target)
Creates a symlink.
|
static Path |
dropInternalCaches(Path p)
Drop the cache in Path object.
|
static Path |
emptyOf(Path path)
Return empty path with the same filesystem as provided path
|
static Path |
expandHomeDir(Path path)
Expands "~/foo" into "/home/zuck/foo".
|
static com.google.common.collect.ImmutableSet<Path> |
filterForSubpaths(Iterable<Path> paths,
Path root)
Filters out
Path objects from paths that aren't a subpath of root and
returns a set of paths relative to root . |
static Path |
fixPath(Path p)
Drop any "." parts (useless).
|
static String |
getFileExtension(Path path) |
static String |
getNameWithoutExtension(Path file) |
static Path |
getParentOrEmpty(Path path) |
static RelPath |
getParentOrEmpty(RelPath path) |
static Path |
getRelativePath(Path path,
Path baseDir)
Get the path of a file relative to a base directory.
|
static String |
getWindowsLongPathString(Path path)
Converts a path to an absolute Windows 'long path' as a string.
|
static boolean |
isDirectory(Path path,
LinkOption... linkOptions)
Returns whether a path is a directory..
|
static boolean |
isEmpty(Path path)
Return true if provided path is empty path ("")
|
static AbsPath |
normalize(AbsPath path)
Type-safer version of
normalize(Path) . |
static Path |
normalize(Path path)
Get a path without unnecessary path parts.
|
static String |
pathWithPlatformSeparators(Path path) |
static String |
pathWithPlatformSeparators(String path) |
static Path |
relativize(Path path1,
Path path2)
Get a relative path from path1 to path2, first normalizing each path.
|
static RelPath |
relativize(PathWrapper path1,
PathWrapper path2) |
static Path |
relativizeWithDotDotSupport(Path basePath,
Path childPath)
Returns a child path relative to a base path.
|
static Optional<Pair<Path,com.google.common.collect.ImmutableList<Path>>> |
splitOnCommonPrefix(Iterable<Path> paths) |
static Pair<Path,Path> |
stripCommonSuffix(Path a,
Path b) |
static String |
stripPathPrefixAndExtension(Path fileName,
String prefix) |
static Optional<Path> |
stripPrefix(Path p,
Path prefix) |
static java.util.function.Function<String,Path> |
toPathFn(FileSystem fileSystem) |
public static Path getRelativePath(Path path, @Nullable Path baseDir)
path
- must reference a file, not a directory.baseDir
- must reference a directory that is relative to a common directory with the path.
may be null if referencing the same directory as the path.public static Path relativize(Path path1, Path path2)
This method is a workaround for JDK-6925169 (Path.relativize returns incorrect result if path contains "." or "..").
public static RelPath relativize(PathWrapper path1, PathWrapper path2)
public static Path relativizeWithDotDotSupport(Path basePath, Path childPath)
basePath
- the path against which childPath will be relativizedchildPath
- the path to relativize against basePath
childPath
relativized against basePath
public static Path normalize(Path path)
This method is a workaround for JDK-8037945 (Paths.get("").normalize() throws ArrayIndexOutOfBoundsException).
public static AbsPath normalize(AbsPath path)
normalize(Path)
.public static Path emptyOf(Path path)
public static boolean isEmpty(Path path)
public static com.google.common.collect.ImmutableSet<Path> filterForSubpaths(Iterable<Path> paths, Path root)
Path
objects from paths
that aren't a subpath of root
and
returns a set of paths relative to root
.public static Path expandHomeDir(Path path)
public static com.google.common.io.ByteSource asByteSource(Path path)
public static String stripPathPrefixAndExtension(Path fileName, String prefix)
public static java.util.function.Function<String,Path> toPathFn(FileSystem fileSystem)
public static Path fixPath(Path p)
Note that while Path objects provide a Path.normalize()
method for eliminating
redundant parts of paths like in foo/a/../b/c
, changing its internal parts (and
actually using the filesystem), we don't use those methods to clean up the incoming paths; we
only strip empty parts, and those consisting only of .
because doing so maps
exactly-same paths together, and can't influence where it may point to, whereas ..
and
symbolic links might.
public static Path dropInternalCaches(Path p)
Path's implementation class UnixPath
, will lazily initialize a String representation
and store it in the object when #toString()
is called for the first time. This doubles
the memory requirement for the Path object.
This hack constructs a new path, dropping the cached toString value.
Due to the nature of what this function does, it's very sensitive to the implementation. Any
calls to #toString()
on the returned object would also recreate the cached string
value.
public static Optional<Pair<Path,com.google.common.collect.ImmutableList<Path>>> splitOnCommonPrefix(Iterable<Path> paths)
public static void createSymLink(@Nullable WindowsFS winFS, Path symLink, Path target) throws IOException
winFS
- WindowsFS object that creates symlink on Windows using different permission level
implementations.symLink
- the symlink to create.target
- the target of the symlink.IOException
public static boolean isDirectory(Path path, LinkOption... linkOptions)
path
- An absolute file namelinkOptions
- Link optionspublic static String getWindowsLongPathString(Path path)
path
- The path to transform to a long path appropriate string form