8194746: (fs) Add equivalents of Paths.get to Path interface

Copy Paths.get() methods to Path.get() methods and have former call latter

Reviewed-by: alanb, forax, chegar, psandoz
This commit is contained in:
Brian Burkhalter 2018-03-22 12:30:47 -07:00
parent 37f1b2b1e3
commit 9e3d8fd230
18 changed files with 174 additions and 119 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,7 @@ package java.nio.file;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.spi.FileSystemProvider;
import java.util.Iterator;
import java.util.NoSuchElementException;
@ -93,12 +94,124 @@ import java.util.NoSuchElementException;
* multiple concurrent threads.
*
* @since 1.7
* @see Paths
*/
public interface Path
extends Comparable<Path>, Iterable<Path>, Watchable
{
/**
* Returns a {@code Path} by converting a path string, or a sequence of
* strings that when joined form a path string. If {@code more} does not
* specify any elements then the value of the {@code first} parameter is
* the path string to convert. If {@code more} specifies one or more
* elements then each non-empty string, including {@code first}, is
* considered to be a sequence of name elements and is joined to form a
* path string. The details as to how the Strings are joined is provider
* specific but typically they will be joined using the
* {@link FileSystem#getSeparator name-separator} as the separator.
* For example, if the name separator is "{@code /}" and
* {@code getPath("/foo","bar","gus")} is invoked, then the path string
* {@code "/foo/bar/gus"} is converted to a {@code Path}. A {@code Path}
* representing an empty path is returned if {@code first} is the empty
* string and {@code more} does not contain any non-empty strings.
*
* <p> The {@code Path} is obtained by invoking the {@link FileSystem#getPath
* getPath} method of the {@link FileSystems#getDefault default} {@link
* FileSystem}.
*
* <p> Note that while this method is very convenient, using it will imply
* an assumed reference to the default {@code FileSystem} and limit the
* utility of the calling code. Hence it should not be used in library code
* intended for flexible reuse. A more flexible alternative is to use an
* existing {@code Path} instance as an anchor, such as:
* <pre>{@code
* Path dir = ...
* Path path = dir.resolve("file");
* }</pre>
*
* @param first
* the path string or initial part of the path string
* @param more
* additional strings to be joined to form the path string
*
* @return the resulting {@code Path}
*
* @throws InvalidPathException
* if the path string cannot be converted to a {@code Path}
*
* @see FileSystem#getPath
*
* @since 11
*/
public static Path of(String first, String... more) {
return FileSystems.getDefault().getPath(first, more);
}
/**
* Returns a {@code Path} by converting a URI.
*
* <p> This method iterates over the {@link FileSystemProvider#installedProviders()
* installed} providers to locate the provider that is identified by the
* URI {@link URI#getScheme scheme} of the given URI. URI schemes are
* compared without regard to case. If the provider is found then its {@link
* FileSystemProvider#getPath getPath} method is invoked to convert the
* URI.
*
* <p> In the case of the default provider, identified by the URI scheme
* "file", the given URI has a non-empty path component, and undefined query
* and fragment components. Whether the authority component may be present
* is platform specific. The returned {@code Path} is associated with the
* {@link FileSystems#getDefault default} file system.
*
* <p> The default provider provides a similar <em>round-trip</em> guarantee
* to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it
* is guaranteed that
* <blockquote>{@code
* Path.of(}<i>p</i>{@code .}{@link Path#toUri() toUri}{@code ()).equals(}
* <i>p</i>{@code .}{@link Path#toAbsolutePath() toAbsolutePath}{@code ())}
* </blockquote>
* so long as the original {@code Path}, the {@code URI}, and the new {@code
* Path} are all created in (possibly different invocations of) the same
* Java virtual machine. Whether other providers make any guarantees is
* provider specific and therefore unspecified.
*
* @param uri
* the URI to convert
*
* @return the resulting {@code Path}
*
* @throws IllegalArgumentException
* if preconditions on the {@code uri} parameter do not hold. The
* format of the URI is provider specific.
* @throws FileSystemNotFoundException
* The file system, identified by the URI, does not exist and
* cannot be created automatically, or the provider identified by
* the URI's scheme component is not installed
* @throws SecurityException
* if a security manager is installed and it denies an unspecified
* permission to access the file system
*
* @since 11
*/
public static Path of(URI uri) {
String scheme = uri.getScheme();
if (scheme == null)
throw new IllegalArgumentException("Missing scheme");
// check for default provider to avoid loading of installed providers
if (scheme.equalsIgnoreCase("file"))
return FileSystems.getDefault().provider().getPath(uri);
// try to find provider
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
if (provider.getScheme().equalsIgnoreCase(scheme)) {
return provider.getPath(uri);
}
}
throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed");
}
/**
* Returns the file system that created this object.
*
@ -527,7 +640,7 @@ public interface Path
* to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it
* is guaranteed that
* <blockquote>
* {@link Paths#get(URI) Paths.get}{@code (}<i>p</i>{@code .toUri()).equals(}<i>p</i>
* {@link Path#of(URI) Path.of}{@code (}<i>p</i>{@code .toUri()).equals(}<i>p</i>
* {@code .}{@link #toAbsolutePath() toAbsolutePath}{@code ())}
* </blockquote>
* so long as the original {@code Path}, the {@code URI}, and the new {@code