Skip to content

Commit f58ce36

Browse files
committed
Polishing.
Introduce dedicated methods to express object creation intents. Remove unnecessary casts. See #2837 Original pull request: #2838
1 parent 4d038ff commit f58ce36

File tree

1 file changed

+43
-64
lines changed

1 file changed

+43
-64
lines changed

src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java

+43-64
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@
1515
*/
1616
package org.springframework.data.mapping.context;
1717

18-
import java.util.*;
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.Comparator;
22+
import java.util.HashSet;
23+
import java.util.Iterator;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Optional;
27+
import java.util.Set;
1928
import java.util.function.Function;
2029
import java.util.function.Predicate;
2130
import java.util.stream.Collectors;
@@ -34,14 +43,14 @@
3443
import org.springframework.lang.Nullable;
3544
import org.springframework.util.Assert;
3645
import org.springframework.util.ConcurrentReferenceHashMap;
37-
import org.springframework.util.ObjectUtils;
3846
import org.springframework.util.StringUtils;
3947

4048
/**
4149
* A factory implementation to create {@link PersistentPropertyPath} instances in various ways.
4250
*
4351
* @author Oliver Gierke
4452
* @author Christoph Strobl
53+
* @author Mark Paluch
4554
* @since 2.1
4655
* @soundtrack Cypress Hill - Boom Biddy Bye Bye (Fugees Remix, Unreleased & Revamped)
4756
*/
@@ -172,7 +181,7 @@ private PersistentPropertyPath<P> getPersistentPropertyPath(TypeInformation<?> t
172181

173182
private PathResolution getPotentiallyCachedPath(TypeInformation<?> type, String propertyPath) {
174183
return propertyPaths.computeIfAbsent(TypeAndPath.of(type, propertyPath),
175-
it -> createPersistentPropertyPath(it.getPath(), it.getType()));
184+
it -> createPersistentPropertyPath(it.path(), it.type()));
176185
}
177186

