-
Notifications
You must be signed in to change notification settings - Fork 4
/
PropertySelector.java
223 lines (193 loc) · 6.59 KB
/
PropertySelector.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
package uk.nhs.ciao.docs.parser;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import uk.nhs.ciao.util.SimpleEntry;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.ObjectArrays;
/**
* Selects a property (or properties) from a dynamic map structure.
* <p>
* Supported syntax is:
* <ul>
* <li><code>.{string}</code> - matches key in map
* <li><code>[{integer}]</code> - matches index in list
* <li><code>.*</code> - matches any key in map
* <li><code>[*]</code> - matches any index in list
* </ul>
* <p>
* Some examples:
* <ul>
* <li><code>authors[1]</code>
* <li><code>author.firstName</code>
* <li><code>author.*</code>
* <li><code>authors[*]</code>
* <li><code>authors[*].*</code>
* </ul>
*/
public final class PropertySelector {
/**
* Selects the root of a properties map
*/
private static final PropertySelector ROOT = new PropertySelector(new Object[0]);
private final Object[] segments;
private final boolean multi;
private int hash;
/**
* Returns a selector matching the specified path
*/
public static PropertySelector valueOf(final String path) {
final boolean allowWildcards = true;
return Strings.isNullOrEmpty(path) ? ROOT : new PropertySelector(PropertyPath.parse(path, allowWildcards));
}
PropertySelector(final Object[] segments) {
this.segments = segments;
this.multi = PropertyPath.containsWildcard(segments);
}
/**
* Tests if this is the root selector
*/
public boolean isRoot() {
return segments.length == 0;
}
/**
* Tests if this selector <em>may</em> select multiple matches
* (i.e. contains a wildcard)
*/
public boolean isMulti() {
return multi;
}
/**
* Returns the PropertyName equivalent to this selector if a single name is identified or
* <code>null</code> if the path contains wildcards
*/
public PropertyName toPropertyName() {
return multi ? null : new PropertyName(Arrays.copyOf(segments, segments.length));
}
/**
* Returns the string path associated with this selector
*/
public String getPath() {
return PropertyPath.toString(segments);
}
/**
* Returns the parent of this selector if it is not a root
*/
public PropertySelector getParent() {
return isRoot() ? null : new PropertySelector(Arrays.copyOf(segments, segments.length - 1));
}
/**
* Returns the child selector associated with this selector and the specified
* appended path segments
*/
public PropertySelector getChild(final String childPath) {
final boolean allowWildcards = true;
final Object[] childSegments = PropertyPath.parse(childPath, allowWildcards);
Preconditions.checkArgument(childSegments.length > 0, "childPath must be provided");
return new PropertySelector(ObjectArrays.concat(segments, childSegments, Object.class));
}
/**
* Selects the first matching property key/value pairs from the dynamic map
* <p>
* The entry key is encoded as a matching path
*/
public Entry<String, Object> select(final Map<String, Object> properties) {
return select(Object.class, properties);
}
/**
* Selects the first matching property value from the dynamic map
*/
public Object selectValue(final Map<String, Object> properties) {
return selectValue(Object.class, properties);
}
/**
* Selects the first matching property key/value pairs from the dynamic map
* <p>
* The entry key is encoded as a matching path
*
* @param type The type of objects matched - any objects matching the path selector but of
* the wrong type are ignored
*/
public <T> Entry<String, T> select(final Class<T> type, final Map<String, Object> properties) {
final Entry<Object[], T> entry = PropertyPath.getEntry(type, properties, segments);
return entry == null ? null : SimpleEntry.valueOf(PropertyPath.toString(entry.getKey()), entry.getValue());
}
/**
* Selects the first matching property value from the dynamic map
*
* @param type The type of objects matched - any objects matching the path selector but of
* the wrong type are ignored
*/
public <T> T selectValue(final Class<T> type, final Map<String, Object> properties) {
return PropertyPath.getValue(type, properties, segments);
}
/**
* Selects all matching property key/value pairs from the dynamic map
* <p>
* The entry key is encoded as a matching path
*/
public Map<String, Object> selectAll(final Map<String, Object> properties) {
return selectAll(Object.class, properties);
}
/**
* Selects all matching property values from the dynamic map
*/
public List<Object> selectAllValues(final Map<String, Object> properties) {
return selectAllValues(Object.class, properties);
}
/**
* Selects all matching property key/value pairs from the dynamic map
* <p>
* The entry key is encoded as a matching path
*
* @param type The type of objects matched - any objects matching the path selector but of
* the wrong type are ignored
*/
public <T> Map<String, T> selectAll(final Class<T> type, final Map<String, Object> properties) {
final Map<String, T> results = Maps.newLinkedHashMap();
for (final Entry<Object[], T> entry: PropertyPath.findAll(type, properties, segments)) {
results.put(PropertyPath.toString(entry.getKey()), entry.getValue());
}
return results;
}
/**
* Selects all matching property values from the dynamic map
*
* @param type The type of objects matched - any objects matching the path selector but of
* the wrong type are ignored
*/
public <T> List<T> selectAllValues(final Class<T> type, final Map<String, Object> properties) {
final List<T> results = Lists.newArrayList();
for (final Entry<Object[], T> entry: PropertyPath.findAll(type, properties, segments)) {
results.add(entry.getValue());
}
return results;
}
@Override
public int hashCode() {
int result = hash;
if (result == 0) {
result = Arrays.hashCode(segments);
hash = result; // safe to publish without volatile
}
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
}
final PropertySelector other = (PropertySelector) obj;
return Arrays.equals(segments, other.segments);
}
@Override
public String toString() {
return Arrays.toString(segments);
}
}