
br.com.objectos.io.DirectoryJava7 Maven / Gradle / Ivy
/*
* Copyright (C) 2011-2021 Objectos Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package br.com.objectos.io;
import static br.com.objectos.collections.Collections.newGrowableList;
import static br.com.objectos.lang.Lang.checkArgument;
import static br.com.objectos.lang.Lang.checkNotNull;
import br.com.objectos.collections.GrowableList;
import br.com.objectos.collections.ImmutableList;
import br.com.objectos.lang.OperatingSystem.Family;
import br.com.objectos.multirelease.Concrete;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.util.Iterator;
@Concrete.Bridge
abstract class DirectoryJava7 extends AbstractDirectory {
private final Path delegate;
@Concrete.Constructor
DirectoryJava7(File file) {
throw new AssertionError("Should never be called");
}
@Concrete.Constructor
DirectoryJava7(Path delegate) {
this.delegate = delegate;
}
@Override
public final RegularFile createRegularFile(
String fileName, RegularFileCreateOption... options) throws IOException {
Path path;
path = createRegularFile0(fileName, options);
return new RegularFile(path);
}
@Override
public final void delete() throws IOException {
Files.delete(delegate);
}
@Override
public final void deleteContents() throws IOException {
deleteContents0(delegate);
}
@Override
public final boolean exists() {
return Files.isDirectory(delegate);
}
@Override
public final String getName() {
Path fileName;
fileName = delegate.getFileName();
return fileName.toString();
}
@Override
public final Directory getOrCreateDirectory(String directoryName) throws IOException {
try {
Path candidate;
candidate = getDirectoryPath(directoryName);
Files.createDirectories(candidate);
return new Directory(candidate);
} catch (FileAlreadyExistsException e) {
String message;
message = e.getMessage();
throw new RegularFileExistsException(message);
}
}
@Override
public final FsObject refresh() {
return QueryBuilderJava7.refresh(getDelegate());
}
@Override
public final FsObject resolve(String fileName) {
checkNotNull(fileName, "fileName == null");
Path path;
path = Paths.get(fileName);
return resolve0(path);
}
@Override
public final FsObject resolve(String firstName, String... rest) {
Path path;
path = checkAndGetPath(firstName, rest);
return resolve0(path);
}
@Override
public final URI resolveUri(String path) {
Path resolved;
resolved = delegate.resolve(path);
return resolved.toUri();
}
@Override
public final File toFile() {
return delegate.toFile();
}
@Override
public final URI toUri() {
return delegate.toUri();
}
@Override
public final void visitContents(DirectoryContentsVisitor visitor) throws IOException {
checkNotNull(visitor, "visitor == null");
try (DirectoryStream entries = Files.newDirectoryStream(delegate)) {
for (Path entry : entries) {
if (Files.isDirectory(entry)) {
Directory directory;
directory = new Directory(entry);
visitor.visitDirectory(directory);
}
else if (Files.isRegularFile(entry)) {
RegularFile file;
file = new RegularFile(entry);
visitor.visitRegularFile(file);
}
}
}
}
@Override
final boolean contains(FileName fileName) {
Path filePath;
filePath = getDelegate(fileName);
Path normalizedFilePath;
normalizedFilePath = filePath.normalize();
Path normalizedThisPath;
normalizedThisPath = delegate.normalize();
return normalizedFilePath.startsWith(normalizedThisPath);
}
@Override
final Directory createDirectoryImpl(Object delegate) throws IOException {
Path path;
path = toPath(delegate);
Files.createDirectories(path);
return new Directory(path);
}
@Override
final void createFile(Object o) throws IOException {
Path path;
path = toPath(o);
Files.createFile(path);
}
@Override
final Directory createTempDirImpl() throws IOException {
Path path;
path = Files.createTempDirectory(delegate, "objectos-comuns-io-");
return new Directory(path);
}
@Override
final RegularFile createTempFileImpl() throws IOException {
Path path;
path = Files.createTempFile(delegate, "objectos-comuns-io-", ".tmp");
return new RegularFile(path);
}
@Override
final boolean equalsImpl(Object thisDelegate, Object thatDelegate) {
Path thisPath;
thisPath = toPath(thisDelegate);
Path thatPath;
thatPath = toPath(thatDelegate);
Path thisNormalized;
thisNormalized = thisPath.normalize();
Path thatNormalized;
thatNormalized = thatPath.normalize();
return thisNormalized.equals(thatNormalized);
}
@Override
final boolean exists(Object o) {
Path path;
path = toPath(o);
return Files.exists(path);
}
@Override
final Path getDelegate() {
return delegate;
}
@Override
final Path getDirectoryPath(String directoryName) {
checkNotNull(directoryName, "directoryName == null");
checkArgument(!directoryName.equals(""), "directoryName is empty");
Path path;
path = Paths.get(directoryName);
checkArgument(!path.isAbsolute(), "directoryName is absolute");
return delegate.resolve(path);
}
@Override
final Path getDirectoryPath(String firstName, String... rest) {
Path path;
path = checkAndGetPath(firstName, rest);
checkArgument(!path.isAbsolute(), "directoryName is absolute");
return delegate.resolve(path);
}
@Override
final Path getFilePath(String fileName) {
checkNotNull(fileName, "fileName == null");
checkArgument(!fileName.equals(""), "fileName is empty");
Path path;
path = Paths.get(fileName);
checkArgument(path.getNameCount() == 1, "fileName is in a subdirectory");
checkArgument(!path.isAbsolute(), "fileName is absolute");
return delegate.resolve(path);
}
@Override
final Directory getParent0() throws IOException {
Path parent;
parent = delegate.getParent();
if (parent == null) {
throw new DirectoryNotFoundException("No parent directory for " + delegate);
}
return newDirectory(parent);
}
@Override
final ImmutableList getRelativeParts(FileName file) {
Path filePath;
filePath = getDelegate(file);
Path normalizedFilePath;
normalizedFilePath = filePath.normalize();
Path normalizedThisPath;
normalizedThisPath = delegate.normalize();
Path parts;
parts = normalizedThisPath.relativize(normalizedFilePath);
GrowableList stream;
stream = newGrowableList();
for (Path part : parts) {
String s;
s = part.toString();
stream.add(s);
}
return stream.toImmutableList();
}
@Override
final boolean isDirectory(Object o) {
Path path;
path = toPath(o);
return Files.isDirectory(path);
}
@Override
final boolean isRegularFile(Object o) {
Path path;
path = toPath(o);
return Files.isRegularFile(path);
}
@Override
final Directory newDirectory(Object o) {
Path path;
path = toPath(o);
return new Directory(path);
}
@Override
final NotFound newNotFound(Object o) {
Path path;
path = toPath(o);
return new NotFound(path);
}
@Override
final RegularFile newRegularFile(Object o) {
Path path;
path = toPath(o);
return new RegularFile(path);
}
private Path checkAndGetPath(String firstName, String... rest) {
checkNotNull(firstName, "firstName == null");
Path path;
path = Paths.get(firstName, rest);
return path;
}
private void clear1(Path path) throws IOException {
if (Files.isDirectory(path)) {
deleteContents0(path);
}
Files.delete(path);
}
private Path createRegularFile0(
String fileName, RegularFileCreateOption[] options) throws IOException {
checkNotNull(options, "options == null");
Path path;
path = getFilePath(fileName);
FileAttribute>[] attributes;
switch (options.length) {
case 0:
attributes = IoImplJava7.EMPTY_FILE_ATTRIBUTES;
break;
case 1:
RegularFileCreateOption option;
option = options[0];
attributes = (FileAttribute>[]) option.createRegularFileGetValue();
break;
default:
Family osFamily;
osFamily = IoImplJava7._OS_FAMILY;
FileAttributeBuilder builder;
builder = FileAttributeBuilder.create(osFamily);
for (RegularFileCreateOption o : options) {
o.createRegularFileSetValue(builder);
}
attributes = builder.build();
break;
}
try {
Files.createFile(path, attributes);
} catch (FileAlreadyExistsException e) {
throw new RegularFileExistsException(e.getMessage(), e);
}
return path;
}
private void deleteContents0(Path path) throws IOException {
try (DirectoryStream stream = Files.newDirectoryStream(path)) {
Iterator iterator;
iterator = stream.iterator();
while (iterator.hasNext()) {
Path next;
next = iterator.next();
clear1(next);
}
}
}
private Path getDelegate(FileName fileName) {
FsObject query;
query = (FsObject) fileName;
Object path;
path = query.getDelegate();
return (Path) path;
}
private FsObject resolve0(Path path) {
checkArgument(!path.isAbsolute(), "pathName is absolute");
Path resolved;
resolved = delegate.resolve(path);
Path normalizedDelegate;
normalizedDelegate = delegate.normalize();
checkArgument(resolved.startsWith(normalizedDelegate), "pathName is not a descendant");
return QueryBuilderJava7.refresh(resolved);
}
private Path toPath(Object o) {
return (Path) o;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy