-
Notifications
You must be signed in to change notification settings - Fork 11
/
FileSystem
72 lines (62 loc) · 1.82 KB
/
FileSystem
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
import java.util.*;
class MyRunnableThread implements Runnable{
public String value ;
public MyRunnableThread(String value){
this.value=value;
}
public void run() {
System.out.println(value);
}
}
public class FileSystem {
Map<String,Integer> fileMap;
Map<String, Runnable> callBackMap;
public FileSystem() {
this.fileMap = new HashMap<>();
this.callBackMap = new HashMap<>();
this.fileMap.put("", 0);
}
public boolean create(String filePath,int value) {
if(fileMap.containsKey(filePath)) return false;
int lastSlashIndex = filePath.lastIndexOf("/");
if(!fileMap.containsKey(filePath.substring(0, lastSlashIndex))) return false;
fileMap.put(filePath, value);
return true;
}
public int get(String filePath) {
if(fileMap.containsKey(filePath)) return fileMap.get(filePath);
return -1;
}
public boolean set(String filePath, int value) {
if(!fileMap.containsKey(filePath)) return false;
fileMap.put(filePath, value);
String curPath = filePath;
while(curPath.length()>0) {
if(callBackMap.containsKey(curPath)) {
callBackMap.get(curPath).run();
}
int lastSlashIndex = filePath.lastIndexOf("/");
curPath = filePath.substring(0, lastSlashIndex);
}
return true;
}
public boolean watch(String filePath,Runnable callback) {
if(!fileMap.containsKey(filePath)) return false;
callBackMap.put(filePath, callback);
return true;
}
public static void main(String []args) {
FileSystem fs = new FileSystem();
fs.create("/a", 1);
System.out.println(fs.get("/a"));
fs.create("/a/b", 2);
System.out.println(fs.get("/a/b"));
fs.create("/c/d", 1);
System.out.println(fs.get("/c"));
fs.watch("/a",new MyRunnableThread("yes"));
fs.watch("/a/b",new MyRunnableThread("no"));
fs.set("/a", 1);
fs.set("/a/b", 2);
fs.create("/a/b/c", 1);
}
}