-
Notifications
You must be signed in to change notification settings - Fork 511
Expand file tree
/
Copy pathDirectory.java
More file actions
44 lines (36 loc) · 1021 Bytes
/
Directory.java
File metadata and controls
44 lines (36 loc) · 1021 Bytes
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
package ThreadSafeInMemoryFileSystem;
import java.util.*;
class Directory extends FileNode {
private final Map<String, FileNode> children = new HashMap<>();
public Directory(String name, Directory parent) {
super(name, parent);
}
// Internal (caller must hold lock)
public FileNode getChildUnsafe(String name) {
return children.get(name);
}
public void addChild(FileNode node) {
lock.writeLock().lock();
try {
children.put(node.getName(), node);
} finally {
lock.writeLock().unlock();
}
}
public FileNode removeChild(String name) {
lock.writeLock().lock();
try {
return children.remove(name);
} finally {
lock.writeLock().unlock();
}
}
public List<String> list() {
lock.readLock().lock();
try {
return new ArrayList<>(children.keySet());
} finally {
lock.readLock().unlock();
}
}
}