Untitled
unknown
java
8 months ago
5.1 kB
12
Indexable
// Parent Node retrival
public class ParentNodeRetrival implements NodeRetrival {
@Override
public Node getNodeFromPath(String path, Directory root) throws Exception{
String [] parsePath = PathParsingUtil.parsePath(path);
int childIdx = 0;
Directory currentRoot = root;
String targetParentName = PathParsingUtil.getParentName(path);
if("NA".equals(targetParentName)){
return root;
}
while(!targetParentName.equals(currentRoot.getName())){
String nextNodeName = parsePath[childIdx];
childIdx++;
Node nextNode = currentRoot.getChild(nextNodeName);
if(!(nextNode instanceof Directory)){
throw new Exception("Invalid Path!!");
}
currentRoot = (Directory) nextNode;
}
return currentRoot;
}
}
// child node retirval
public class ChildRetrivalStrategy implements NodeRetrival {
ParentNodeRetrival s = new ParentNodeRetrival();
@Override
public Node getNodeFromPath(String path, Directory root) throws Exception {
String childNode = PathParsingUtil.getChildName(path);
Directory d = (Directory) s.getNodeFromPath(path, root);
return d.getChild(childNode);
}
}
// Directory Class
public class Directory extends Node {
private HashMap<String, Node> children;
public Directory(String name){
super(name, NodeType.DIRECTORY);
this.children = new HashMap<>();
}
public void addChild(String name, NodeType type){
// can use factory here for dynamic;
Node newChild = switch (type){
case FILE -> new File(name);
case DIRECTORY -> new Directory(name);
};
this.children.putIfAbsent(newChild.getName(), newChild);
}
public Node getChild(String name){
return this.children.getOrDefault(name, null);
}
public void listChildren(){
System.out.println("Directory with name: "+ super.getName() + " contains ");
for(String child: children.keySet()){
System.out.print(child+ " ");
}
System.out.println();
}
}
// File class
public class File extends Node {
String content = "";
public File(String name){
super(name, NodeType.FILE);
}
public void writeContent(String content){
this.content = content;
}
public String readContent(){
return this.content;
}
}
// Node
public abstract class Node {
private String name;
private NodeType type;
public Node(String name, NodeType type){
this.name = name;
this.type = type;
}
public String getName(){
return this.name;
}
}
// Path parsing
public class PathParsingUtil {
public static String getChildName(String path){
String [] parsedPath = parsePath(path);
if(parsedPath.length == 0){
return "NA";
}
return getStringFromIndex(parsedPath, parsedPath.length-1);
}
public static String getParentName(String path){
String [] parsedPath = parsePath(path);
if(parsedPath.length < 2){
return "NA";
}
return getStringFromIndex(parsedPath, parsedPath.length-2);
}
private static String getStringFromIndex(String [] path, int idx){
return path[idx];
}
public static String[] parsePath(String path){
return Arrays.stream(path.split("/")).filter(p -> !p.isEmpty()).toArray(String[]::new);
}
}
// File System main
public class FileSystem {
private Directory root;
private NodeRetrival strategy;
FileSystem(){
this.root = new Directory("/");
}
// supports mkdir, touch, ls, readFile and writeFile
public void mkdir(String path) throws Exception {
// get root node
Directory parent = (Directory) new ParentNodeRetrival().getNodeFromPath(path, root);
// get child node to be added
String childNode = PathParsingUtil.getChildName(path);
// add child node
parent.addChild(childNode, NodeType.DIRECTORY);
}
public void touch(String path) throws Exception {
// get root node
Directory parent = (Directory) new ParentNodeRetrival().getNodeFromPath(path, root);
// get child node to be added
String childNode = PathParsingUtil.getChildName(path);
// add child node
parent.addChild(childNode, NodeType.FILE);
}
public void ls(String path) throws Exception {
// get child node
Directory dir = (Directory) new ParentNodeRetrival().getNodeFromPath(path, root);
String childNode = PathParsingUtil.getChildName(path);
if(childNode == "NA"){
root.listChildren();
return;
}
Node nodeToList = dir.getChild(childNode);
if(!(nodeToList instanceof Directory)){
throw new Exception("Blah blah");
}
dir = (Directory) nodeToList;
dir.listChildren();
}
}
Editor is loading...
Leave a Comment