Efficient Java Solution with high readability, n-ary tree


  • 0
    M

    This problem gives us a chance to use the concept of trees in implementing a simple file system. Having a Map of childFiles increases the efficiency while parsing the path.

    class File
    {
        private boolean isDir;
        Map<String, File> childFiles;
        private String name;
        private String content;
        
        public File(String name, boolean isDir)
        {
            this.name = name;
            this.isDir = isDir;
            this.childFiles = new HashMap<>();
            this.content = "";
        }
        
        public String getName()
        {
            return this.name;
        }
        
        public boolean isDirectory()
        {
            return this.isDir;
        }
        
        public void addContent(String content)
        {
            this.content += content;
        }
        
        public String getContent()
        {
            return this.content;
        }
        
    }
    
    public class FileSystem {
        
        private File root;
    
        public FileSystem() {
            root = new File("/", true);
        }
        
        public List<String> ls(String path) {
            
            List<String> result;
            File curNode = getReferenceToResource(path);
            
            if( curNode.isDirectory() )
            {
                result = new ArrayList<>(curNode.childFiles.keySet());
                Collections.sort(result);
            }
            else
            {
                result = Arrays.asList(curNode.getName());
            }
            return result;
    
        }
        
        public void mkdir(String path) {
            
            if( path.equals("/") )
                return;
            String[] files = path.split("/");
            File curNode = this.root;
            
            for( int index = 1; index < files.length; index++ )
            {
                String curFileName = files[index];
                
                if( !curNode.childFiles.containsKey(curFileName) )
                {
                    File newFile = new File(curFileName, true);
                    curNode.childFiles.put(curFileName, newFile);
                }
                curNode = curNode.childFiles.get(curFileName);
            }
                    
        }
        
        public void addContentToFile(String filePath, String content) {
            
            File parentFile, childFile;
            int parentEndIndex = filePath.lastIndexOf("/");
            String childFileName = filePath.substring(parentEndIndex + 1);
            if( parentEndIndex == 0 )
            {
                parentFile = this.root;
            }
            else
            {
                String parent = filePath.substring(0, parentEndIndex);
                parentFile = getReferenceToResource(parent);
            }
            if( !parentFile.childFiles.containsKey(childFileName) )
            {
                childFile = new File(childFileName, false);
                parentFile.childFiles.put(childFileName, childFile);
            }
            else
            {
                childFile = parentFile.childFiles.get(childFileName);
            }
            childFile.addContent(content);
            return;
        }
        
        public String readContentFromFile(String filePath) {
            File file = getReferenceToResource(filePath);
            return file.getContent();
        }
        
        /**
         * This method helps us to reduce the code duplication and is useful for getting the reference to the resource in the path.
         */
        private File getReferenceToResource(String path)
        {
            File curNode = this.root;
            String[] files = path.split("/");
            for( int index = 1; index < files.length; index++ )
            {
                String curFile = files[index];
                curNode = curNode.childFiles.get(curFile);
            }
            return curNode;
        }
    }
    
    /**
     * Your FileSystem object will be instantiated and called as such:
     * FileSystem obj = new FileSystem();
     * List<String> param_1 = obj.ls(path);
     * obj.mkdir(path);
     * obj.addContentToFile(filePath,content);
     * String param_4 = obj.readContentFromFile(filePath);
     */
    

Log in to reply
 

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