|
29 | 29 | */ |
30 | 30 | public class FileSystemPath { |
31 | 31 |
|
32 | | - @Getter |
33 | | - private String path; |
34 | | - private Deque<String> deque = new ArrayDeque<>(); |
| 32 | + @Getter |
| 33 | + private String path; |
| 34 | + private Deque<String> deque = new ArrayDeque<>(); |
35 | 35 |
|
36 | | - public FileSystemPath(String path) { |
37 | | - this.path = path; |
| 36 | + public FileSystemPath(String path) { |
| 37 | + this.path = path; |
38 | 38 |
|
39 | | - String[] directories = path.split("/"); |
40 | | - for (String directory : directories) { |
41 | | - deque.addLast(directory); |
| 39 | + String[] directories = path.split("/"); |
| 40 | + for (String directory : directories) { |
| 41 | + deque.addLast(directory); |
| 42 | + } |
42 | 43 | } |
43 | | - } |
44 | 44 |
|
45 | | - public void cd(String newPath) { |
46 | | - path = process(newPath); |
47 | | - } |
48 | | - |
49 | | - private String process(String newPath) { |
50 | | - if (newPath.startsWith("/")) { |
51 | | - return newPath; |
| 45 | + public void cd(String newPath) { |
| 46 | + path = process(newPath); |
52 | 47 | } |
53 | 48 |
|
54 | | - if (newPath.startsWith("./")) { |
55 | | - return path + newPath.substring(1); |
56 | | - } |
| 49 | + private String process(String newPath) { |
| 50 | + if (newPath.startsWith("/")) { |
| 51 | + return newPath; |
| 52 | + } |
| 53 | + |
| 54 | + if (newPath.startsWith("./")) { |
| 55 | + return path + newPath.substring(1); |
| 56 | + } |
57 | 57 |
|
58 | | - String[] directories = newPath.split("/"); |
59 | | - for (String directory : directories) { |
60 | | - if (directory.equals("..")) { |
61 | | - deque.removeLast(); |
62 | | - } else { |
63 | | - deque.addLast(directory); |
64 | | - } |
| 58 | + String[] directories = newPath.split("/"); |
| 59 | + for (String directory : directories) { |
| 60 | + if (directory.equals("..")) { |
| 61 | + deque.removeLast(); |
| 62 | + } else { |
| 63 | + deque.addLast(directory); |
| 64 | + } |
| 65 | + } |
| 66 | + return String.join("/", deque); |
65 | 67 | } |
66 | | - return String.join("/", deque); |
67 | | - } |
68 | 68 | } |
0 commit comments