
br.com.objectos.io.AbstractDirectory 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.lang.Lang.checkArgument;
import static br.com.objectos.lang.Lang.checkNotNull;
import br.com.objectos.collections.ImmutableList;
import br.com.objectos.lang.Lang;
import br.com.objectos.lang.ShutdownHook;
import br.com.objectos.lang.ShutdownHookListener;
import br.com.objectos.multirelease.Concrete;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import javax.lang.model.SourceVersion;
@Concrete(modifiers = "public final", simpleName = "Directory")
abstract class AbstractDirectory extends Node implements ShutdownHookListener {
private Directory parent;
AbstractDirectory() {}
static Directory systemTempDirectory() {
return SystemTmpDirHolder.INSTANCE;
}
@Override
public final R acceptFsObjectVisitor(FsObjectVisitor visitor, P p)
throws IOException {
return visitor.visitDirectory(self(), p);
}
public final Directory changeTo(String pathName) throws IOException {
Object candidate;
candidate = getDirectoryPath(pathName);
if (!exists(candidate)) {
String message;
message = candidate.toString();
throw new DirectoryNotFoundException(message);
}
else if (!isDirectory(candidate)) {
String message;
message = candidate.toString();
throw new NotDirectoryException(message);
}
else {
return newDirectory(candidate);
}
}
@Override
public final void copyTo(final Directory dest) throws IOException {
visitContents(new DirectoryContentsVisitor() {
@Override
public final void visitDirectory(Directory directory) throws IOException {
String directoryName;
directoryName = directory.getName();
Directory subdir;
subdir = dest.getOrCreateDirectory(directoryName);
directory.copyTo(subdir);
}
@Override
public final void visitRegularFile(RegularFile file) throws IOException {
file.copyTo(dest);
}
});
}
public final Directory createDirectory(String directoryName) throws IOException {
Object delegate;
delegate = getDirectoryPath(directoryName);
return createDirectory0(delegate);
}
public final Directory createDirectory(String firstName, String... rest) throws IOException {
Object delegate;
delegate = getDirectoryPath(firstName, rest);
return createDirectory0(delegate);
}
public abstract RegularFile createRegularFile(
String fileName, RegularFileCreateOption... options) throws IOException;
public final Directory createTempDir() throws IOException {
Directory dir;
dir = createTempDirImpl();
ShutdownHook shutdownHook;
shutdownHook = Lang.getShutdownHook();
shutdownHook.addListener(dir);
return dir;
}
public final RegularFile createTempFile() throws IOException {
RegularFile f;
f = createTempFileImpl();
ShutdownHook shutdownHook;
shutdownHook = Lang.getShutdownHook();
shutdownHook.addListener(f);
return f;
}
public abstract void deleteContents() throws IOException;
@Override
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Directory)) {
return false;
}
Directory that = (Directory) obj;
return getClass().equals(that.getClass())
&& equalsImpl(getDelegate(), that.getDelegate());
}
@Override
public abstract boolean exists();
public final Directory getDirectory(String directoryName) throws IOException {
Object path;
path = getDirectoryPath(directoryName);
return getDirectory0(path);
}
public final Directory getDirectory(String firstName, String... rest) throws IOException {
Object path;
path = getDirectoryPath(firstName, rest);
return getDirectory0(path);
}
public final String getJavaBinaryName(FileName classFile) {
checkNotNull(classFile, "classFile == null");
String extension;
extension = classFile.getExtension();
checkArgument(extension.equals("class"), "classFile extension not .class");
checkArgument(contains(classFile), "classFile is not a child of this directory");
StringBuilder result;
result = new StringBuilder();
ImmutableList parts;
parts = getRelativeParts(classFile);
int partsCount;
partsCount = parts.size();
if (partsCount > 1) {
for (int i = 0; i < partsCount - 1; i++) {
String part;
part = parts.get(i);
result.append(part);
result.append('.');
}
}
String simpleName;
simpleName = classFile.getName();
outer: for (char c : simpleName.toCharArray()) {
switch (c) {
case '.':
break outer;
default:
result.append(c);
break;
}
}
return result.toString();
}
public final NotFound getNotFound(String fileName) throws FoundException {
checkNotNull(fileName, "fileName == null");
Object filePath;
filePath = getFilePath(fileName);
if (exists(filePath)) {
String message;
message = filePath.toString();
throw new FoundException(message);
}
return newNotFound(filePath);
}
public abstract Directory getOrCreateDirectory(String directoryName) throws IOException;
public final Directory getOrCreateJavaPackageDirectory(String packageName) throws IOException {
checkNotNull(packageName, "packageName == null");
Directory result;
result = self();
StringBuilder partBuilder;
partBuilder = new StringBuilder();
int[] codePoints;
codePoints = Lang.toCodePoints(packageName);
for (int i = 0; i < codePoints.length; i++) {
int codePoint;
codePoint = codePoints[i];
if (codePoint != '.') {
partBuilder.appendCodePoint(codePoint);
continue;
}
result = createJavaPackageDirectory0(result, partBuilder);
}
if (partBuilder.length() > 0) {
result = createJavaPackageDirectory0(result, partBuilder);
}
return result;
}
public final RegularFile getOrCreateRegularFile(String fileName) throws IOException {
Object filePath;
filePath = getFilePath(fileName);
if (!exists(filePath)) {
createFile(filePath);
return newRegularFile(filePath);
}
if (!isRegularFile(filePath)) {
String message;
message = filePath.toString();
throw new NotRegularFileException(message);
}
return newRegularFile(filePath);
}
public final Directory getParent() throws IOException {
if (parent == null) {
parent = getParent0();
}
return parent;
}
public final RegularFile getRegularFile(String fileName) throws IOException {
checkNotNull(fileName, "fileName == null");
Object filePath;
filePath = getFilePath(fileName);
if (!exists(filePath)) {
String message;
message = filePath.toString();
throw new RegularFileNotFoundException(message);
}
if (!isRegularFile(filePath)) {
String message;
message = filePath.toString();
throw new NotRegularFileException(message);
}
return newRegularFile(filePath);
}
@Override
public final boolean isDirectory() {
return true;
}
public final boolean isEmpty() {
File file;
file = toFile();
String[] list;
list = file.list();
return list.length == 0;
}
@Override
public final void onShutdownHook() throws Exception {
deleteContents();
delete();
}
public abstract FsObject resolve(String pathName);
public abstract FsObject resolve(String firstName, String... rest);
public abstract URI resolveUri(String path);
@Override
public final Directory toDirectory() {
return self();
}
public final RegularFile touchRegularFile(String fileName) throws IOException {
Object filePath;
filePath = getFilePath(fileName);
if (!exists(filePath)) {
createFile(filePath);
return newRegularFile(filePath);
}
else if (isRegularFile(filePath)) {
RegularFile result;
result = newRegularFile(filePath);
result.setLastModifiedTimeToNow();
return result;
}
else {
throw new NotRegularFileException(filePath.toString());
}
}
public abstract void visitContents(DirectoryContentsVisitor visitor) throws IOException;
abstract boolean contains(FileName fileName);
abstract Directory createDirectoryImpl(Object delegate) throws IOException;
abstract void createFile(Object o) throws IOException;
abstract Directory createTempDirImpl() throws IOException;
abstract RegularFile createTempFileImpl() throws IOException;
abstract boolean equalsImpl(Object thisDelegate, Object thatDelegate);
abstract boolean exists(Object o);
abstract Object getDirectoryPath(String directoryName);
abstract Object getDirectoryPath(String firstName, String... rest);
abstract Object getFilePath(String fileName);
abstract Directory getParent0() throws IOException;
abstract ImmutableList getRelativeParts(FileName fileName);
abstract boolean isDirectory(Object o);
abstract boolean isRegularFile(Object o);
abstract Directory newDirectory(Object o);
abstract NotFound newNotFound(Object o);
abstract RegularFile newRegularFile(Object o);
final Directory self() {
return Directory.class.cast(this);
}
private Directory createDirectory0(Object delegate) throws IOException {
if (isDirectory(delegate)) {
String message;
message = delegate.toString();
throw new DirectoryExistsException(message);
}
if (exists(delegate)) {
String message;
message = delegate.toString();
throw new NotDirectoryException(message);
}
return createDirectoryImpl(delegate);
}
private Directory createJavaPackageDirectory0(
Directory result, StringBuilder partBuilder) throws IOException {
String part;
part = partBuilder.toString();
partBuilder.setLength(0);
checkArgument(SourceVersion.isIdentifier(part), part + " is not a valid Java identifier");
return result.getOrCreateDirectory(part);
}
private Directory getDirectory0(Object path)
throws DirectoryNotFoundException, NotDirectoryException {
if (!exists(path)) {
String message;
message = path.toString();
throw new DirectoryNotFoundException(message);
}
if (!isDirectory(path)) {
String message;
message = path.toString();
throw new NotDirectoryException(message);
}
return newDirectory(path);
}
private static class SystemTmpDirHolder {
static final Directory INSTANCE;
static {
try {
String tmpdir;
tmpdir = Lang.getSystemProperty("java.io.tmpdir");
INSTANCE = Io.getDirectory(tmpdir);
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy