-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
RelativePath.java
306 lines (273 loc) · 8.99 KB
/
RelativePath.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
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
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.file;
import org.gradle.internal.file.FilePathUtil;
import javax.annotation.Nullable;
import java.io.File;
import java.io.Serializable;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ListIterator;
/**
* <p>Represents a relative path from some base directory to a file. Used in file copying to represent both a source
* and target file path when copying files.</p>
*
* <p>{@code RelativePath} instances are immutable.</p>
*/
public class RelativePath implements Serializable, Comparable<RelativePath>, CharSequence {
public static final RelativePath EMPTY_ROOT = new RelativePath(false);
private final boolean endsWithFile;
private final String[] segments;
/**
* Creates a {@code RelativePath}.
*
* @param endsWithFile - if true, the path ends with a file, otherwise a directory
*/
public RelativePath(boolean endsWithFile, String... segments) {
this(endsWithFile, null, segments);
}
private RelativePath(boolean endsWithFile, @Nullable RelativePath parentPath, String... childSegments) {
this.endsWithFile = endsWithFile;
final int expectedLength;
if (parentPath != null) {
expectedLength = parentPath.segments.length + childSegments.length;
} else {
expectedLength = childSegments.length;
}
String[] newSegments = new String[expectedLength];
int nextIndex = 0;
if (parentPath != null) {
System.arraycopy(parentPath.segments, 0, newSegments, 0, parentPath.segments.length);
nextIndex = parentPath.segments.length;
}
for (String segment : childSegments) {
if (segment.equals(".")) {
continue;
}
if (segment.equals("..")) {
if (nextIndex == 0 || newSegments[nextIndex - 1].equals("..")) {
newSegments[nextIndex] = "..";
nextIndex++;
} else {
nextIndex--;
}
continue;
}
newSegments[nextIndex] = segment;
nextIndex++;
}
if (nextIndex < newSegments.length) {
// Truncate the array to the filled portion
newSegments = Arrays.copyOf(newSegments, nextIndex);
}
this.segments = newSegments;
}
private static void copySegments(String[] target, String[] source) {
copySegments(target, source, target.length);
}
private static void copySegments(String[] target, String[] source, int length) {
System.arraycopy(source, 0, target, 0, length);
}
public String[] getSegments() {
return segments;
}
public ListIterator<String> segmentIterator() {
ArrayList<String> content = new ArrayList<String>(Arrays.asList(segments));
return content.listIterator();
}
public boolean isFile() {
return endsWithFile;
}
public String getPathString() {
if (segments.length == 0) {
return "";
}
StringBuilder path = new StringBuilder(256);
for (int i = 0, len = segments.length; i < len; i++) {
if (i != 0) {
path.append('/');
}
path.append(segments[i]);
}
return path.toString();
}
@Override
public int length() {
if (segments.length == 0) {
return 0;
}
int length = segments.length - 1;
for (String segment : segments) {
length += segment.length();
}
return length;
}
@Override
public char charAt(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
int remaining = index;
int nextSegment = 0;
while (nextSegment < segments.length) {
String segment = segments[nextSegment];
int length = segment.length();
if (remaining < length) {
return segment.charAt(remaining);
} else if (remaining == length) {
return '/';
} else {
remaining -= length + 1;
nextSegment++;
}
}
throw new IndexOutOfBoundsException(String.valueOf(index));
}
@Override
public CharSequence subSequence(int start, int end) {
return CharBuffer.wrap(this, start, end);
}
public File getFile(File baseDir) {
return new File(baseDir, getPathString());
}
public String getLastName() {
if (segments.length > 0) {
return segments[segments.length - 1];
} else {
return null;
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RelativePath that = (RelativePath) o;
if (endsWithFile != that.endsWithFile) {
return false;
}
return Arrays.equals(segments, that.segments);
}
@Override
public int hashCode() {
int result = endsWithFile ? 1 : 0;
result = 31 * result + Arrays.hashCode(segments);
return result;
}
@Override
public String toString() {
return getPathString();
}
/**
* Returns the parent of this path.
*
* @return The parent of this path, or null if this is the root path.
*/
public RelativePath getParent() {
switch (segments.length) {
case 0:
return null;
case 1:
return EMPTY_ROOT;
default:
String[] parentSegments = new String[segments.length - 1];
copySegments(parentSegments, segments);
return new RelativePath(false, parentSegments);
}
}
public static RelativePath parse(boolean isFile, String path) {
return parse(isFile, null, path);
}
public static RelativePath parse(boolean isFile, @Nullable RelativePath parent, String path) {
String[] names = FilePathUtil.getPathSegments(path);
return new RelativePath(isFile, parent, names);
}
/**
* <p>Returns a copy of this path, with the last name replaced with the given name.</p>
*
* @param name The name.
* @return The path.
*/
public RelativePath replaceLastName(String name) {
String[] newSegments = new String[segments.length];
copySegments(newSegments, segments, segments.length - 1);
newSegments[segments.length - 1] = name;
return new RelativePath(endsWithFile, newSegments);
}
/**
* <p>Appends the given path to the end of this path.
*
* @param other The path to append
* @return The new path
*/
public RelativePath append(RelativePath other) {
return new RelativePath(other.endsWithFile, this, other.segments);
}
/**
* <p>Appends the given path to the end of this path.
*
* @param other The path to append
* @return The new path
*/
public RelativePath plus(RelativePath other) {
return append(other);
}
/**
* Appends the given names to the end of this path.
*
* @param segments The names to append.
* @param endsWithFile when true, the new path refers to a file.
* @return The new path.
*/
public RelativePath append(boolean endsWithFile, String... segments) {
return new RelativePath(endsWithFile, this, segments);
}
/**
* Prepends the given names to the start of this path.
*
* @param segments The names to prepend
* @return The new path.
*/
public RelativePath prepend(String... segments) {
return new RelativePath(false, segments).append(this);
}
@Override
public int compareTo(RelativePath o) {
int len1 = segments.length;
int len2 = o.segments.length;
if (len1 != len2) {
return len1 - len2;
}
int lim = Math.min(len1, len2);
String[] v1 = segments;
String[] v2 = o.segments;
int k = 0;
while (k < lim) {
String c1 = v1[k];
String c2 = v2[k];
int compareResult = c1 == c2 ? 0 : c1.compareTo(c2);
if (compareResult != 0) {
return compareResult;
}
k++;
}
return 0;
}
}