Skip to content

Commit 2d78148

Browse files
committed
add a new method PropertyMapper.from(value).
polish
1 parent 0f0fc9e commit 2d78148

File tree

7 files changed

+52
-13
lines changed

7 files changed

+52
-13
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected JobLauncher createJobLauncher() throws Exception {
122122
protected JobRepository createJobRepository() throws Exception {
123123
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
124124
PropertyMapper map = PropertyMapper.get();
125-
map.from(() -> this.dataSource).to(factory::setDataSource);
125+
map.from(this.dataSource).to(factory::setDataSource);
126126
map.from(this::determineIsolationLevel).whenNonNull()
127127
.to(factory::setIsolationLevelForCreate);
128128
map.from(this.properties::getTablePrefix).whenHasText()

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/ConcurrentKafkaListenerContainerFactoryConfigurer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ private void configureListenerFactory(
8585
PropertyMapper map = PropertyMapper.get();
8686
Listener properties = this.properties.getListener();
8787
map.from(properties::getConcurrency).whenNonNull().to(factory::setConcurrency);
88-
map.from(() -> this.messageConverter).whenNonNull()
89-
.to(factory::setMessageConverter);
90-
map.from(() -> this.replyTemplate).whenNonNull().to(factory::setReplyTemplate);
88+
map.from(this.messageConverter).whenNonNull().to(factory::setMessageConverter);
89+
map.from(this.replyTemplate).whenNonNull().to(factory::setReplyTemplate);
9190
map.from(properties::getType).whenEqualTo(Listener.Type.BATCH)
9291
.toCall(() -> factory.setBatchListener(true));
9392
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
8282
tomcatProperties.getMaxThreads()));
8383
propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive)
8484
.to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads));
85-
propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive)
85+
propertyMapper.from(this::determineMaxHttpHeaderSize).when(this::isPositive)
8686
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
8787
maxHttpHeaderSize));
8888
propertyMapper.from(tomcatProperties::getMaxHttpPostSize)

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void customize(ConfigurableUndertowWebServerFactory factory) {
8181
.to(factory::setAccessLogSuffix);
8282
propertyMapper.from(accesslogProperties::isRotate)
8383
.to(factory::setAccessLogRotate);
84-
propertyMapper.from(() -> getOrDeduceUseForwardHeaders())
84+
propertyMapper.from(this::getOrDeduceUseForwardHeaders)
8585
.to(factory::setUseForwardHeaders);
8686
propertyMapper.from(properties::getMaxHttpHeaderSize).when(this::isPositive)
8787
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ public PropertyMapper alwaysApplying(SourceOperator operator) {
9191
return new PropertyMapper(this, operator);
9292
}
9393

94+
/**
95+
* Return a new {@link Source} from the specified value that can be used to perform
96+
* the mapping.
97+
* @param <T> the source type
98+
* @param value the value
99+
* @return a {@link Source} that can be used to complete the mapping
100+
*/
101+
public <T> Source<T> from(T value) {
102+
Source<T> source = getSource(() -> value);
103+
if (this.sourceOperator != null) {
104+
source = this.sourceOperator.apply(source);
105+
}
106+
return source;
107+
}
108+
94109
/**
95110
* Return a new {@link Source} from the specified value supplier that can be used to
96111
* perform the mapping.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,12 @@ public <T extends WebServiceTemplate> T configure(T webServiceTemplate) {
502502
configureMessageSenders(webServiceTemplate);
503503
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
504504
applyCustomizers(webServiceTemplate, this.internalCustomizers);
505-
map.from(() -> this.marshaller).to(webServiceTemplate::setMarshaller);
506-
map.from(() -> this.unmarshaller).to(webServiceTemplate::setUnmarshaller);
507-
map.from(() -> this.destinationProvider)
508-
.to(webServiceTemplate::setDestinationProvider);
509-
map.from(() -> this.transformerFactoryClass)
505+
map.from(this.marshaller).to(webServiceTemplate::setMarshaller);
506+
map.from(this.unmarshaller).to(webServiceTemplate::setUnmarshaller);
507+
map.from(this.destinationProvider).to(webServiceTemplate::setDestinationProvider);
508+
map.from(this.transformerFactoryClass)
510509
.to(webServiceTemplate::setTransformerFactoryClass);
511-
map.from(() -> this.messageFactory).to(webServiceTemplate::setMessageFactory);
510+
map.from(this.messageFactory).to(webServiceTemplate::setMessageFactory);
512511
if (!CollectionUtils.isEmpty(this.interceptors)) {
513512
Set<ClientInterceptor> merged = new LinkedHashSet<>(this.interceptors);
514513
if (webServiceTemplate.getInterceptors() != null) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,37 @@ public class PropertyMapperTests {
3737
@Rule
3838
public ExpectedException thrown = ExpectedException.none();
3939

40+
@Test
41+
public void fromNullValue() {
42+
ExampleDest dest = new ExampleDest();
43+
this.map.from((String) null).to(dest::setName);
44+
assertThat(dest.getName()).isNull();
45+
}
46+
47+
@Test
48+
public void fromValue() {
49+
ExampleDest dest = new ExampleDest();
50+
this.map.from("Hello World").to(dest::setName);
51+
assertThat(dest.getName()).isEqualTo("Hello World");
52+
}
53+
54+
@Test
55+
public void fromValueAsIntShouldAdaptSupplier() {
56+
Integer result = this.map.from("123").asInt(Long::valueOf)
57+
.toInstance(Integer::new);
58+
assertThat(result).isEqualTo(123);
59+
}
60+
61+
@Test
62+
public void fromValueAlwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
63+
this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(Assert::fail);
64+
}
65+
4066
@Test
4167
public void fromWhenSupplierIsNullShouldThrowException() {
4268
this.thrown.expect(IllegalArgumentException.class);
4369
this.thrown.expectMessage("Supplier must not be null");
44-
this.map.from(null);
70+
this.map.from((Supplier<?>) null);
4571
}
4672

4773
@Test

0 commit comments

Comments
 (0)