178187
/**
@@ -200,14 +209,14 @@ private PathResolution createPersistentPropertyPath(String propertyPath, TypeInf
200209
Pair<DefaultPersistentPropertyPath<P>, E> pair = getPair(path, iterator, segment, current);
201210

202211
if (pair == null) {
203-
return new PathResolution(parts, segment, type, currentPath);
212+
return PathResolution.unresolved(parts, segment, type, currentPath);
204213
}
205214

206215
path = pair.getFirst();
207216
current = pair.getSecond();
208217
}
209218

210-
return new PathResolution(path);
219+
return PathResolution.resolved(path);
211220
}
212221

213222
@Nullable
@@ -240,7 +249,7 @@ private Collection<PersistentPropertyPath<P>> from(E entity, Predicate<? super P
240249

241250
Set<PersistentPropertyPath<P>> properties = new HashSet<>();
242251

243-
PropertyHandler<P> propertyTester = (PropertyHandler<P>) persistentProperty -> {
252+
PropertyHandler<P> propertyTester = persistentProperty -> {
244253

245254
TypeInformation<?> typeInformation = persistentProperty.getTypeInformation();
246255
TypeInformation<?> actualPropertyType = typeInformation.getActualType();
@@ -263,64 +272,19 @@ private Collection<PersistentPropertyPath<P>> from(E entity, Predicate<? super P
263272

264273
entity.doWithProperties(propertyTester);
265274

266-
AssociationHandler<P> handler = (AssociationHandler<P>) association -> propertyTester
275+
AssociationHandler<P> handler = association -> propertyTester
267276
.doWithPersistentProperty(association.getInverse());
268277
entity.doWithAssociations(handler);
269278

270279
return properties;
271280
}
272281

273-
static final class TypeAndPath {
274-
275-
private final TypeInformation<?> type;
276-
private final String path;
277-
278-
private TypeAndPath(TypeInformation<?> type, String path) {
279-
this.type = type;
280-
this.path = path;
281-
}
282+
record TypeAndPath(TypeInformation<?> type, String path) {
282283

283284
public static TypeAndPath of(TypeInformation<?> type, String path) {
284285
return new TypeAndPath(type, path);
285286
}
286287

287-
public TypeInformation<?> getType() {
288-
return this.type;
289-
}
290-
291-
public String getPath() {
292-
return this.path;
293-
}
294-
295-
@Override
296-
public boolean equals(@Nullable Object o) {
297-
298-
if (this == o) {
299-
return true;
300-
}
301-
302-
if (!(o instanceof TypeAndPath that)) {
303-
return false;
304-
}
305-
306-
if (!ObjectUtils.nullSafeEquals(type, that.type)) {
307-
return false;
308-
}
309-
310-
return ObjectUtils.nullSafeEquals(path, that.path);
311-
}
312-
313-
@Override
314-
public int hashCode() {
315-
int result = ObjectUtils.nullSafeHashCode(type);
316-
result = 31 * result + ObjectUtils.nullSafeHashCode(path);
317-
return result;
318-
}
319-
320-
@Override
321-
public String toString() {
322-
return "PersistentPropertyPathFactory.TypeAndPath(type=" + this.getType() + ", path=" + this.getPath() + ")";
323-
}
324288
}
325289

326290
static class DefaultPersistentPropertyPaths<T, P extends PersistentProperty<P>>
@@ -389,7 +353,7 @@ public PersistentPropertyPaths<T, P> dropPathIfSegmentMatches(Predicate<? super
389353
Assert.notNull(predicate, "Predicate must not be null");
390354

391355
List<PersistentPropertyPath<P>> paths = this.stream() //
392-
.filter(it -> !it.stream().anyMatch(predicate)) //
356+
.filter(it -> it.stream().noneMatch(predicate)) //
393357
.collect(Collectors.toList());
394358

395359
return paths.equals(this.paths) ? this : new DefaultPersistentPropertyPaths<>(type, paths);
@@ -408,17 +372,15 @@ public String toString() {
408372
* @author Oliver Gierke
409373
* @since 2.1
410374
*/
411-
private enum ShortestSegmentFirst
412-
implements Comparator<PersistentPropertyPath<? extends PersistentProperty<?>>> {
375+
private enum ShortestSegmentFirst implements Comparator<PersistentPropertyPath<? extends PersistentProperty<?>>> {
413376

414377
INSTANCE;
415378

416379
@Override
417380
@SuppressWarnings("null")
418381
public int compare(PersistentPropertyPath<?> left, PersistentPropertyPath<?> right) {
419382

420-
Function<PersistentProperty<?>, Integer> mapper = (Function<PersistentProperty<?>, Integer>) it -> it.getName()
421-
.length();
383+
Function<PersistentProperty<?>, Integer> mapper = it -> it.getName().length();
422384

423385
Stream<Integer> leftNames = left.stream().map(mapper);
424386
Stream<Integer> rightNames = right.stream().map(mapper);
@@ -440,16 +402,22 @@ private static class PathResolution {
440402

441403
private final PersistentPropertyPath<?> path;
442404
private final boolean resolvable;
443-
private String source, segment;
444-
private TypeInformation<?> type;
405+
private @Nullable final String source;
445406

446-
public PathResolution(PersistentPropertyPath<?> path) {
407+
private @Nullable final String segment;
408+
private @Nullable final TypeInformation<?> type;
409+
410+
private PathResolution(PersistentPropertyPath<?> path) {
447411

448412
this.path = path;
449413
this.resolvable = true;
414+
this.source = null;
415+
this.segment = null;
416+
this.type = null;
450417
}
451418

452-
PathResolution(List<String> parts, String segment, TypeInformation<?> type, PersistentPropertyPath<?> path) {
419+
private PathResolution(List<String> parts, String segment, TypeInformation<?> type,
420+
PersistentPropertyPath<?> path) {
453421

454422
this.source = StringUtils.collectionToDelimitedString(parts, ".");
455423
this.segment = segment;
@@ -458,15 +426,26 @@ public PathResolution(PersistentPropertyPath<?> path) {
458426
this.resolvable = false;
459427
}
460428

429+
static PathResolution unresolved(List<String> parts, String segment, TypeInformation<?> type,
430+
PersistentPropertyPath<?> path) {
431+
return new PathResolution(parts, segment, type, path);
432+
}
433+
434+
static PathResolution resolved(PersistentPropertyPath<?> path) {
435+
return new PathResolution(path);
436+
}
437+
461438
/**
462439
* @return the path if available.
463440
* @throws InvalidPersistentPropertyPath when the path could not be resolved to an actual property
464441
*/
465-
PersistentPropertyPath getResolvedPath() {
442+
@SuppressWarnings("unchecked")
443+
<P extends PersistentProperty<P>> PersistentPropertyPath<P> getResolvedPath() {
466444

467445
if (resolvable) {
468-
return path;
446+
return (PersistentPropertyPath<P>) path;
469447
}
448+
470449
throw new InvalidPersistentPropertyPath(source, type, segment, path);
471450
}
472451
}

0 commit comments

Comments
 (0)