/
AbstractPath.java
561 lines (505 loc) · 20.4 KB
/
AbstractPath.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.jcr.value.basic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import org.modeshape.common.CommonI18n;
import org.modeshape.common.annotation.Immutable;
import org.modeshape.common.annotation.NotThreadSafe;
import org.modeshape.common.text.TextEncoder;
import org.modeshape.common.util.CheckArg;
import org.modeshape.jcr.GraphI18n;
import org.modeshape.jcr.value.InvalidPathException;
import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.NamespaceRegistry;
import org.modeshape.jcr.value.Path;
/**
* An abstract foundation for different {@link Path} implementations. This class does not manage any of the {@link Path}'s state,
* but it does provide implementations for most of the methods based upon a few abstract methods. For example, any implementaton
* that requires the {@link org.modeshape.jcr.value.Path.Segment path's segments} are written to use the {@link #iterator()}, since that is likely more
* efficient for the majority of implementations.
*/
@Immutable
public abstract class AbstractPath implements Path {
/**
* The initial serializable version. Version {@value}
*/
private static final long serialVersionUID = 1L;
public static final Path SELF_PATH = new BasicPath(Collections.singletonList(Path.SELF_SEGMENT), false);
protected static Iterator<Path.Segment> EMPTY_PATH_ITERATOR = new Iterator<Segment>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public Segment next() {
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
@NotThreadSafe
protected static class SingleIterator<T> implements Iterator<T> {
private T value;
protected SingleIterator( T value ) {
this.value = value;
}
@Override
public boolean hasNext() {
return value != null;
}
@Override
public T next() {
if (value == null) throw new NoSuchElementException();
T next = value;
value = null;
return next;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
private transient int hc = 0;
protected boolean isNormalized( List<Segment> segments ) {
boolean nonParentReference = false;
boolean first = isAbsolute(); // only care about first one when it's absolute
for (Segment segment : segments) {
if (segment.isSelfReference()) return false;
if (segment.isParentReference()) {
if (nonParentReference || first) return false;
} else {
nonParentReference = true;
}
first = false;
}
return true;
}
@Override
public boolean isIdentifier() {
return false;
}
@Override
public Path getCanonicalPath() {
if (!this.isAbsolute()) {
String msg = GraphI18n.pathIsNotAbsolute.text(this);
throw new InvalidPathException(msg);
}
if (this.isNormalized()) return this;
return this.getNormalizedPath();
}
@Override
public Path getCommonAncestor( Path that ) {
CheckArg.isNotNull(that, "that");
if (that.isRoot()) return that;
Path normalizedPath = this.getNormalizedPath();
int lastIndex = 0;
Iterator<Segment> thisIter = normalizedPath.iterator();
Iterator<Segment> thatIter = that.getNormalizedPath().iterator();
while (thisIter.hasNext() && thatIter.hasNext()) {
Segment thisSeg = thisIter.next();
Segment thatSeg = thatIter.next();
if (thisSeg.equals(thatSeg)) {
++lastIndex;
} else {
break;
}
}
if (lastIndex == 0) return RootPath.INSTANCE;
return normalizedPath.subpath(0, lastIndex);
}
@Override
public Path.Segment getLastSegment() {
return this.getSegmentsList().get(size() - 1);
}
@Override
public boolean endsWith( Name nameOfLastSegment ) {
Segment segment = getLastSegment();
return segment != null && segment.getName().equals(nameOfLastSegment) && !segment.hasIndex();
}
@Override
public boolean endsWith( Name nameOfLastSegment,
int snsIndex ) {
Segment segment = getLastSegment();
return segment != null && segment.getName().equals(nameOfLastSegment) && segment.getIndex() == snsIndex;
}
@Override
public Path getParent() {
return getAncestor(1);
}
@Override
public Segment getSegment( int index ) {
CheckArg.isNonNegative(index, "index");
return this.getSegmentsList().get(index);
}
@Override
public Segment[] getSegmentsArray() {
// By default, make a new array every time since arrays are mutable, and use the iterator
// since that is probably more efficient than creating a list ...
Segment[] result = new Path.Segment[size()];
int i = 0;
for (Segment segment : this) {
result[i] = segment;
++i;
}
return result;
}
@Override
public Path getNormalizedPath() {
if (this.isNormalized()) return this;
LinkedList<Segment> newSegments = new LinkedList<Segment>();
for (Segment segment : this) {
if (segment.isSelfReference()) continue;
if (segment.isParentReference()) {
if (newSegments.isEmpty()) {
if (this.isAbsolute()) {
throw new InvalidPathException(CommonI18n.pathCannotBeNormalized.text(this));
}
} else if (!newSegments.getLast().isParentReference()) {
newSegments.removeLast();
continue;
}
}
newSegments.add(segment);
}
if (newSegments.isEmpty()) {
if (this.isAbsolute()) return RootPath.INSTANCE;
// Otherwise relative and it had contained nothing but self references ...
return SELF_PATH;
}
return new BasicPath(newSegments, this.isAbsolute());
}
@Override
public String getString() {
return doGetString(null, null, null);
}
@Override
public String getString( TextEncoder encoder ) {
return doGetString(null, encoder, null);
}
@Override
public String getString( NamespaceRegistry namespaceRegistry ) {
CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
return doGetString(namespaceRegistry, null, null);
}
@Override
public String getString( NamespaceRegistry namespaceRegistry,
TextEncoder encoder ) {
CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
return doGetString(namespaceRegistry, encoder, null);
}
@Override
public String getString( NamespaceRegistry namespaceRegistry,
TextEncoder encoder,
TextEncoder delimiterEncoder ) {
return doGetString(namespaceRegistry, encoder, delimiterEncoder);
}
/**
* Method that creates the string representation. This method works two different ways depending upon whether the namespace
* registry is provided.
*
* @param namespaceRegistry
* @param encoder
* @param delimiterEncoder
* @return this path as a string
*/
protected String doGetString( NamespaceRegistry namespaceRegistry,
TextEncoder encoder,
TextEncoder delimiterEncoder ) {
if (encoder == null) encoder = DEFAULT_ENCODER;
final String delimiter = delimiterEncoder != null ? delimiterEncoder.encode(DELIMITER_STR) : DELIMITER_STR;
// Since the segments are immutable, this code need not be synchronized because concurrent threads
// may just compute the same value (with no harm done)
StringBuilder sb = new StringBuilder();
if (this.isAbsolute()) sb.append(delimiter);
boolean first = true;
for (Segment segment : this) {
if (first) {
first = false;
} else {
sb.append(delimiter);
}
assert segment != null;
sb.append(segment.getString(namespaceRegistry, encoder, delimiterEncoder));
}
String result = sb.toString();
// Save the result to the internal string if this the default encoder is used.
// This is not synchronized, but it's okay
return result;
}
@Override
public boolean hasSameAncestor( Path that ) {
CheckArg.isNotNull(that, "that");
if (that.size() != this.size()) return false;
if (this.size() == 1) return true; // both nodes are just under the root
for (int i = this.size() - 2; i >= 0; --i) {
Path.Segment thisSegment = this.getSegment(i);
Path.Segment thatSegment = that.getSegment(i);
if (!thisSegment.equals(thatSegment)) return false;
}
return true;
}
@Override
public boolean isAncestorOf( Path descendant ) {
CheckArg.isNotNull(descendant, "descendant");
return descendant.isDescendantOf(this);
}
@Override
public boolean isAtOrBelow( Path other ) {
CheckArg.isNotNull(other, "other");
if (this == other) return true;
if (other.isRoot()) return true;
if (other.size() > this.size()) return false;
// Find this path's ancestor that is of the same size ...
Path thisPath = this.getAncestor(this.size() - other.size());
Path thatPath = other;
assert thisPath.size() == other.size();
// Walk up and compare; it is more likely that the lower path segments differ (plus it's usually more efficient) ...
while (!thisPath.isRoot() && thisPath.getLastSegment().equals(thatPath.getLastSegment())) {
thisPath = thisPath.getParent();
thatPath = thatPath.getParent();
}
return thisPath.isRoot();
}
@Override
public boolean isAtOrAbove( Path other ) {
CheckArg.isNotNull(other, "other");
return other.isAtOrBelow(this);
}
@Override
public boolean isDescendantOf( Path ancestor ) {
CheckArg.isNotNull(ancestor, "ancestor");
if (this == ancestor) return false;
if (ancestor.isRoot()) return true;
if (ancestor.size() >= this.size()) return false;
// Find this path's ancestor that is of the same size ...
Path thisPath = this.getAncestor(this.size() - ancestor.size());
Path thatPath = ancestor;
assert thisPath.size() == thatPath.size();
// Walk up and compare; it is more likely that the lower path segments differ (plus it's usually more efficient) ...
while (!thisPath.isRoot() && thisPath.getLastSegment().equals(thatPath.getLastSegment())) {
thisPath = thisPath.getParent();
thatPath = thatPath.getParent();
}
return thisPath.isRoot();
}
@Override
public boolean isSameAs( Path other ) {
return other != null && this.compareTo(other) == 0;
}
@Override
public Iterator<Segment> iterator() {
return getSegmentsList().iterator();
}
@Override
public Iterator<Path> pathsFromRoot() {
LinkedList<Path> paths = new LinkedList<Path>();
Path path = this;
while (path != null) {
paths.addFirst(path);
if (path.isRoot()) break;
path = path.getParent();
}
return paths.iterator();
}
@Override
public Path relativeToRoot() {
return new BasicPath(getSegmentsList(), false);
}
@Override
public Path relativeTo( Path startingPath ) {
CheckArg.isNotNull(startingPath, "to");
if (!this.isAbsolute()) {
String msg = GraphI18n.pathIsNotAbsolute.text(this);
throw new InvalidPathException(msg);
}
if (startingPath.isRoot()) {
// We just want a relative path containing the same segments ...
return relativeToRoot();
}
if (!startingPath.isAbsolute()) {
String msg = GraphI18n.pathIsNotAbsolute.text(startingPath);
throw new InvalidPathException(msg);
}
// Count the number of segments up to the common ancestor (relative path is what remains) ...
int lengthOfCommonAncestor = 0;
Iterator<Segment> thisIter = this.getNormalizedPath().iterator();
Iterator<Segment> toIter = startingPath.getNormalizedPath().iterator();
while (thisIter.hasNext() && toIter.hasNext()) {
Segment thisSeg = thisIter.next();
Segment toSeg = toIter.next();
if (thisSeg.equals(toSeg)) {
++lengthOfCommonAncestor;
} else {
break;
}
}
// Create the relative path, starting with parent references to the common ancestor ...
int numberOfParentReferences = startingPath.size() - lengthOfCommonAncestor;
List<Segment> relativeSegments = new ArrayList<Segment>();
for (int i = 0; i != numberOfParentReferences; ++i) {
relativeSegments.add(Path.PARENT_SEGMENT);
}
// Add the segments of this path from the common ancestor ...
for (int i = lengthOfCommonAncestor; i < this.size(); ++i) {
relativeSegments.add(getSegment(i));
}
if (relativeSegments.isEmpty()) {
relativeSegments.add(Path.SELF_SEGMENT);
}
return new BasicPath(relativeSegments, false);
}
@Override
public Path resolve( Path relativePath ) {
CheckArg.isNotNull(relativePath, "relative path");
if (!this.isAbsolute()) {
String msg = GraphI18n.pathIsNotAbsolute.text(this);
throw new InvalidPathException(msg);
}
if (relativePath.isAbsolute()) {
String msg = GraphI18n.pathIsNotRelative.text(relativePath);
throw new InvalidPathException(msg);
}
// If the relative path is the self or parent reference ...
relativePath = relativePath.getNormalizedPath();
if (relativePath.size() == 1) {
Segment onlySegment = relativePath.getSegment(0);
if (onlySegment.isSelfReference()) return this;
if (onlySegment.isParentReference()) return this.getParent();
}
List<Segment> segments = new ArrayList<Segment>(this.size() + relativePath.size());
for (Segment segment : this) {
segments.add(segment);
}
for (Segment segment : relativePath) {
segments.add(segment);
}
return new BasicPath(segments, true).getNormalizedPath();
}
@Override
public Path resolveAgainst( Path absolutePath ) {
CheckArg.isNotNull(absolutePath, "absolute path");
return absolutePath.resolve(this);
}
@Override
public Path subpath( int beginIndex ) {
return subpath(beginIndex, size());
}
@Override
public Path subpath( int beginIndex,
int endIndex ) {
CheckArg.isNonNegative(beginIndex, "beginIndex");
CheckArg.isNonNegative(endIndex, "endIndex");
int size = size();
if (beginIndex == 0) {
if (endIndex == 0) return RootPath.INSTANCE;
if (endIndex == size) return this;
}
if (beginIndex >= size) {
throw new IndexOutOfBoundsException(
GraphI18n.unableToCreateSubpathBeginIndexGreaterThanOrEqualToSize.text(beginIndex,
size));
}
if (beginIndex > endIndex) {
throw new IndexOutOfBoundsException(
GraphI18n.unableToCreateSubpathBeginIndexGreaterThanOrEqualToEndingIndex.text(beginIndex,
endIndex));
}
// This reuses the same list, so it's pretty efficient ...
return new BasicPath(createSegmentsSubList(beginIndex, endIndex), this.isAbsolute());
}
protected List<Segment> createSegmentsSubList( int validBeginIndex,
int validEndIndex ) {
return this.getSegmentsList().subList(validBeginIndex, validEndIndex);
}
@Override
public int hashCode() {
if (hc == 0) {
int hashCode = 1;
for (Segment segment : this) {
hashCode = 31 * hashCode + segment.hashCode();
}
hc = hashCode;
}
return hc;
}
/**
* Method used by {@link AbstractPath#equals(Object)} implementation to quickly get an Iterator over the segments in the
* parent.
*
* @return the iterator over the segments; never null, but may not have any elements
*/
protected abstract Iterator<Segment> getSegmentsOfParent();
@Override
public boolean equals( Object obj ) {
if (obj == this) return true;
if (obj instanceof Path) {
Path that = (Path)obj;
// First check whether the paths are roots ...
if (this.isRoot()) return that.isRoot();
else if (that.isRoot()) return false;
// Now check the hash code and size ...
if (this.hashCode() != that.hashCode()) return false;
if (this.size() != that.size()) return false;
// Check the last segments, since these will often differ anyway ...
if (!this.getLastSegment().equals(that.getLastSegment())) return false;
if (this.size() == 1) return true;
// Check the rest of the names ...
Iterator<Segment> thisIter = that instanceof AbstractPath ? this.getSegmentsOfParent() : this.iterator();
Iterator<Segment> thatIter = that instanceof AbstractPath ? ((AbstractPath)that).getSegmentsOfParent() : that.iterator();
while (thisIter.hasNext()) {
Segment thisSegment = thisIter.next();
Segment thatSegment = thatIter.next();
if (!thisSegment.equals(thatSegment)) return false;
}
return true;
}
return false;
}
@Override
public int compareTo( Path that ) {
if (this == that) return 0;
Iterator<Segment> thisIter = getSegmentsList().iterator();
Iterator<Segment> thatIter = that.iterator();
while (thisIter.hasNext() && thatIter.hasNext()) {
Segment thisSegment = thisIter.next();
Segment thatSegment = thatIter.next();
int diff = thisSegment.compareTo(thatSegment);
if (diff != 0) return diff;
}
if (thisIter.hasNext()) return 1;
if (thatIter.hasNext()) return -1;
return 0;
}
@Override
public String toString() {
return getString(Path.NO_OP_ENCODER);
}
}