Skip to content

Commit 73e5033

Browse files
committed
Polishing.
Eliminate potential NoSuchElementException from unchecked Optional.get usage. Simplify stream. Return Staged value, fix Nullability annotations. See #1907 Original pull request: #1920
1 parent a7d7ada commit 73e5033

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateChangeExecutionContext.java

+19-16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Umut Erturk
5050
* @author Myeonghyeon Lee
5151
* @author Chirag Tailor
52+
* @author Mark Paluch
5253
*/
5354
@SuppressWarnings("rawtypes")
5455
class JdbcAggregateChangeExecutionContext {
@@ -268,12 +269,10 @@ <T> List<T> populateIdsIfNecessary() {
268269

269270
cascadingValues.stage(insert.getDependingOn(), insert.getPropertyPath(),
270271
qualifierValue, newEntity);
271-
272272
} else if (insert.getPropertyPath().getLeafProperty().isCollectionLike()) {
273273

274274
cascadingValues.gather(insert.getDependingOn(), insert.getPropertyPath(),
275275
qualifierValue, newEntity);
276-
277276
}
278277
}
279278
}
@@ -289,6 +288,7 @@ <T> List<T> populateIdsIfNecessary() {
289288
return roots;
290289
}
291290

291+
@SuppressWarnings("unchecked")
292292
private <S> Object setIdAndCascadingProperties(DbAction.WithEntity<S> action, @Nullable Object generatedId,
293293
StagedValues cascadingValues) {
294294

@@ -328,6 +328,7 @@ private PersistentPropertyPath<?> getRelativePath(DbAction<?> action, Persistent
328328
throw new IllegalArgumentException(String.format("DbAction of type %s is not supported", action.getClass()));
329329
}
330330

331+
@SuppressWarnings("unchecked")
331332
private <T> RelationalPersistentEntity<T> getRequiredPersistentEntity(Class<T> type) {
332333
return (RelationalPersistentEntity<T>) context.getRequiredPersistentEntity(type);
333334
}
@@ -358,7 +359,7 @@ private <T> void updateWithVersion(DbAction.UpdateRoot<T> update) {
358359
*/
359360
private static class StagedValues {
360361

361-
static final List<MultiValueAggregator> aggregators = Arrays.asList(SetAggregator.INSTANCE, MapAggregator.INSTANCE,
362+
static final List<MultiValueAggregator<?>> aggregators = Arrays.asList(SetAggregator.INSTANCE, MapAggregator.INSTANCE,
362363
ListAggregator.INSTANCE, SingleElementAggregator.INSTANCE);
363364

364365
Map<DbAction, Map<PersistentPropertyPath, StagedValue>> values = new HashMap<>();
@@ -374,13 +375,14 @@ private static class StagedValues {
374375
* be {@literal null}.
375376
* @param value The value to be set. Must not be {@literal null}.
376377
*/
377-
@SuppressWarnings("unchecked")
378-
<T> void stage(DbAction<?> action, PersistentPropertyPath path, @Nullable Object qualifier, Object value) {
379-
gather(action, path, qualifier, value);
380-
values.get(action).get(path).isStaged = true;
378+
void stage(DbAction<?> action, PersistentPropertyPath path, @Nullable Object qualifier, Object value) {
379+
380+
StagedValue gather = gather(action, path, qualifier, value);
381+
gather.isStaged = true;
381382
}
382383

383-
<T> void gather(DbAction<?> action, PersistentPropertyPath path, @Nullable Object qualifier, Object value) {
384+
@SuppressWarnings("unchecked")
385+
<T> StagedValue gather(DbAction<?> action, PersistentPropertyPath path, @Nullable Object qualifier, Object value) {
384386

385387
MultiValueAggregator<T> aggregator = getAggregatorFor(path);
386388

@@ -391,19 +393,20 @@ <T> void gather(DbAction<?> action, PersistentPropertyPath path, @Nullable Objec
391393
persistentPropertyPath -> new StagedValue(aggregator.createEmptyInstance()));
392394
T currentValue = (T) stagedValue.value;
393395

394-
Object newValue = aggregator.add(currentValue, qualifier, value);
395-
396-
stagedValue.value = newValue;
396+
stagedValue.value = aggregator.add(currentValue, qualifier, value);
397397

398398
valuesForPath.put(path, stagedValue);
399+
400+
return stagedValue;
399401
}
400402

401-
private MultiValueAggregator getAggregatorFor(PersistentPropertyPath path) {
403+
@SuppressWarnings("unchecked")
404+
private <T> MultiValueAggregator<T> getAggregatorFor(PersistentPropertyPath path) {
402405

403406
PersistentProperty property = path.getLeafProperty();
404-
for (MultiValueAggregator aggregator : aggregators) {
407+
for (MultiValueAggregator<?> aggregator : aggregators) {
405408
if (aggregator.handles(property)) {
406-
return aggregator;
409+
return (MultiValueAggregator<T>) aggregator;
407410
}
408411
}
409412

@@ -427,10 +430,10 @@ void forEachPath(DbAction<?> dbAction, BiConsumer<PersistentPropertyPath, Object
427430
}
428431

429432
private static class StagedValue {
430-
Object value;
433+
@Nullable Object value;
431434
boolean isStaged;
432435

433-
public StagedValue(Object value) {
436+
public StagedValue(@Nullable Object value) {
434437
this.value = value;
435438
}
436439
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/DbAction.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -482,16 +482,21 @@ interface WithDependingOn<T> extends WithPropertyPath<T>, WithEntity<T> {
482482
default Pair<PersistentPropertyPath<RelationalPersistentProperty>, Object> getQualifier() {
483483

484484
Map<PersistentPropertyPath<RelationalPersistentProperty>, Object> qualifiers = getQualifiers();
485+
485486
if (qualifiers.isEmpty()) {
486487
return null;
487488
}
488489

489490
Set<Map.Entry<PersistentPropertyPath<RelationalPersistentProperty>, Object>> entries = qualifiers.entrySet();
490-
Map.Entry<PersistentPropertyPath<RelationalPersistentProperty>, Object> entry = entries.stream().sorted(Comparator.comparing(e -> -e.getKey().getLength())).findFirst().get();
491+
Optional<Map.Entry<PersistentPropertyPath<RelationalPersistentProperty>, Object>> optionalEntry = entries.stream()
492+
.filter(e -> e.getValue() != null).min(Comparator.comparing(e -> -e.getKey().getLength()));
493+
494+
Map.Entry<PersistentPropertyPath<RelationalPersistentProperty>, Object> entry = optionalEntry.orElse(null);
491495

492-
if (entry.getValue() == null) {
496+
if (entry == null) {
493497
return null;
494498
}
499+
495500
return Pair.of(entry.getKey(), entry.getValue());
496501
}
497502

0 commit comments

Comments
 (0)