1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.property.basic;
25
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29 import net.jcip.annotations.Immutable;
30 import org.modeshape.common.text.Inflector;
31 import org.modeshape.common.text.TextEncoder;
32 import org.modeshape.common.util.CheckArg;
33 import org.modeshape.graph.GraphI18n;
34 import org.modeshape.graph.property.InvalidPathException;
35 import org.modeshape.graph.property.Name;
36 import org.modeshape.graph.property.NamespaceRegistry;
37 import org.modeshape.graph.property.Path;
38
39
40
41
42 @Immutable
43 public class RootPath extends AbstractPath {
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50 public static final Path INSTANCE = new RootPath();
51
52 private static final Path.Segment[] EMPTY_SEGMENT_ARRAY = new Path.Segment[] {};
53 private static final List<Path.Segment> EMPTY_SEGMENT_LIST = Collections.emptyList();
54
55 private RootPath() {
56 }
57
58
59
60
61
62
63 public Path getAncestor( int degree ) {
64 CheckArg.isNonNegative(degree, "degree");
65 if (degree == 0) {
66 return this;
67 }
68 String msg = GraphI18n.pathAncestorDegreeIsInvalid.text(this.getString(), Inflector.getInstance().ordinalize(degree));
69 throw new InvalidPathException(msg);
70 }
71
72
73
74
75
76
77 @Override
78 protected Iterator<Segment> getSegmentsOfParent() {
79 return EMPTY_PATH_ITERATOR;
80 }
81
82
83
84
85
86
87 @Override
88 public Path getCanonicalPath() {
89 return this;
90 }
91
92
93
94
95
96
97 @Override
98 public Path getCommonAncestor( Path that ) {
99 CheckArg.isNotNull(that, "that");
100 return this;
101 }
102
103
104
105
106
107
108 @Override
109 public Segment getLastSegment() {
110 return null;
111 }
112
113
114
115
116
117
118 @Override
119 public boolean endsWith( Name nameOfLastSegment ) {
120 return false;
121 }
122
123
124
125
126
127
128 @Override
129 public boolean endsWith( Name nameOfLastSegment,
130 int snsIndex ) {
131 return false;
132 }
133
134
135
136
137
138
139 @Override
140 public Path getNormalizedPath() {
141 return this;
142 }
143
144
145
146
147
148
149 @Override
150 public Path relativeToRoot() {
151 return BasicPath.SELF_PATH;
152 }
153
154
155
156
157
158
159 @Override
160 public Path resolve( Path relativePath ) {
161 CheckArg.isNotNull(relativePath, "relative path");
162 if (relativePath.isAbsolute()) {
163 String msg = GraphI18n.pathIsNotRelative.text(relativePath);
164 throw new InvalidPathException(msg);
165 }
166
167 return new BasicPath(relativePath.getSegmentsList(), true).getNormalizedPath();
168 }
169
170
171
172
173
174
175 @Override
176 public Path getParent() {
177 return null;
178 }
179
180
181
182
183
184
185 @Override
186 public Segment getSegment( int index ) {
187 CheckArg.isNonNegative(index, "index");
188 EMPTY_SEGMENT_LIST.get(index);
189 return null;
190 }
191
192
193
194
195
196
197 @Override
198 public Segment[] getSegmentsArray() {
199
200 return EMPTY_SEGMENT_ARRAY;
201 }
202
203
204
205
206
207
208 public List<Segment> getSegmentsList() {
209 return EMPTY_SEGMENT_LIST;
210 }
211
212
213
214
215
216
217 @Override
218 public String getString() {
219 return Path.DELIMITER_STR;
220 }
221
222
223
224
225
226
227 @Override
228 public String getString( TextEncoder encoder ) {
229 return Path.DELIMITER_STR;
230 }
231
232
233
234
235
236
237 @Override
238 public String getString( NamespaceRegistry namespaceRegistry ) {
239 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
240 return Path.DELIMITER_STR;
241 }
242
243
244
245
246
247
248
249 @Override
250 public String getString( NamespaceRegistry namespaceRegistry,
251 TextEncoder encoder ) {
252 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
253 return Path.DELIMITER_STR;
254 }
255
256
257
258
259
260
261
262 @Override
263 public String getString( NamespaceRegistry namespaceRegistry,
264 TextEncoder encoder,
265 TextEncoder delimiterEncoder ) {
266 return (delimiterEncoder == null) ? DELIMITER_STR : delimiterEncoder.encode(DELIMITER_STR);
267 }
268
269
270
271
272
273
274 @Override
275 public boolean hasSameAncestor( Path that ) {
276 CheckArg.isNotNull(that, "that");
277 return true;
278 }
279
280
281
282
283
284
285 public boolean isAbsolute() {
286 return true;
287 }
288
289
290
291
292
293
294 @Override
295 public boolean isAncestorOf( Path decendant ) {
296 CheckArg.isNotNull(decendant, "decendant");
297 return !decendant.isRoot();
298 }
299
300
301
302
303
304
305 @Override
306 public boolean isAtOrAbove( Path other ) {
307 CheckArg.isNotNull(other, "other");
308 return true;
309 }
310
311
312
313
314
315
316 @Override
317 public boolean isAtOrBelow( Path other ) {
318 CheckArg.isNotNull(other, "other");
319 return other.isRoot();
320 }
321
322
323
324
325
326
327 @Override
328 public boolean isDecendantOf( Path ancestor ) {
329 CheckArg.isNotNull(ancestor, "ancestor");
330 return false;
331 }
332
333
334
335
336
337
338 public boolean isNormalized() {
339 return true;
340 }
341
342
343
344
345
346
347 public boolean isRoot() {
348 return true;
349 }
350
351
352
353
354
355
356 @Override
357 public boolean isSameAs( Path other ) {
358 CheckArg.isNotNull(other, "other");
359 return other.isRoot();
360 }
361
362
363
364
365
366
367 @Override
368 public Iterator<Segment> iterator() {
369 return EMPTY_SEGMENT_LIST.iterator();
370 }
371
372
373
374
375
376
377 @Override
378 public Iterator<Path> pathsFromRoot() {
379 return new SingleIterator<Path>(this);
380 }
381
382
383
384
385
386
387 public int size() {
388 return 0;
389 }
390
391
392
393
394
395
396 @Override
397 public Path subpath( int beginIndex ) {
398 CheckArg.isNonNegative(beginIndex, "beginIndex");
399 if (beginIndex == 0) return this;
400 EMPTY_SEGMENT_LIST.get(1);
401 return null;
402 }
403
404
405
406
407
408
409 @Override
410 public Path subpath( int beginIndex,
411 int endIndex ) {
412 CheckArg.isNonNegative(beginIndex, "beginIndex");
413 CheckArg.isNonNegative(endIndex, "endIndex");
414 if (endIndex >= 1) {
415 EMPTY_SEGMENT_LIST.get(endIndex);
416 }
417 return this;
418 }
419
420
421
422
423
424
425 @Override
426 public int compareTo( Path other ) {
427 return other.isRoot() ? 0 : -1;
428 }
429
430
431
432
433
434
435 @Override
436 public boolean equals( Object obj ) {
437 if (obj == this) return true;
438 if (obj instanceof Path) {
439 Path that = (Path)obj;
440 return that.isRoot();
441 }
442 return false;
443 }
444
445 }