Repository navigation
Expand file tree
/
Copy pathPath.java
More file actions
71 lines (58 loc) · 1.68 KB
/
Copy pathPath.java
File metadata and controls
71 lines (58 loc) · 1.68 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
package graphql.execution;
public class Path {
private final Path parent;
private PathComponent tail;
public static Path root= new Path();
private Path() {
parent = null;
tail = null;
}
public Path(String topLevelField) {
this(null, new StringPathComponent(topLevelField));
}
private Path(Path parent, PathComponent tail) {
this.parent = parent;
this.tail = tail;
}
public Path withTrailing(String trailing) {
return new Path(this, new StringPathComponent(trailing));
}
public Path withTrailing(int index) {
return new Path(this, new IntPathComponent(index));
}
@Override
public String toString() {
if (tail == null) {
return "";
}
if (parent == root) {
return tail.toString().substring(1);
}
return parent.toString() + tail.toString();
}
private interface PathComponent {
}
private static class StringPathComponent implements PathComponent {
private final String value;
public StringPathComponent(String value) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("empty path component");
}
this.value = value;
}
@Override
public String toString() {
return '/' + value;
}
}
private static class IntPathComponent implements PathComponent {
private final int value;
public IntPathComponent(int value) {
this.value = value;
}
@Override
public String toString() {
return "[" + value + ']';
}
}
}