Files
Testate/WS2526/Testat 3a/InMemoryPath.java
T

226 lines
6.2 KiB
Java

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchEvent.Modifier;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Iterator;
public class InMemoryPath implements Path {
private final InMemoryFileSystem fileSystem;
private final String path;
private static final String SEPARATOR = "/";
public InMemoryPath(InMemoryFileSystem fileSystem, String path) {
this.fileSystem = fileSystem;
this.path = normalizePath(path);
}
private String normalizePath(String path) {
if (path == null || path.isEmpty()) {
return SEPARATOR;
}
if (!path.startsWith(SEPARATOR)) {
path = SEPARATOR + path;
}
return path;
}
@Override
public int compareTo(Path other) {
return this.path.compareTo(other.toString());
}
@Override
public boolean endsWith(Path other) {
return this.path.endsWith(other.toString());
}
@Override
public Path getFileName() {
int lastSeparator = path.lastIndexOf(SEPARATOR);
if (lastSeparator == -1 || lastSeparator == path.length() - 1) {
return this;
}
return new InMemoryPath(fileSystem, path.substring(lastSeparator + 1));
}
@Override
public FileSystem getFileSystem() {
return fileSystem;
}
@Override
public Path getName(int index) {
String[] parts = path.split(SEPARATOR);
if (index < 0 || index >= parts.length) {
throw new IllegalArgumentException("Index out of bounds");
}
return new InMemoryPath(fileSystem, parts[index]);
}
@Override
public int getNameCount() {
if (path.equals(SEPARATOR))
return 0;
return path.split(SEPARATOR).length - 1; // -1 because split on "/" creates empty first element
}
@Override
public Path getParent() {
int lastSeparator = path.lastIndexOf(SEPARATOR);
if (lastSeparator <= 0) {
return null;
}
return new InMemoryPath(fileSystem, path.substring(0, lastSeparator));
}
@Override
public Path getRoot() {
return new InMemoryPath(fileSystem, SEPARATOR);
}
@Override
public boolean isAbsolute() {
return path.startsWith(SEPARATOR);
}
@Override
public Path normalize() {
return this; // Already normalized in constructor
}
@Override
public WatchKey register(WatchService watcher, Kind<?>[] events, Modifier... modifiers) throws IOException {
throw new UnsupportedOperationException("Watch service not supported");
}
@Override
public Path relativize(Path other) {
// Simplified implementation
return new InMemoryPath(fileSystem, other.toString());
}
@Override
public Path resolve(Path other) {
if (other.isAbsolute()) {
return other;
}
String resolvedPath = path;
if (!path.endsWith("/")) {
resolvedPath += "/";
}
resolvedPath += other.toString();
return new InMemoryPath(fileSystem, resolvedPath);
}
@Override
public boolean startsWith(Path other) {
return this.path.startsWith(other.toString());
}
@Override
public Path subpath(int beginIndex, int endIndex) {
String[] parts = path.split("/");
if (beginIndex < 0 || endIndex > parts.length - 1 || beginIndex >= endIndex) {
throw new IllegalArgumentException("Invalid indices");
}
StringBuilder sb = new StringBuilder();
for (int i = beginIndex + 1; i <= endIndex; i++) { // +1 because parts[0] is empty
if (sb.length() != 0) {
sb.append("/");
}
sb.append(parts[i]);
}
return new InMemoryPath(fileSystem, sb.toString());
}
@Override
public Path toAbsolutePath() {
return this; // Already absolute
}
@Override
public Path toRealPath(LinkOption... options) throws IOException {
return this;
}
@Override
public URI toUri() {
return URI.create("memory://default" + path);
}
@Override
public String toString() {
return path;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof InMemoryPath))
return false;
InMemoryPath other = (InMemoryPath) obj;
return path.equals(other.path);
}
@Override
public int hashCode() {
return path.hashCode();
}
@Override
public boolean startsWith(String other) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'startsWith'");
}
@Override
public boolean endsWith(String other) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'endsWith'");
}
@Override
public Path resolve(String other) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'resolve'");
}
@Override
public Path resolveSibling(Path other) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'resolveSibling'");
}
@Override
public Path resolveSibling(String other) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'resolveSibling'");
}
@Override
public File toFile() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'toFile'");
}
@Override
public WatchKey register(WatchService watcher, Kind<?>... events) throws IOException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'register'");
}
@Override
public Iterator<Path> iterator() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'iterator'");
}
}