Skip to content

Commit 893a052

Browse files
committed
add a new method PropertyMapper.from(value).
1 parent 3acbffb commit 893a052

File tree

10 files changed

+62
-27
lines changed

10 files changed

+62
-27
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
@@ -84,7 +84,7 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
8484
tomcatProperties.getMaxThreads()));
8585
propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive)
8686
.to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads));
87-
propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive)
87+
propertyMapper.from(this::determineMaxHttpHeaderSize).when(this::isPositive)
8888
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
8989
maxHttpHeaderSize));
9090
propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull()

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-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ public void setResolveLazily(boolean resolveLazily) {
138138
public MultipartConfigElement createMultipartConfig() {
139139
MultipartConfigFactory factory = new MultipartConfigFactory();
140140
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
141-
map.from(() -> this.fileSizeThreshold).to(factory::setFileSizeThreshold);
142-
map.from(() -> this.location).whenHasText().to(factory::setLocation);
143-
map.from(() -> this.maxRequestSize).to(factory::setMaxRequestSize);
144-
map.from(() -> this.maxFileSize).to(factory::setMaxFileSize);
141+
map.from(this.fileSizeThreshold).to(factory::setFileSizeThreshold);
142+
map.from(this.location).whenHasText().to(factory::setLocation);
143+
map.from(this.maxRequestSize).to(factory::setMaxRequestSize);
144+
map.from(this.maxFileSize).to(factory::setMaxFileSize);
145145
return factory.createMultipartConfig();
146146
}
147147

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ 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+
* @see #from(Supplier)
101+
*/
102+
public <T> Source<T> from(T value) {
103+
return from(() -> value);
104+
}
105+
94106
/**
95107
* Return a new {@link Source} from the specified value supplier that can be used to
96108
* perform the mapping.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,15 @@ public <T extends ThreadPoolTaskExecutor> T build(Class<T> taskExecutorClass) {
279279
*/
280280
public <T extends ThreadPoolTaskExecutor> T configure(T taskExecutor) {
281281
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
282-
map.from(() -> this.queueCapacity).to(taskExecutor::setQueueCapacity);
283-
map.from(() -> this.corePoolSize).to(taskExecutor::setCorePoolSize);
284-
map.from(() -> this.maxPoolSize).to(taskExecutor::setMaxPoolSize);
285-
map.from(() -> this.keepAlive).asInt(Duration::getSeconds)
282+
map.from(this.queueCapacity).to(taskExecutor::setQueueCapacity);
283+
map.from(this.corePoolSize).to(taskExecutor::setCorePoolSize);
284+
map.from(this.maxPoolSize).to(taskExecutor::setMaxPoolSize);
285+
map.from(this.keepAlive).asInt(Duration::getSeconds)
286286
.to(taskExecutor::setKeepAliveSeconds);
287-
map.from(() -> this.allowCoreThreadTimeOut)
288-
.to(taskExecutor::setAllowCoreThreadTimeOut);
289-
map.from(() -> this.threadNamePrefix).whenHasText()
287+
map.from(this.allowCoreThreadTimeOut).to(taskExecutor::setAllowCoreThreadTimeOut);
288+
map.from(this.threadNamePrefix).whenHasText()
290289
.to(taskExecutor::setThreadNamePrefix);
291-
map.from(() -> this.taskDecorator).to(taskExecutor::setTaskDecorator);
290+
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
292291

293292
if (!CollectionUtils.isEmpty(this.taskExecutorCustomizers)) {
294293
for (TaskExecutorCustomizer customizer : this.taskExecutorCustomizers) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ public ThreadPoolTaskScheduler build() {
167167
*/
168168
public <T extends ThreadPoolTaskScheduler> T configure(T taskScheduler) {
169169
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
170-
map.from(() -> this.poolSize).to(taskScheduler::setPoolSize);
171-
map.from(() -> this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix);
170+
map.from(this.poolSize).to(taskScheduler::setPoolSize);
171+
map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix);
172172

173173
if (!CollectionUtils.isEmpty(this.taskSchedulerCustomizers)) {
174174
for (TaskSchedulerCustomizer customizer : this.taskSchedulerCustomizers) {

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)