java.nio.file.Pathインタフェースは様々なファイルシステム上でのパスを扱うためのインタフェースである。インタフェースもstaticメソッド,および,デフォルトメソッドを持つことができるようになり,このインタフェースにもいくつかの実装コードが含まれる。
of(String, String …)メソッドは,パス文字列からPathオブジェクトを作成して返す。内部ではデフォルトのファイルシステムを取得した上で,getPath()メソッドを呼び出している。
of(URI)メソッドはURIからパスオブジェクトを構築する。URIオブジェクトからスキーム文字列を取得する。スキームが”file”であれば,デフォルトのファイルシステムから FileSystemProvider を取得して,そのgetPath()メソッドでパスオブジェクトを取得する。
それ以外のスキームの場合は,ファイルシステムにインストールされているFileSystemProviderをループで回しながら,スキームが一致するものを探す。一致するFileSystemProviderがみつかれば,そのgetPath()メソッドを実行してパスを取得する。見つからなければ, FileSystemNotFoundExceptionをスローする。
デフォルトメソッドの iterator()は,無名クラスを構築して返すようになっている。このインタフェースの実装クラスからPath型のパス名の数を取得して,その数だけ反復させるイテレータを構築して返している。
デフォルトメソッドのtoFile()はPath型からjava.io.File型へ変換する。システムのデフォルトファイルシステムが使用されていた場合には,Fileクラスのコンストラクタにこのインタフェースを実装したクラスのtoString()の戻り値を渡して,Fileクラスのインスタンスを作成している。デフォルトファイルシステム以外が使用されていた場合は, UnsupportedOperationExceptionをスローする実装となっている。
* 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
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;
public interface Path
extends Comparable<Path>, Iterable<Path>, Watchable
{
public static Path of(String first, String… more) {
return FileSystems.getDefault().getPath(first, more);
}
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");
}
FileSystem getFileSystem();
boolean isAbsolute();
Path getRoot();
Path getFileName();
Path getParent();
int getNameCount();
Path getName(int index);
Path subpath(int beginIndex, int endIndex);
boolean startsWith(Path other);
default boolean startsWith(String other) {
return startsWith(getFileSystem().getPath(other));
}
boolean endsWith(Path other);
default boolean endsWith(String other) {
return endsWith(getFileSystem().getPath(other));
}
Path normalize();
// — resolution and relativization —
Path resolve(Path other);
default Path resolve(String other) {
return resolve(getFileSystem().getPath(other));
}
default Path resolveSibling(Path other) {
if (other == null)
throw new NullPointerException();
Path parent = getParent();
return (parent == null) ? other : parent.resolve(other);
}
default Path resolveSibling(String other) {
return resolveSibling(getFileSystem().getPath(other));
}
Path relativize(Path other);
URI toUri();
Path toAbsolutePath();
Path toRealPath(LinkOption… options) throws IOException;
default File toFile() {
if (getFileSystem() == FileSystems.getDefault()) {
return new File(toString());
} else {
throw new UnsupportedOperationException("Path not associated with "
+ "default file system.");
}
}
// — watchable —
@Override
WatchKey register(WatchService watcher,
WatchEvent.Kind<?>[] events,
WatchEvent.Modifier… modifiers)
throws IOException;
@Override
default WatchKey register(WatchService watcher,
WatchEvent.Kind<?>… events) throws IOException {
return register(watcher, events, new WatchEvent.Modifier[0]);
}
// — Iterable —
@Override
default Iterator<Path> iterator() {
return new Iterator<>() {
private int i = 0;
@Override
public boolean hasNext() {
return (i < getNameCount());
}
@Override
public Path next() {
if (i < getNameCount()) {
Path result = getName(i);
i++;
return result;
} else {
throw new NoSuchElementException();
}
}
};
}
// — compareTo/equals/hashCode —
@Override
int compareTo(Path other);
boolean equals(Object other);
int hashCode();
String toString();
}