
br.com.objectos.io.DirectoryJava6 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.multirelease.Concrete;
import java.io.File;
import java.io.IOException;
import java.net.URI;
@Concrete.Bridge
abstract class DirectoryJava6 extends AbstractDirectory {
private final File delegate;
private URI uri;
@Concrete.Constructor
DirectoryJava6(File delegate) {
this.delegate = delegate;
}
@Override
public final RegularFile createRegularFile(
String fileName, RegularFileCreateOption... options)
throws IOException {
checkNotNull(options, "options == null");
File file;
file = getFilePath(fileName);
checkRegularFileDoesNotExist(file);
boolean created;
created = file.createNewFile();
if (!created) {
throw new IOException("Failed to create new file " + file);
}
return newRegularFile(file, options);
}
@Override
public final void delete() throws IOException {
if (!delegate.delete()) {
throw new IOException("delete operation failed");
}
}
@Override
public final void deleteContents() throws IOException {
File[] files;
files = delegate.listFiles();
if (files == null) {
return;
}
for (File file : files) {
deleteContents0(file);
file.delete();
}
}
@Override
public final boolean exists() {
return delegate.exists();
}
@Override
public final String getName() {
return delegate.getName();
}
@Override
public final Directory getOrCreateDirectory(String directoryName) throws IOException {
File candidate;
candidate = getDirectoryPath(directoryName);
if (!candidate.exists() && !candidate.mkdirs()) {
throw new IOException("mkdirs operation failed");
}
if (!candidate.isDirectory()) {
String message;
message = candidate.toString();
throw new RegularFileExistsException(message);
}
return new Directory(candidate);
}
@Override
public final FsObject refresh() {
return QueryBuilderJava6.refresh(delegate);
}
@Override
public final FsObject resolve(String pathName) {
checkNotNull(pathName, "pathName == null");
return resolve0(pathName);
}
@Override
public final FsObject resolve(String firstName, String... rest) {
String pathName;
pathName = checkAndJoinNames(firstName, rest);
return resolve0(pathName);
}
@Override
public final URI resolveUri(String path) {
URI uri;
uri = getUri();
URI resolved;
resolved = uri.resolve(path);
return ComunsIoJava6.toUri(resolved);
}
@Override
public final File toFile() {
return delegate;
}
@Override
public final URI toUri() {
return getUri();
}
@Override
public final void visitContents(DirectoryContentsVisitor visitor) throws IOException {
checkNotNull(visitor, "visitor == null");
File[] files;
files = delegate.listFiles();
if (files == null) {
return;
}
for (File f : files) {
if (f.isDirectory()) {
Directory directory;
directory = new Directory(f);
visitor.visitDirectory(directory);
}
else if (f.isFile()) {
RegularFile file;
file = new RegularFile(f);
visitor.visitRegularFile(file);
}
}
}
@Override
final boolean contains(FileName fileName) {
File file;
file = getDelegate(fileName);
File parentFile;
parentFile = file.getParentFile();
while (parentFile != null) {
if (delegate.equals(parentFile)) {
return true;
}
parentFile = parentFile.getParentFile();
}
return false;
}
@Override
final Directory createDirectoryImpl(Object delegate) throws IOException {
File file;
file = toFile(delegate);
if (!file.mkdirs()) {
throw new IOException("Could not create new directory");
}
return new Directory(file);
}
@Override
final void createFile(Object o) throws IOException {
File file;
file = toFile(o);
if (!file.createNewFile()) {
throw new IOException("Could not create new file");
}
}
@Override
final Directory createTempDirImpl() throws IOException {
File file;
file = File.createTempFile("objectos-comuns-io-", "", delegate);
if (file.delete() && file.mkdir()) {
return new Directory(file);
} else {
throw new IOException("Could not create tempdir");
}
}
@Override
final RegularFile createTempFileImpl() throws IOException {
File file;
file = File.createTempFile("objectos-comuns-io-", ".tmp", delegate);
return new RegularFile(file);
}
@Override
final boolean equalsImpl(Object thisDelegate, Object thatDelegate) {
File thisFile;
thisFile = (File) thisDelegate;
File thatFile;
thatFile = (File) thatDelegate;
URI thisUri;
thisUri = thisFile.toURI().normalize();
URI thatUri;
thatUri = thatFile.toURI().normalize();
return thisUri.equals(thatUri);
}
@Override
final boolean exists(Object o) {
File file;
file = toFile(o);
return file.exists();
}
@Override
final File getDelegate() {
return delegate;
}
@Override
final File getDirectoryPath(String directoryName) {
checkNotNull(directoryName, "directoryName == null");
checkArgument(!directoryName.equals(""), "directoryName is empty");
File path;
path = new File(directoryName);
checkArgument(!path.isAbsolute(), "directoryName is absolute");
return new File(delegate, directoryName);
}
@Override
final File getDirectoryPath(String firstName, String... rest) {
String directoryName;
directoryName = checkAndJoinNames(firstName, rest);
return getDirectoryPath(directoryName);
}
@Override
final File getFilePath(String fileName) {
checkNotNull(fileName, "fileName == null");
checkArgument(!fileName.equals(""), "fileName is empty");
File path;
path = new File(fileName);
checkArgument(!path.isAbsolute(), "fileName is absolute");
File candidate;
candidate = new File(delegate, fileName);
File parentFile;
parentFile = candidate.getParentFile();
checkArgument(delegate.equals(parentFile), "fileName is in a subdirectory");
return candidate;
}
@Override
final Directory getParent0() throws DirectoryNotFoundException {
File parentFile;
parentFile = delegate.getParentFile();
if (parentFile == null) {
throw new DirectoryNotFoundException("No parent directory for " + delegate);
}
return newDirectory(parentFile);
}
@Override
final ImmutableList getRelativeParts(FileName fileName) {
File that;
that = getDelegate(fileName);
URI normalizedThat;
normalizedThat = that.toURI().normalize();
URI normalizedThis;
normalizedThis = delegate.toURI().normalize();
URI parts;
parts = normalizedThis.relativize(normalizedThat);
String partsString;
partsString = parts.toString();
GrowableList list;
list = newGrowableList();
StringBuilder part;
part = new StringBuilder();
for (char c : partsString.toCharArray()) {
switch (c) {
default:
part.append(c);
break;
case '/':
list.add(part.toString());
part.setLength(0);
break;
}
}
if (part.length() > 0) {
list.add(part.toString());
}
return list.toImmutableList();
}
@Override
final boolean isDirectory(Object o) {
File file;
file = toFile(o);
return file.isDirectory();
}
@Override
final boolean isRegularFile(Object o) {
File file;
file = toFile(o);
return file.isFile();
}
@Override
final Directory newDirectory(Object o) {
File file;
file = toFile(o);
return new Directory(file);
}
@Override
final NotFound newNotFound(Object o) {
File file;
file = toFile(o);
return new NotFound(file);
}
@Override
final RegularFile newRegularFile(Object o) {
File file;
file = toFile(o);
return new RegularFile(file);
}
private String checkAndJoinNames(String firstName, String... rest) {
checkNotNull(firstName, "firstName == null");
StringBuilder b;
b = new StringBuilder(firstName);
for (int i = 0; i < rest.length; i++) {
String part;
part = rest[i];
if (part == null) {
throw new NullPointerException("rest[" + i + "] == null");
}
b.append(IoFsJava6.FILE_SEPARATOR);
b.append(part);
}
String pathName;
pathName = b.toString();
return pathName;
}
private void checkRegularFileDoesNotExist(File file) throws RegularFileExistsException {
if (file.exists()) {
String message;
message = file.toString();
throw new RegularFileExistsException(message);
}
}
private void deleteContents0(File file) {
File[] files;
files = file.listFiles();
if (files == null) {
return;
}
for (File f : files) {
if (f.isDirectory()) {
deleteContents0(f);
}
f.delete();
}
}
private File getDelegate(FileName fileName) {
FsObject query;
query = (FsObject) fileName;
Object file;
file = query.getDelegate();
return (File) file;
}
private URI getUri() {
if (uri == null) {
URI source;
source = delegate.toURI();
uri = ComunsIoJava6.toUri(source);
}
return uri;
}
private RegularFile newRegularFile(File file, RegularFileCreateOption[] options) {
for (RegularFileCreateOption option : options) {
option.createRegularFileSetValue(file);
}
return new RegularFile(file);
}
private FsObject resolve0(String pathName) {
File path;
path = new File(pathName);
checkArgument(!path.isAbsolute(), "pathName is absolute");
File candidate;
candidate = new File(delegate, pathName);
String parentPath;
parentPath = delegate.getPath();
String candidatePath;
candidatePath = candidate.getPath();
boolean isDescendant;
isDescendant = candidatePath.startsWith(parentPath + IoFsJava6.FILE_SEPARATOR);
checkArgument(isDescendant, "pathName is not a descendant");
return QueryBuilderJava6.refresh(candidate);
}
private File toFile(Object o) {
return (File) o;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy