-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectory.java
More file actions
39 lines (33 loc) · 978 Bytes
/
Copy pathDirectory.java
File metadata and controls
39 lines (33 loc) · 978 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
package structural.composite;
import java.util.*;
public class Directory implements FileSystemItem {
private String name;
private final List<FileSystemItem> children;
public Directory(String name) {
this.name = name;
children = new ArrayList<>();
}
public void add(FileSystemItem item) {
children.add(item);
Collections.sort(children);
}
@Override
public void display(String indent) {
if (Objects.isNull(indent)) {
indent = "";
}
Main.println(indent + "+ Directory: " + name);
for (FileSystemItem item : children) {
item.display(indent + " ");
}
}
@Override
public int compareTo(FileSystemItem fileSystemItem) {
if (fileSystemItem instanceof Directory) {
Directory other = (Directory) fileSystemItem;
return this.name.compareTo(other.name);
} else {
return -1;
}
}
}