|
1 | 1 | package com.deblock.jsondiff.matcher; |
2 | 2 |
|
3 | | - |
4 | 3 | public class PathMatcher { |
5 | | - public final PathMatcher.PathMatcherItem property; |
6 | | - public final PathMatcher next; |
| 4 | + public final PathMatcherItem last; |
| 5 | + public final PathMatcher previous; |
7 | 6 |
|
8 | 7 | public static PathMatcher from(String path) { |
9 | 8 | PathMatcher matcher = new PathMatcher(); |
10 | | - for (String part: path.split("\\.")) { |
| 9 | + for (String part : path.split("\\.")) { |
| 10 | + if (part.isEmpty()) { |
| 11 | + throw new IllegalArgumentException("path matcher part can not be empty"); |
| 12 | + } |
11 | 13 | if (part.endsWith("]")) { |
12 | 14 | String index = part.substring(part.lastIndexOf("[") + 1, part.length() - 1); |
13 | | - matcher = matcher.add(new PathMatcherItem.ObjectProperty(part.substring(0, part.lastIndexOf("[")))); |
14 | | - if ("*".equals(index)) { |
15 | | - matcher = matcher.add(new PathMatcherItem.WilcardMatcherItem(Path.PathItem.ArrayIndex.class)); |
16 | | - } else { |
17 | | - matcher = matcher.add(new PathMatcherItem.ArrayIndex(Integer.parseInt(index))); |
18 | | - } |
19 | | - } else if ("*".equals(part)) { |
20 | | - matcher = matcher.add(new PathMatcherItem.WilcardMatcherItem(Path.PathItem.ObjectProperty.class)); |
| 15 | + matcher = matcher.add(PathMatcherItem.ofProperty(part.substring(0, part.lastIndexOf("[")))); |
| 16 | + matcher = matcher.add(PathMatcherItem.ofArrayIndex(index)); |
21 | 17 | } else { |
22 | | - matcher = matcher.add(new PathMatcher.PathMatcherItem.ObjectProperty(part)); |
| 18 | + matcher = matcher.add(PathMatcherItem.ofProperty(part)); |
23 | 19 | } |
24 | 20 | } |
25 | 21 | return matcher; |
26 | 22 | } |
27 | 23 |
|
28 | | - public PathMatcher() { |
| 24 | + private PathMatcher() { |
29 | 25 | this(null, null); |
30 | 26 | } |
31 | 27 |
|
32 | | - private PathMatcher(PathMatcher.PathMatcherItem property, PathMatcher next) { |
33 | | - this.property = property; |
34 | | - this.next = next; |
| 28 | + private PathMatcher(PathMatcherItem last, PathMatcher previous) { |
| 29 | + this.last = last; |
| 30 | + this.previous = previous; |
35 | 31 | } |
36 | 32 |
|
37 | | - private PathMatcher(PathMatcher.PathMatcherItem property) { |
38 | | - this.property = property; |
39 | | - this.next = null; |
40 | | - } |
41 | | - |
42 | | - public PathMatcher add(PathMatcher.PathMatcherItem item) { |
43 | | - if (this.next == null) { |
44 | | - return new PathMatcher(this.property, new PathMatcher(item)); |
45 | | - } else { |
46 | | - return new PathMatcher(this.property, this.next.add(item)); |
| 33 | + private PathMatcher add(PathMatcherItem item) { |
| 34 | + if (this.last == null) { |
| 35 | + return new PathMatcher(item, null); |
47 | 36 | } |
| 37 | + return new PathMatcher(item, this); |
48 | 38 | } |
49 | 39 |
|
50 | 40 | public boolean match(Path path) { |
51 | | - int pathLength = length(path); |
52 | | - int matcherLength = length(); |
| 41 | + if (this.last == null) { |
| 42 | + return true; |
| 43 | + } |
53 | 44 |
|
54 | | - if (matcherLength > pathLength) { |
| 45 | + if (path == null || path.item() == null) { |
55 | 46 | return false; |
56 | 47 | } |
57 | 48 |
|
58 | | - // Align path to match from the end |
59 | | - Path alignedPath = path; |
60 | | - for (int i = 0; i < pathLength - matcherLength; i++) { |
61 | | - alignedPath = alignedPath.next; |
| 49 | + if (!this.last.match(path.item())) { |
| 50 | + return false; |
62 | 51 | } |
63 | 52 |
|
64 | | - return matchFromHere(alignedPath); |
65 | | - } |
66 | | - |
67 | | - private int length() { |
68 | | - int count = 0; |
69 | | - PathMatcher current = this; |
70 | | - while (current != null && current.property != null) { |
71 | | - count++; |
72 | | - current = current.next; |
| 53 | + if (this.previous == null) { |
| 54 | + return true; |
73 | 55 | } |
74 | | - return count; |
75 | | - } |
76 | 56 |
|
77 | | - private int length(Path path) { |
78 | | - int count = 0; |
79 | | - Path current = path; |
80 | | - while (current != null && current.property != null) { |
81 | | - count++; |
82 | | - current = current.next; |
83 | | - } |
84 | | - return count; |
| 57 | + return this.previous.match(path.previous()); |
85 | 58 | } |
86 | 59 |
|
87 | | - private boolean matchFromHere(Path path) { |
88 | | - if (this.property == null) { |
89 | | - return next == null || next.matchFromHere(path); |
90 | | - } |
91 | | - if (path == null || path.property == null) { |
92 | | - return false; |
93 | | - } |
94 | | - if (this.property.match(path.property)) { |
95 | | - if (next == null) { |
96 | | - return path.next == null || path.next.property == null; |
97 | | - } |
98 | | - return path.next != null && next.matchFromHere(path.next); |
99 | | - } |
100 | | - return false; |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + StringBuilder sb = new StringBuilder("$"); |
| 63 | + appendReversed(sb); |
| 64 | + return sb.toString(); |
101 | 65 | } |
102 | 66 |
|
103 | | - public String toString() { |
104 | | - return ((this.property == null) ? "$" : this.property) + |
105 | | - ((this.next == null) ? "" : "." + this.next); |
| 67 | + private void appendReversed(StringBuilder sb) { |
| 68 | + if (last == null) return; |
| 69 | + if (previous != null) { |
| 70 | + previous.appendReversed(sb); |
| 71 | + } |
| 72 | + sb.append(".").append(last); |
106 | 73 | } |
107 | 74 |
|
108 | | - private interface PathMatcherItem { |
109 | | - static PathMatcherItem of(String property) { |
110 | | - return new PathMatcher.PathMatcherItem.ObjectProperty(property); |
| 75 | + public interface PathMatcherItem { |
| 76 | + static PathMatcherItem ofProperty(String property) { |
| 77 | + if ("*".equals(property)) { |
| 78 | + return new WildcardMatcherItem(Path.PathItem.ObjectProperty.class); |
| 79 | + } |
| 80 | + return new ObjectProperty(property); |
111 | 81 | } |
112 | 82 |
|
113 | | - static PathMatcherItem of(Integer index) { |
114 | | - return new PathMatcher.PathMatcherItem.ArrayIndex(index); |
| 83 | + static PathMatcherItem ofArrayIndex(String index) { |
| 84 | + if ("*".equals(index)) { |
| 85 | + return new WildcardMatcherItem(Path.PathItem.ArrayIndex.class); |
| 86 | + } |
| 87 | + return new ArrayIndex(Integer.parseInt(index)); |
115 | 88 | } |
116 | 89 |
|
117 | 90 | boolean match(Path.PathItem pathItem); |
118 | 91 |
|
119 | | - class WilcardMatcherItem implements PathMatcherItem { |
| 92 | + class WildcardMatcherItem implements PathMatcherItem { |
120 | 93 | private final Class<? extends Path.PathItem> type; |
121 | 94 |
|
122 | | - public WilcardMatcherItem(Class<? extends Path.PathItem> type) { |
| 95 | + public WildcardMatcherItem(Class<? extends Path.PathItem> type) { |
123 | 96 | this.type = type; |
124 | 97 | } |
125 | 98 |
|
| 99 | + @Override |
126 | 100 | public String toString() { |
127 | 101 | return "*"; |
128 | 102 | } |
|
0 commit comments