Skip to content

Commit 6b58051

Browse files
committed
Polish Binder code
1 parent 4fb68d4 commit 6b58051

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Binder.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import java.util.HashSet;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Objects;
2829
import java.util.Set;
2930
import java.util.function.Consumer;
31+
import java.util.function.Function;
3032
import java.util.function.Supplier;
3133

3234
import org.springframework.beans.PropertyEditorRegistry;
@@ -360,7 +362,8 @@ private <T> T handleBindResult(ConfigurationPropertyName name, Bindable<T> targe
360362
result = context.getConverter().convert(result, target);
361363
}
362364
if (result == null && create) {
363-
result = create(target, context);
365+
result = fromDataObjectBinders(target.getBindMethod(),
366+
(dataObjectBinder) -> dataObjectBinder.create(target, context));
364367
result = handler.onCreate(name, target, context, result);
365368
result = context.getConverter().convert(result, target);
366369
Assert.state(result != null, () -> "Unable to create instance for " + target.getType());
@@ -369,16 +372,6 @@ private <T> T handleBindResult(ConfigurationPropertyName name, Bindable<T> targe
369372
return context.getConverter().convert(result, target);
370373
}
371374

372-
private Object create(Bindable<?> target, Context context) {
373-
for (DataObjectBinder dataObjectBinder : this.dataObjectBinders.get(target.getBindMethod())) {
374-
Object instance = dataObjectBinder.create(target, context);
375-
if (instance != null) {
376-
return instance;
377-
}
378-
}
379-
return null;
380-
}
381-
382375
private <T> T handleBindError(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler,
383376
Context context, Exception error) {
384377
try {
@@ -477,15 +470,17 @@ private Object bindDataObject(ConfigurationPropertyName name, Bindable<?> target
477470
}
478471
DataObjectPropertyBinder propertyBinder = (propertyName, propertyTarget) -> bind(name.append(propertyName),
479472
propertyTarget, handler, context, false, false);
480-
return context.withDataObject(type, () -> {
481-
for (DataObjectBinder dataObjectBinder : this.dataObjectBinders.get(bindMethod)) {
482-
Object instance = dataObjectBinder.bind(name, target, context, propertyBinder);
483-
if (instance != null) {
484-
return instance;
485-
}
486-
}
487-
return null;
488-
});
473+
return context.withDataObject(type, () -> fromDataObjectBinders(bindMethod,
474+
(dataObjectBinder) -> dataObjectBinder.bind(name, target, context, propertyBinder)));
475+
}
476+
477+
private Object fromDataObjectBinders(BindMethod bindMethod, Function<DataObjectBinder, Object> operation) {
478+
return this.dataObjectBinders.get(bindMethod)
479+
.stream()
480+
.map(operation)
481+
.filter(Objects::nonNull)
482+
.findFirst()
483+
.orElse(null);
489484
}
490485

491486
private boolean isUnbindableBean(ConfigurationPropertyName name, Bindable<?> target, Context context) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/DataObjectBinder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ interface DataObjectBinder {
3333
/**
3434
* Return a bound instance or {@code null} if the {@link DataObjectBinder} does not
3535
* support the specified {@link Bindable}.
36+
* @param <T> the source type
3637
* @param name the name being bound
3738
* @param target the bindable to bind
3839
* @param context the bind context
3940
* @param propertyBinder property binder
40-
* @param <T> the source type
4141
* @return a bound instance or {@code null}
4242
*/
4343
<T> T bind(ConfigurationPropertyName name, Bindable<T> target, Context context,
@@ -46,9 +46,9 @@ <T> T bind(ConfigurationPropertyName name, Bindable<T> target, Context context,
4646
/**
4747
* Return a newly created instance or {@code null} if the {@link DataObjectBinder}
4848
* does not support the specified {@link Bindable}.
49+
* @param <T> the source type
4950
* @param target the bindable to create
5051
* @param context the bind context
51-
* @param <T> the source type
5252
* @return the created instance
5353
*/
5454
<T> T create(Bindable<T> target, Context context);

0 commit comments

Comments
 (0)