-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign_In_Memory_File_System.java
More file actions
129 lines (111 loc) · 3.6 KB
/
Design_In_Memory_File_System.java
File metadata and controls
129 lines (111 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class FileSystem {
abstract class FileObj implements Comparable<FileObj> {
protected String name;
FileObj(String name) {
this.name = name;
}
@Override
public int compareTo(FileObj f2) {
return name.compareTo(f2.name);
}
abstract public List<String> ls();
}
class File extends FileObj {
private String content;
File(String name, String content) {
super(name);
this.content = content;
}
public String addContent(String content) {
this.content += content;
return this.content;
}
public List<String> ls() {
List<String> result = new ArrayList<>(1);
result.add(this.name);
return result;
}
}
class Dir extends FileObj {
TreeSet<FileObj> children;
HashMap<String, FileObj> childrenMap;
Dir(String name) {
super(name);
this.children = new TreeSet<>();
this.childrenMap = new HashMap<>();
}
public List<String> ls() {
List<String> result = new ArrayList<>();
for (FileObj o: children) {
result.add(o.name);
}
return result;
}
public void addChild(FileObj child) {
children.add(child);
childrenMap.put(child.name, child);
}
}
private Dir root;
public FileSystem() {
root = new Dir("");
}
public List<String> ls(String path) {
if (path.equals("/")) {
return root.ls();
}
String[] names = path.split("/");
Dir current = root;
for (int i = 1; i < names.length - 1; ++i) {
current = (Dir) current.childrenMap.get(names[i]);
}
return current.childrenMap.get(names[names.length - 1]).ls();
}
public void mkdir(String path) {
String[] names = path.split("/");
Dir current = root;
Dir next;
for (int i = 1; i < names.length; ++i) {
next = (Dir) current.childrenMap.get(names[i]);
if (next == null) {
next = new Dir(names[i]);
current.addChild(next);
}
current = next;
}
}
public void addContentToFile(String filePath, String content) {
String[] names = filePath.split("/");
Dir current = root;
for (int i = 1; i < names.length - 1; ++i) {
current = (Dir) current.childrenMap.get(names[i]);
}
String fileName = names[names.length - 1];
File file = (File) current.childrenMap.get(fileName);
if (file == null) {
file = new File(fileName, content);
current.addChild(file);
}
else {
file.addContent(content);
}
}
public String readContentFromFile(String filePath) {
String[] names = filePath.split("/");
Dir current = root;
for (int i = 1; i < names.length - 1; ++i) {
current = (Dir) current.childrenMap.get(names[i]);
}
String fileName = names[names.length - 1];
File file = (File) current.childrenMap.get(fileName);
return file.content;
}
}
/**
* 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);
*/