-
Notifications
You must be signed in to change notification settings - Fork 659
/
PropertyPath.java
380 lines (311 loc) · 9.8 KB
/
PropertyPath.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mapping;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Abstraction of a {@link PropertyPath} of a domain class.
*
* @author Oliver Gierke
*/
public class PropertyPath implements Iterable<PropertyPath> {
private static final String DELIMITERS = "_\\.";
private static final String ALL_UPPERCASE = "[A-Z0-9._$]+";
private static final Pattern SPLITTER = Pattern.compile("(?:[%s]?([%s]*?[^%s]+))".replaceAll("%s", DELIMITERS));
private final TypeInformation<?> owningType;
private final String name;
private final TypeInformation<?> type;
private final boolean isCollection;
private PropertyPath next;
/**
* Creates a leaf {@link PropertyPath} (no nested ones) with the given name inside the given owning type.
*
* @param name must not be {@literal null} or empty.
* @param owningType must not be {@literal null}.
*/
PropertyPath(String name, Class<?> owningType) {
this(name, ClassTypeInformation.from(owningType), Collections.<PropertyPath> emptyList());
}
/**
* Creates a leaf {@link PropertyPath} (no nested ones with the given name and owning type.
*
* @param name must not be {@literal null} or empty.
* @param owningType must not be {@literal null}.
* @param base the {@link PropertyPath} previously found.
*/
PropertyPath(String name, TypeInformation<?> owningType, List<PropertyPath> base) {
Assert.hasText(name, "Name must not be null or empty!");
Assert.notNull(owningType, "Owning type must not be null!");
Assert.notNull(base, "Perviously found properties must not be null!");
String propertyName = name.matches(ALL_UPPERCASE) ? name : StringUtils.uncapitalize(name);
TypeInformation<?> propertyType = owningType.getProperty(propertyName);
if (propertyType == null) {
throw new PropertyReferenceException(propertyName, owningType, base);
}
this.owningType = owningType;
this.isCollection = propertyType.isCollectionLike();
this.type = propertyType.getActualType();
this.name = propertyName;
}
/**
* Returns the owning type of the {@link PropertyPath}.
*
* @return the owningType will never be {@literal null}.
*/
public TypeInformation<?> getOwningType() {
return owningType;
}
/**
* Returns the name of the {@link PropertyPath}.
*
* @return the name will never be {@literal null}.
*/
public String getSegment() {
return name;
}
/**
* Returns the leaf property of the {@link PropertyPath}.
*
* @return will never be {@literal null}.
*/
public PropertyPath getLeafProperty() {
PropertyPath result = this;
while (result.hasNext()) {
result = result.next();
}
return result;
}
/**
* Returns the type of the property will return the plain resolved type for simple properties, the component type for
* any {@link Iterable} or the value type of a {@link java.util.Map} if the property is one.
*
* @return
*/
public Class<?> getType() {
return this.type.getType();
}
/**
* Returns the next nested {@link PropertyPath}.
*
* @return the next nested {@link PropertyPath} or {@literal null} if no nested {@link PropertyPath} available.
* @see #hasNext()
*/
public PropertyPath next() {
return next;
}
/**
* Returns whether there is a nested {@link PropertyPath}. If this returns {@literal true} you can expect
* {@link #next()} to return a non- {@literal null} value.
*
* @return
*/
public boolean hasNext() {
return next != null;
}
/**
* Returns the {@link PropertyPath} in dot notation.
*
* @return
*/
public String toDotPath() {
if (hasNext()) {
return getSegment() + "." + next().toDotPath();
}
return getSegment();
}
/**
* Returns whether the {@link PropertyPath} is actually a collection.
*
* @return
*/
public boolean isCollection() {
return isCollection;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !getClass().equals(obj.getClass())) {
return false;
}
PropertyPath that = (PropertyPath) obj;
return this.name.equals(that.name) && this.type.equals(that.type)
&& ObjectUtils.nullSafeEquals(this.next, that.next);
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int result = 17;
result += 31 * name.hashCode();
result += 31 * type.hashCode();
result += 31 * (next == null ? 0 : next.hashCode());
return result;
}
/*
* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
public Iterator<PropertyPath> iterator() {
return new Iterator<PropertyPath>() {
private PropertyPath current = PropertyPath.this;
public boolean hasNext() {
return current != null;
}
public PropertyPath next() {
PropertyPath result = current;
this.current = current.next();
return result;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
/**
* Extracts the {@link PropertyPath} chain from the given source {@link String} and type.
*
* @param source
* @param type
* @return
*/
public static PropertyPath from(String source, Class<?> type) {
return from(source, ClassTypeInformation.from(type));
}
/**
* Extracts the {@link PropertyPath} chain from the given source {@link String} and {@link TypeInformation}.
*
* @param source must not be {@literal null}.
* @param type
* @return
*/
public static PropertyPath from(String source, TypeInformation<?> type) {
Assert.hasText(source, "Source must not be null or empty!");
Assert.notNull(type, "TypeInformation must not be null or empty!");
List<String> iteratorSource = new ArrayList<String>();
Matcher matcher = SPLITTER.matcher("_" + source);
while (matcher.find()) {
iteratorSource.add(matcher.group(1));
}
Iterator<String> parts = iteratorSource.iterator();
PropertyPath result = null;
Stack<PropertyPath> current = new Stack<PropertyPath>();
while (parts.hasNext()) {
if (result == null) {
result = create(parts.next(), type, current);
current.push(result);
} else {
current.push(create(parts.next(), current));
}
}
return result;
}
/**
* Creates a new {@link PropertyPath} as subordinary of the given {@link PropertyPath}.
*
* @param source
* @param base
* @return
*/
private static PropertyPath create(String source, Stack<PropertyPath> base) {
PropertyPath previous = base.peek();
PropertyPath propertyPath = create(source, previous.type, base);
previous.next = propertyPath;
return propertyPath;
}
/**
* Factory method to create a new {@link PropertyPath} for the given {@link String} and owning type. It will inspect
* the given source for camel-case parts and traverse the {@link String} along its parts starting with the entire one
* and chewing off parts from the right side then. Whenever a valid property for the given class is found, the tail
* will be traversed for subordinary properties of the just found one and so on.
*
* @param source
* @param type
* @return
*/
private static PropertyPath create(String source, TypeInformation<?> type, List<PropertyPath> base) {
return create(source, type, "", base);
}
/**
* Tries to look up a chain of {@link PropertyPath}s by trying the givne source first. If that fails it will split the
* source apart at camel case borders (starting from the right side) and try to look up a {@link PropertyPath} from
* the calculated head and recombined new tail and additional tail.
*
* @param source
* @param type
* @param addTail
* @return
*/
private static PropertyPath create(String source, TypeInformation<?> type, String addTail, List<PropertyPath> base) {
PropertyReferenceException exception = null;
PropertyPath current = null;
try {
current = new PropertyPath(source, type, base);
if (!base.isEmpty()) {
base.get(base.size() - 1).next = current;
}
List<PropertyPath> newBase = new ArrayList<PropertyPath>(base);
newBase.add(current);
if (StringUtils.hasText(addTail)) {
current.next = create(addTail, current.type, newBase);
}
return current;
} catch (PropertyReferenceException e) {
if (current != null) {
throw e;
}
exception = e;
}
Pattern pattern = Pattern.compile("\\p{Lu}+\\p{Ll}*$");
Matcher matcher = pattern.matcher(source);
if (matcher.find() && matcher.start() != 0) {
int position = matcher.start();
String head = source.substring(0, position);
String tail = source.substring(position);
try {
return create(head, type, tail + addTail, base);
} catch (PropertyReferenceException e) {
throw e.hasDeeperResolutionDepthThan(exception) ? e : exception;
}
}
throw exception;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("%s.%s", owningType.getType().getSimpleName(), toDotPath());
}
}