| 1 | package net.minecraft.util.filefix.virtualfilesystem;
|
| 2 |
|
| 3 | import java.util.Collection;
|
| 4 | import java.util.Collections;
|
| 5 | import java.util.HashMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Objects;
|
| 8 | import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSNoSuchFileException;
|
| 9 | import net.minecraft.util.filefix.virtualfilesystem.exception.CowFSNotDirectoryException;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | final class DirectoryNode extends Node {
|
| 13 | private final Map<String, Node> childNodes = new HashMap<>();
|
| 14 |
|
| 15 | public DirectoryNode(final CopyOnWriteFSPath path) {
|
| 16 | super(path);
|
| 17 | }
|
| 18 |
|
| 19 | public Collection<Node> children() {
|
| 20 | return Collections.unmodifiableCollection(this.childNodes.values());
|
| 21 | }
|
| 22 |
|
| 23 | public void addChild(final Node child) {
|
| 24 | String name = Objects.requireNonNull(child.name());
|
| 25 | this.childNodes.put(name, child);
|
| 26 | child.setParent(this);
|
| 27 | }
|
| 28 |
|
| 29 | void removeChild(final String name) {
|
| 30 | this.childNodes.remove(name);
|
| 31 | }
|
| 32 |
|
| 33 | public @Nullable Node getChild(final String name) {
|
| 34 | return this.childNodes.get(name);
|
| 35 | }
|
| 36 |
|
| 37 | public DirectoryNode directoryByPath(final CopyOnWriteFSPath path) throws CowFSNoSuchFileException, CowFSNotDirectoryException {
|
| 38 | if (this.byPath(path) instanceof DirectoryNode result) {
|
| 39 | return result;
|
| 40 | } else {
|
| 41 | throw new CowFSNotDirectoryException(path + " was a file, expected directory");
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | public FileNode fileByPath(final CopyOnWriteFSPath path) throws CowFSNoSuchFileException {
|
| 46 | if (this.byPathOrNull(path) instanceof FileNode result) {
|
| 47 | return result;
|
| 48 | } else {
|
| 49 | throw new CowFSNoSuchFileException(path.toString());
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | public Node byPath(final CopyOnWriteFSPath path) throws CowFSNoSuchFileException {
|
| 54 | Node node = this.byPathOrNull(path);
|
| 55 | if (node != null) {
|
| 56 | return node;
|
| 57 | } else {
|
| 58 | throw new CowFSNoSuchFileException(path.toString());
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public @Nullable Node byPathOrNull(final CopyOnWriteFSPath path) {
|
| 63 | int nameCount = path.getNameCount();
|
| 64 | DirectoryNode directory = this;
|
| 65 |
|
| 66 | for (int i = 0; i < nameCount; i++) {
|
| 67 | String name = path.getName(i).toString();
|
| 68 | if (!name.equals(".")) {
|
| 69 | if (name.equals("..")) {
|
| 70 | DirectoryNode parent = directory.parent;
|
| 71 | if (parent != null) {
|
| 72 | directory = parent;
|
| 73 | }
|
| 74 | } else {
|
| 75 | Node nextNode = directory.getChild(name);
|
| 76 | if (!(nextNode instanceof DirectoryNode nextDirectory)) {
|
| 77 | return i == nameCount - 1 ? nextNode : null;
|
| 78 | }
|
| 79 |
|
| 80 | directory = nextDirectory;
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | return directory;
|
| 86 | }
|
| 87 | }
|
| 88 |
|