|
2 | 2 |
|
3 | 3 | import java.util.Objects; |
4 | 4 |
|
| 5 | +/** |
| 6 | + * Represents a JSON path (e.g., $.property.0.subproperty). |
| 7 | + * Stored in reverse order (last element at head) for O(1) add operations |
| 8 | + * and efficient end-matching in PathMatcher. |
| 9 | + */ |
5 | 10 | public class Path { |
6 | 11 | public static final Path ROOT = new Path(); |
7 | 12 |
|
8 | | - public final PathItem property; |
9 | | - public final Path next; |
| 13 | + private final PathItem last; |
| 14 | + private final Path previous; |
10 | 15 |
|
11 | 16 | public Path() { |
12 | 17 | this(null, null); |
13 | 18 | } |
14 | 19 |
|
15 | | - private Path(PathItem property, Path next) { |
16 | | - this.property = property; |
17 | | - this.next = next; |
| 20 | + private Path(PathItem last, Path previous) { |
| 21 | + this.last = last; |
| 22 | + this.previous = previous; |
18 | 23 | } |
19 | 24 |
|
20 | | - private Path(PathItem property) { |
21 | | - this.property = property; |
22 | | - this.next = null; |
| 25 | + public Path add(PathItem item) { |
| 26 | + if (this.last == null) { |
| 27 | + return new Path(item, null); |
| 28 | + } |
| 29 | + return new Path(item, this); |
23 | 30 | } |
24 | 31 |
|
25 | | - public Path add(PathItem item) { |
26 | | - if (this.next == null) { |
27 | | - return new Path(this.property, new Path(item)); |
28 | | - } else { |
29 | | - return new Path(this.property, this.next.add(item)); |
| 32 | + public PathItem item() { |
| 33 | + return last; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the path without its last element. |
| 38 | + */ |
| 39 | + public Path previous() { |
| 40 | + return previous == null ? ROOT : previous; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns the path items in natural order (from root to leaf). |
| 45 | + * This is useful for traversing the path from start to end. |
| 46 | + */ |
| 47 | + public java.util.List<PathItem> toList() { |
| 48 | + java.util.List<PathItem> result = new java.util.ArrayList<>(); |
| 49 | + collectItems(result); |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + private void collectItems(java.util.List<PathItem> result) { |
| 54 | + if (last == null) return; |
| 55 | + if (previous != null) { |
| 56 | + previous.collectItems(result); |
30 | 57 | } |
| 58 | + result.add(last); |
31 | 59 | } |
32 | 60 |
|
| 61 | + @Override |
33 | 62 | public String toString() { |
34 | | - return ((this.property == null) ? "$" : this.property) + |
35 | | - ((this.next == null) ? "" : "." + this.next); |
| 63 | + StringBuilder sb = new StringBuilder("$"); |
| 64 | + appendReversed(sb); |
| 65 | + return sb.toString(); |
| 66 | + } |
| 67 | + |
| 68 | + private void appendReversed(StringBuilder sb) { |
| 69 | + if (last == null) return; |
| 70 | + if (previous != null) { |
| 71 | + previous.appendReversed(sb); |
| 72 | + } |
| 73 | + sb.append(".").append(last); |
36 | 74 | } |
37 | 75 |
|
38 | 76 | @Override |
39 | 77 | public boolean equals(Object o) { |
40 | 78 | if (this == o) return true; |
41 | 79 | if (o == null || getClass() != o.getClass()) return false; |
42 | 80 | Path path = (Path) o; |
43 | | - return Objects.equals(property, path.property) && Objects.equals(next, path.next); |
| 81 | + return Objects.equals(last, path.last) && Objects.equals(previous, path.previous); |
44 | 82 | } |
45 | 83 |
|
46 | 84 | @Override |
47 | 85 | public int hashCode() { |
48 | | - return Objects.hash(property, next); |
| 86 | + return Objects.hash(last, previous); |
49 | 87 | } |
50 | 88 |
|
51 | 89 | public interface PathItem { |
|
0 commit comments