-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUnionPath.java
More file actions
347 lines (314 loc) · 12.1 KB
/
UnionPath.java
File metadata and controls
347 lines (314 loc) · 12.1 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright (c) Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package cpw.mods.niofs.union;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Objects;
import java.util.function.IntBinaryOperator;
import java.util.stream.IntStream;
public class UnionPath implements Path {
private final UnionFileSystem fileSystem;
private final boolean absolute;
private final boolean empty;
private final String[] pathParts;
// Store the normalized path after it has been created first
private UnionPath normalized;
UnionPath(final UnionFileSystem fileSystem, final String... pathParts) {
this.fileSystem = fileSystem;
if (pathParts.length == 0) {
this.absolute = false;
this.pathParts = new String[]{ "" };
this.empty = true;
} else {
StringBuilder joiner = new StringBuilder();
for (int i = 0; i < pathParts.length; i++) {
final String element = pathParts[i];
if (!element.isEmpty()) {
joiner.append(element);
if (i<pathParts.length-1) joiner.append(UnionFileSystem.SEP_STRING);
}
}
final var longstring = joiner.toString();
this.absolute = longstring.startsWith(UnionFileSystem.SEP_STRING);
this.pathParts = getPathParts(this.absolute, longstring);
this.empty = !this.absolute && this.pathParts.length == 1 && this.pathParts[0].isEmpty();
}
this.normalized = null;
}
// Private constructor only for known correct split and extra value for absolute
UnionPath(final UnionFileSystem fileSystem, boolean absolute, final String... pathParts) {
this(fileSystem, absolute, false, pathParts);
}
private UnionPath(final UnionFileSystem fileSystem, boolean absolute, boolean isNormalized, final String... pathParts) {
this.fileSystem = fileSystem;
if (!absolute && (pathParts.length == 0 || (pathParts.length == 1 && pathParts[0].isEmpty()))) {
this.absolute = false;
this.pathParts = new String[]{ "" };
this.empty = true;
} else {
this.absolute = absolute;
this.empty = false;
this.pathParts = pathParts;
if (isNormalized)
this.normalized = this;
else
this.normalized = null;
}
}
private String[] getPathParts(final boolean isAbsolute, final String longstring) {
var clean = longstring.replace('\\', '/');
int startIndex = 0;
List<String> parts = new ArrayList<>();
while (startIndex != longstring.length()) {
int index = clean.indexOf('/', startIndex);
if (index == -1) {
parts.add(clean.substring(startIndex));
break;
}
// Skips double slash and slash and start/end
if (index != startIndex) {
parts.add(clean.substring(startIndex, index));
}
startIndex = (index + 1);
}
if (parts.isEmpty() && !isAbsolute) {
return new String[]{ "" };
} else {
return parts.toArray(String[]::new);
}
}
@Override
public UnionFileSystem getFileSystem() {
return this.fileSystem;
}
@Override
public boolean isAbsolute() {
return this.absolute;
}
@Override
public Path getRoot() {
if (!this.absolute)
return null;
return this.fileSystem.getRoot();
}
@Override
public Path getFileName() {
if (this.empty) {
return null;
} else if (this.pathParts.length > 0) {
return new UnionPath(this.getFileSystem(), false, this.pathParts[this.pathParts.length - 1]);
} else {
return this.absolute ? null : new UnionPath(this.fileSystem, false);
}
}
@Override
public Path getParent() {
if (this.pathParts.length > 1 || (this.absolute && this.pathParts.length == 1)) {
return new UnionPath(this.fileSystem, this.absolute, Arrays.copyOf(this.pathParts,this.pathParts.length - 1));
} else {
return null;
}
}
@Override
public int getNameCount() {
return this.pathParts.length;
}
@Override
public Path getName(final int index) {
if (index < 0 || index > this.pathParts.length -1) throw new IllegalArgumentException();
return new UnionPath(this.fileSystem, false, this.pathParts[index]);
}
@Override
public UnionPath subpath(final int beginIndex, final int endIndex) {
if (!this.absolute && this.pathParts.length == 0 && beginIndex == 0 && endIndex == 1)
return new UnionPath(this.fileSystem, false);
if (beginIndex < 0 || beginIndex > this.pathParts.length - 1 || endIndex < 0 || endIndex > this.pathParts.length || beginIndex >= endIndex) {
throw new IllegalArgumentException("Out of range "+beginIndex+" to "+endIndex+" for length "+this.pathParts.length);
}
if (!this.absolute && beginIndex == 0 && endIndex == this.pathParts.length) {
return this;
}
return new UnionPath(this.fileSystem, false, Arrays.copyOfRange(this.pathParts, beginIndex, endIndex));
}
@Override
public boolean startsWith(final Path other) {
if (other.getFileSystem() != this.getFileSystem()) {
return false;
}
if (other instanceof UnionPath bp) {
if (this.absolute != bp.absolute)
return false;
return checkArraysMatch(this.pathParts, bp.pathParts, false);
}
return false;
}
@Override
public boolean endsWith(final Path other) {
if (other.getFileSystem() != this.getFileSystem()) {
return false;
}
if (other instanceof UnionPath bp) {
if (!this.absolute && bp.absolute)
return false;
return checkArraysMatch(this.pathParts, bp.pathParts, true);
}
return false;
}
private static boolean checkArraysMatch(String[] array1, String[] array2, boolean reverse) {
var length = Math.min(array1.length, array2.length);
IntBinaryOperator offset = reverse ? (l, i) -> l - i - 1 : (l, i) -> i;
for (int i = 0; i < length; i++) {
if (!Objects.equals(array1[offset.applyAsInt(array1.length, i)], array2[offset.applyAsInt(array2.length, i)]))
return false;
}
return true;
}
@Override
public Path normalize() {
if (normalized != null)
return normalized;
Deque<String> normpath = new ArrayDeque<>();
for (String pathPart : this.pathParts) {
switch (pathPart) {
case ".":
break;
case "..":
if (!this.absolute && (normpath.isEmpty() || normpath.getLast().equals(".."))) {
// .. on an empty path is allowed as long as it is not absolute, so keep it
normpath.addLast(pathPart);
} else if (!normpath.isEmpty()) {
normpath.removeLast();
}
break;
default:
normpath.addLast(pathPart);
break;
}
}
normalized = new UnionPath(this.fileSystem, this.absolute, true, normpath.toArray(new String[0]));
return normalized;
}
@Override
public Path resolve(final Path other) {
if (other instanceof UnionPath path) {
if (path.isAbsolute() || this.empty) {
return path;
}
if (path.empty) {
return this;
}
String[] mergedParts = new String[this.pathParts.length + path.pathParts.length];
System.arraycopy(this.pathParts, 0, mergedParts, 0, this.pathParts.length);
System.arraycopy(path.pathParts, 0, mergedParts, this.pathParts.length, path.pathParts.length);
return new UnionPath(this.fileSystem, this.absolute, mergedParts);
}
return other;
}
@Override
public Path relativize(final Path other) {
if (other.getFileSystem()!=this.getFileSystem()) throw new IllegalArgumentException("Wrong filesystem");
if (other instanceof UnionPath p) {
if (this.absolute != p.absolute) {
throw new IllegalArgumentException("Different types of path");
}
var length = Math.min(this.pathParts.length, p.pathParts.length);
int i = 0;
while (i < length) {
if (!Objects.equals(this.pathParts[i], p.pathParts[i]))
break;
i++;
}
var remaining = this.pathParts.length - i;
if (remaining == 0 && i == p.pathParts.length) {
return new UnionPath(this.getFileSystem(), false);
} else if (remaining == 0) {
return p.subpath(i, p.getNameCount());
} else {
var updots = IntStream.range(0, remaining).mapToObj(idx -> "..").toArray(String[]::new);
if (i == p.pathParts.length) {
return new UnionPath(this.getFileSystem(), false, updots);
} else {
var subpath = p.subpath(i, p.getNameCount());
String[] mergedParts = new String[updots.length + subpath.pathParts.length];
System.arraycopy(updots, 0, mergedParts, 0, updots.length);
System.arraycopy(subpath.pathParts, 0, mergedParts, updots.length, subpath.pathParts.length);
return new UnionPath(this.getFileSystem(), false, mergedParts);
}
}
}
throw new IllegalArgumentException("Wrong filesystem");
}
@Override
public URI toUri() {
try {
return new URI(
fileSystem.provider().getScheme(),
null,
fileSystem.getKey() + '!' + toAbsolutePath(),
null
);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
public Path toAbsolutePath() {
if (isAbsolute())
return this;
else
return fileSystem.getRoot().resolve(this);
}
@Override
public Path toRealPath(final LinkOption... options) throws IOException {
return this.toAbsolutePath().normalize();
}
@Override
public WatchKey register(final WatchService watcher, final WatchEvent.Kind<?>[] events, final WatchEvent.Modifier... modifiers) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public int compareTo(final Path other) {
if (other instanceof UnionPath path) {
if (this.absolute && !path.absolute)
return 1;
else if (!this.absolute && path.absolute)
return -1;
else
return Arrays.compare(this.pathParts, path.pathParts);
} else {
return 0;
}
}
@Override
public boolean equals(final Object o) {
if (o instanceof UnionPath p) {
return p.getFileSystem() == this.getFileSystem() && this.absolute == p.absolute && Arrays.equals(this.pathParts, p.pathParts);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(this.fileSystem) + 31 * Arrays.hashCode(this.pathParts);
}
@Override
public String toString() {
return (this.absolute ? UnionFileSystem.SEP_STRING : "") + String.join(UnionFileSystem.SEP_STRING, this.pathParts);
}
public InputStream buildInputStream() {
return fileSystem.buildInputStream(this);
}
}