forked from LuigiCortese/java-certification-ocp8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
181 lines (137 loc) · 3.93 KB
/
App.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package net.devsedge.path.basics;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
*
* @author Luigi Cortese
*
*/
public class App {
static int i;
public static void main(String[] args) {
/*
* Normalize: removes redundant name elements
*/
Path p = Paths.get("a",".","b","..","b","c");
System.out.println(++i+") Normalizing "
+ "\n\t"+p
+ "\n\t----"
+ "\n\t"+p.normalize()+"\n");
/*
* Resolve returns
*
* - p2, if p2 is an absolute path
* - p1, if p2 is an empty path
* - p1+p2 otherwise
*/
Path p1 = Paths.get("a","b","c");
Path p2 = Paths.get("d:","e","f");
System.out.println(++i+") Resolving (p2 is an absolute path): "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.resolve(p2)+"\n");
p2 = Paths.get("");
System.out.println(++i+") Resolving (p2 is an empty path): "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.resolve(p2)+"\n");
p2 = Paths.get("d","e","f");
System.out.println(++i+") Resolving (joining paths): "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.resolve(p2)+"\n");
p2 = Paths.get("a","b","c");
System.out.println(++i+") Resolving (joining paths): "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.resolve(p2)+"\n");
p2 = Paths.get("..",".","c");
System.out.println(++i+") Resolving (joining paths): "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.resolve(p2)+"\n");
/*
* ResolveSibling returns
*
* - p2, if p2 is absolute or p1 does not have a parent path
* - p1's parent, if p2 is empty
* - p1+p2's parent otherwise
*/
p1 = Paths.get("a","b","c");
p2 = Paths.get("d:","e","f");
System.out.println(++i+") Resolving sibling (p2 is an absolute path): "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.resolveSibling(p2)+"\n");
p2 = Paths.get("");
System.out.println(++i+") Resolving sibling (p2 is an empty path): "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.resolveSibling(p2)+"\n");
p2 = Paths.get("d","e","f");
System.out.println(++i+") Resolving sibling (joining paths): "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.resolveSibling(p2)+"\n");
/*
* Relativize: returns a relative path between p1 and p2
*/
p1 = Paths.get("a","b","c");
p2 = Paths.get("d","e","f");
System.out.println(++i+") Relativizing: "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.relativize(p2)+"\n");
p2 = Paths.get("a","b","d");
System.out.println(++i+") Relativizing: "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.relativize(p2)+"\n");
p2 = Paths.get("a","b","c","d","e");
System.out.println(++i+") Relativizing: "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.relativize(p2)+"\n");
p2 = Paths.get("a","b","..");
System.out.println(++i+") Relativizing: "
+ "\n\t"+p1
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+p1.relativize(p2)+"\n");
p2 = Paths.get("a:","b","e");
System.out.println(++i+") Relativizing: "
+ "\n\t"+Paths.get("a:","b",".","c","..","d")
+ "\n\t"+p2
+ "\n\t----"
+ "\n\t"+Paths.get("a:","b",".","c","..","d").relativize(p2)+"\n");
/*
* Subpath
*/
p1 = Paths.get("a","b","c","d","e");
System.out.println(++i+") Subpath (0,2): "
+ "\n\t"+p1
+ "\n\t----"
+ "\n\t"+p1.subpath(0,2)+"\n");
p1 = Paths.get("a:","b","c","d","e");
System.out.println(++i+") Subpath (0,2): "
+ "\n\t"+p1
+ "\n\t----"
+ "\n\t"+p1.subpath(0,2)+"\n");
p1 = Paths.get("a:","b","..","b","c");
System.out.println(++i+") Subpath (0,3): "
+ "\n\t"+p1
+ "\n\t----"
+ "\n\t"+p1.subpath(0,3)+"\n");
}
}