File system as a tree - Java


  • 1
    public class FileSystem {
        
        private class TreeNode implements Comparable<TreeNode> {
            String label;
            boolean isDir;
            Map<String, TreeNode> dirContents = new TreeMap<>();
            String fileContents;
            
            public int compareTo(TreeNode other) {
                return label.compareTo(other.label);
            }
            
            public int hashCode() {
                return Objects.hash(label);
            }
            
            public boolean equals(Object treeNode) {
                TreeNode other = (TreeNode) treeNode;
                return Objects.equals(label, other.label);
            }
        }    
        
        private TreeNode sentinel = new TreeNode();
    
        public FileSystem() {
            TreeNode root = new TreeNode();
            root.label = "";
            root.isDir = true;
            sentinel.dirContents.put("", root);
        }
        
        public List<String> ls(String path) {
            String[] labels = path.split("/");
            TreeNode curr = (labels.length != 0) ? sentinel : sentinel.dirContents.get("");
            List<String> result = new ArrayList<>();
            for(String label : labels) {
                curr = curr.dirContents.get(label);
                System.out.println(">>" + curr);
            }
            if(curr.isDir) {
                for(String label : curr.dirContents.keySet()) {
                    result.add(label);
                }
            }
            else {
                result.add(curr.label);
            }
            
            return result;
        }
        
        public void mkdir(String path) {
            String[] labels = path.split("/");
            TreeNode curr = sentinel;
            int currIndex = 0;
            for(String label : labels) {
                TreeNode next = curr.dirContents.get(label);
                if(next == null) {
                    break;
                }
                currIndex++;
                curr = next;
            }
            
            for(int i = currIndex; i < labels.length; i++) {
                TreeNode newDir = new TreeNode();
                newDir.isDir = true;
                newDir.label = labels[i];
                curr.dirContents.put(labels[i], newDir);
                
                curr = newDir;
            }
        }
        
        public void addContentToFile(String filePath, String content) {
            String[] labels = filePath.split("/");
            TreeNode curr = sentinel, prev = null;
            
            for(String label : labels) {
                prev = curr;
                if(curr == null) {
                    break;
                }
                curr = curr.dirContents.get(label);
            }
            
            if(curr == null) {
                curr = new TreeNode();
                curr.isDir = false;
                curr.label = labels[labels.length - 1];
                curr.fileContents = content;
                prev.dirContents.put(curr.label, curr);
            }
            else {
                curr.fileContents = curr.fileContents == null ? content : curr.fileContents + content;
            }
        }
        
        public String readContentFromFile(String filePath) {
            String[] labels = filePath.split("/");
            TreeNode curr = sentinel;
            
            for(String label : labels) {
                curr = curr.dirContents.get(label);
            }
            
            return (curr.fileContents == null) ? "" : curr.fileContents;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.